Jenkins2 pipeline example
//install private jar
//mvn install:install-file -Dfile=/root/json-1.0.0.jar -DgroupId=json -DartifactId=json -Dversion=1.0.0 -Dpackaging=jar
//mvn install:install-file -Dfile=/root/jackson-all-1.8.5.jar -DgroupId=jackson -DartifactId=jackson-all -Dversion=1.8.5 -Dpackaging=jar
//mvn install:install-file -Dfile=/root/decoder-1.0.0.jar -DgroupId=Decoder -DartifactId=Decoder -Dversion=1.0.0 -Dpackaging=jar
//mvn install:install-file -Dfile=/root/HttpSender-1.0.0.jar -DgroupId=HttpSender -DartifactId=HttpSender -Dversion=1.0.0 -Dpackaging=jar
node {
def server = "139"
def mvnHome = "/usr/lib/apache-maven-3.3.9"
def parentHome = "yuehui-parent"
def buildPath = "${WORKSPACE}/yuehui-web/target"
def propertiesPath = "${WORKSPACE}/yuehui-web/src/main/resources/properties/"
stage('SVN Checkout') { // for display purposes
// Get some code from a SVN repository
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[credentialsId: 'ca1efd2f-3f3f-456d-8d3a-4dc6dcc20f10', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'https://192.168.1.20/svn/yuehui/Java/code']], workspaceUpdater: [$class: 'UpdateUpdater']])
}
dir(parentHome) {
stage('change properties') {
def pwd = pwd()
sh "cp ${pwd}/build/${server}/* ${propertiesPath}"
}
//in pom.xml folder
stage('SonarQube analysis') {
withSonarQubeEnv {
// requires SonarQube Scanner for Maven 3.2+
sh "${mvnHome}/bin/mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar"
}
}
stage('Build and Rename') {
// Run the maven build
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
sh "mv ${buildPath}/yuehui-web-1.0.0-SNAPSHOT.war ${buildPath}/yuehui-web.war"
}
}
stage('Deploy to server') {
try{
sh '''echo "ready to transfer file..."
expect <<!
set timeout -1
spawn scp /var/lib/jenkins/workspace/yuehui-web/yuehui-web/target/yuehui-web.war root@xx.xx.xx.xx:/home/yuehui/software/apache/apache-tomcat-8.5.6-8084/webapps
expect {
"*yes/no" { send "yes\\r"; exp_continue}
"*password:" { send "xxxx\\r" }
}
expect "100%"
expect eof;
!
echo "finish transfer file..."'''
}catch(err){
//if no such file, throw exception
}
// junit '**/target/surefire-reports/TEST-*.xml'
// archive 'target/*.jar'
}
}