1. 环境配置
jenkins安装 Maven Integration plugin
插件
配置maven,java等路径
2. 2.新建item,选择构建maven项目
a. 2.1 源码管理
填入git地址、所使用授权用户、分支
2.2 build配置
Root POM
pom.xml
Goals and options
clean package -DskipTests
2.3 Post Steps
勾选Run only if build succeeds
Add post-build step-exec shell,脚本内容如下
# Jenkins project name PROJECT_NAME=nhis-service # project main module PROJECT_MAIN=nhis-web # Jenkins build id OLD_BUILD_ID=$BUILD_ID # Manven project name APP_NAME=nhis-service # Base directotry in server APP_BASE_DIR=/home/zrapi/$APP_NAME echo "Build [$APP_NAME] successfully!" cd $APP_BASE_DIR cp $JENKINS_HOME/workspace/$PROJECT_NAME/$PROJECT_MAIN/target/$APP_NAME.jar ./ # 改变 Jenkins BUILD_ID防止脚本没执行完就被杀死 export BUILD_ID=dontKillMe # 恢复Jenkins BUILD_ID export BUILD_ID=$OLD_BUILD_ID
此时编译成功后,jar包会在/root/.jenkins/workspace/xxx/xxx 下
再次Add post-build step-exec shell,脚本内容如下
sudo systemctl restart 服务名称