(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输出