(1)新建maven構建的java項目
pom.xml的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.demo</groupId> <artifactId>jenkins_jar</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jenkins_jar</name> <url>http://maven.apache.org</url> <build> <finalName>jenkins_jar</finalName> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${compiler.source}</source> <target>${compiler.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- 源碼打包 <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.1</version> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!--這里 Demo為定義主方法的類名,就是程序的主類--><mainClass>cn.demo.jenkins_jar.demo.Demo</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <compiler.source>1.7</compiler.source> <compiler.target>1.7</compiler.target> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
(2)新建jenkins項目,開始配置
(3)前面就是配置項目的描述,以及源碼關聯 和以往的配置一樣
(4)在Pre-step這一欄配置中 選擇 execute shell

(5)在execute shell 框框中填寫腳本命令 (也可以把腳本命令寫在本機某個位置,然后執行該腳本 比如 把命令都放到 /home/start.sh 文件中 ,然后再在腳本中就寫 sh /home/start.sh)

(6)腳本命令詳解 (#代表注釋內容,第一行不屬於注釋,不能省略)
#!/bin/bash
cd /usr/local/tool/jenkins_program/jenkins_jar #進入指定目錄
#得到進程ID pid,kill該進程
pid=`cat /usr/local/tool/jenkins_program/jenkins_jar/pid` #得到/usr/local/tool/jenkins_program/jenkins_jar/目錄下 pid文件中的內容
if [ -n "$pid" ]
then
echo "kill -9 的pid:" $pid
kill -9 $pid #kill進程ID為 pid 的進程
fi
#將要執行的jar 復制到指定目錄下 (這里可以不用寫,如果寫的話 /usr/lib/jenkins/workspace/jenkins_jar/target/jenkins_jar.jar 是你jenkins中工作區間中你的項目的jar包存放位置
/usr/local/tool/jenkins_program/jenkins_jar 是你的目標執行該jar的位置,如果你打算直接在工作區間目錄下執行,這個可以省略不寫)
cp /usr/lib/jenkins/workspace/jenkins_jar/target/jenkins_jar.jar /usr/local/tool/jenkins_program/jenkins_jar
#執行jar,並將進程掛起,保存進程ID到 pid文件
echo "Execute shell Finish"
BUILD_ID=dontKillMe nohup java -jar /usr/lib/jenkins/workspace/jenkins_jar/target/jenkins_jar.jar &
echo "$!" > pid #得到剛剛啟動的進程id,寫到/usr/local/tool/jenkins_program/jenkins_jar目錄下的pid文件中,這就是上面cd語句的作用
BUILD_ID=dontKillMe :防止殺死剛剛啟動的進程
nohup java -jar /usr/lib/jenkins/workspace/jenkins_jar/target/jenkins_jar.jar & #執行項目jar包,將進程啟動並且掛起
(7)配置完后可以執行構建了,結果會直接在Console Output輸出
