默认方式一 方式2 目的 是为了 方式3 应为我们需要排除掉原来package包
<build>
<!-- 过滤resource中配置 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!--环境变量替换-->
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!--方式一、打包成依赖包,用来提供给第三方-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>1.8</source>-->
<!-- <target>1.8</target>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!--方式二、打包成可执行包,用来运行-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- <mainClass>com.speedchina.permission.web.PermissionWebApplication</mainClass>-->
<!-- <classifier>exec</classifier>-->
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--打包成依赖包,用来提供给第三方-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<!--依赖jar包名称-->
<finalName>${project.artifactId}-${project.version}</finalName>
<!-- <archive>-->
<!-- <manifest>-->
<!-- <!– 指定程序入口 –>-->
<!-- <mainClass>com.speedchina.permission.web.PermissionWebApplication</mainClass>-->
<!-- <!– 打包时 MANIFEST.MF文件不记录的时间戳版本 –>-->
<!-- <useUniqueVersions>false</useUniqueVersions>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- <!– 服务依赖的jar包放在lib目录下 –>-->
<!-- <classpathPrefix>lib/</classpathPrefix>-->
<!-- <addDefaultImplementationEntries>true</addDefaultImplementationEntries>-->
<!-- <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>-->
<!-- </manifest>-->
<!-- <manifestEntries>-->
<!-- <!–MANIFEST.MF 中 Class-Path 加入资源文件目录 –>-->
<!-- <Class-Path>./resources/</Class-Path>-->
<!-- <Implementation-Build>${project.version}</Implementation-Build>-->
<!-- </manifestEntries>-->
<!-- </archive>-->
<!-- jar包中不包括以下文件 -->
<excludes>
<exclude>*.yml</exclude>
<exclude>*.xml</exclude>
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<!--方式三、assembly打包,使用配置文件路径src/main/assembly/assembly.xml-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- deploy时只上传jar包到远程仓库的配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<!-- <configuration>-->
<!-- <skip>true</skip>-->
<!-- </configuration>-->
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<!-- skip默认deploy插件的执行 -->
<configuration>
<skip>true</skip>
</configuration>
</execution>
<execution>
<id>deploy-file</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<!-- 开发阶段上传到snapshot仓库,上线阶段上传到release仓库 -->
<repositoryId>${project.distributionManagement.repository.id}</repositoryId>
<url>${project.distributionManagement.repository.url}</url>
<!--指定deploy依赖jar包的名字-->
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
<!--指定pom文件,保持jar依赖关系-->
<pomFile>pom.xml</pomFile>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- 可执行jar包名称,注 与start.sh,stop.sh等脚本关联。防止与依赖包jar重名,不加版本号,build下不要再加此配置-->
<finalName>${project.artifactId}</finalName>
</build>
jekins部分:(sourceFiles 这个地方 需要注意包的层级)
pipeline {
agent any
tools {
// 声明需要的工具
// Install the Maven version configured as "M3" and add it to the path.
maven "maven3.6"
// nodejs "nodejs"
}
environment{
// 发布到服务器的路径
DEPLOY_PATH='/speedchina/server-test/flowablePlatform/server/'
// 服务器上备份版本的路径
BACKUP_PATH='/speedchina/server-test/flowablePlatform/bak/server/'
// 分支
CODE_BRANCH=''
// 打包后的jar包所在路径 最后 / 必填
JAR_PATH=""
// jar包所在路径的前缀,默认为 target/
JAR_prefix='sd_Flowable_core/target/'
// 打包后的jar包名字(可执行的jar包)
JAR_NAME='sd_Flowable_core-1.0.tar.gz'
// git地址
// CODE_GIT_URL='http://192.168.10.112:8080/speedchinaADD/DevTools/Performance-Manage/performance-manage-server.git'
// CODE_GIT_URL='https://gitee.com/LateWinter/Base-Platform-Server.git'
CODE_GIT_URL='http://192.168.10.112:8080/speedchinaADD/DevTools/dev_flowable/sd_flowable_parent.git'
WECHAT_ROBOT='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8453c173-6cc1-497b-beec-97b59fb1e647'
}
stages {
stage('判断当前操作(发布 or 回滚 )') {
// 服务器操作执行的脚本
steps {
script{
echo "${DEPLOY_PATH}"
echo "${BACKUP_PATH}"
echo "$opt"
echo "${serverList}"
switch("$opt"){
case "deploy":
stage('拉取gitlable代码') {
cleanWs()
checkout([$class: 'GitSCM', branches: [[name: "${params.branch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'f9900871-159e-43a2-bced-5423cbc65fb6', url: "${CODE_GIT_URL}"]]])
// git branch: '${branch}', credentialsId: '60f19237-a2b3-435e-a2ee-9c945e73128d', url: "${CODE_GIT_URL}"
echo 'Hello World'
}
stage('mvn打包') {
sh "pwd"
withMaven(globalMavenSettingsConfig: '0a305e87-6bb0-4455-ad87-a4d6826de07e', jdk: 'jdk1.8', maven: 'maven3.6') {
echo "清理该项目依赖的本地仓库中的maven包"
sh "mvn dependency:purge-local-repository -DreResolve=false"
}
// sh "cd /var/jenkins_home/workspace/zhgd_test"
withMaven(globalMavenSettingsConfig: '0a305e87-6bb0-4455-ad87-a4d6826de07e', jdk: 'jdk1.8', maven: 'maven3.6') {
sh "mvn -B -DskipTests clean install -f pom.xml"
echo "构建代码"
}
}
// stage('归档成品'){
// archiveArtifacts allowEmptyArchive: true, artifacts: "target/${JAR_NAME}", followSymlinks: false, onlyIfSuccessful: true
// }
stage('sshPublisher') {
for (item in "${serverList}".tokenize(',')){
echo "${item}"
// Run Maven on a Unix agent.
sshPublisher(publishers: [sshPublisherDesc(configName: "${item}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /speedchina
mkdir -p '''+"${BACKUP_PATH}"+'''/${BUILD_NUMBER}
cp '''+"${JAR_NAME}"+''' '''+"${BACKUP_PATH}"+'''/${BUILD_NUMBER}
mv '''+"${JAR_NAME}"+''' '''+"${DEPLOY_PATH}"+'''/
cd '''+"${DEPLOY_PATH}"+'''
tar zxfv '''+"${JAR_NAME}"+'''
cd scripts
pwd
sh server.sh restart '''+"${active}"+'''
''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/', remoteDirectorySDF: false, removePrefix: "sd_Flowable_core/target/", sourceFiles: "sd_Flowable_core/target/${JAR_NAME}")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo "启动-"+"${item}"
}
}
break;
default:
echo "defalut"
stage('回滚代码') {
echo "回滚代码!${version}"
for (item in "${serverList}".tokenize(',')){
echo "${item}"
sshPublisher(publishers: [sshPublisherDesc(configName: "${item}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
if [ -f '''+"${BACKUP_PATH}"+'''/${version}/'''+"${JAR_NAME}"+''' ];then
echo "回滚的版本文件存在,可以回滚 。"
else
echo "回滚的版本文件不存在,回滚失败 !"
exit 127
fi
cp '''+"${BACKUP_PATH}"+'''/${version}/'''+"${JAR_NAME}"+''' '''+"${DEPLOY_PATH}"+'''/
cd '''+"${DEPLOY_PATH}"+'''
tar zxfv '''+"${JAR_NAME}"+'''
cd scripts
pwd
sh server.sh restart '''+"${active}"+'''
''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
break;
}
}
// Get some code from a GitHub repository
// To run Maven on a Windows agent, use
// bat "mvn -Dmaven.test.failure.ignore=true clean package"
}
}
}
// post {
// always{
// qyWechatNotification aboutSend: true, failSend: true, mentionedId: '', mentionedMobile: '', successSend: true, unstableSend: true, webhookUrl: "${WECHAT_ROBOT}"
// }
// }
// 旧版
// post{
// always{
// qyWechatNotification failNotify: true, mentionedId: '', mentionedMobile: '', webhookUrl: "${WECHAT_ROBOT}"
// }
// }
// dingding通知
}