背景:Jenkins官方文档只提供了python的api,没有提供java的API,但是java相对于python使用起来又得心应手,所以在网上找了半天java版本的API,在这里记录下,顺便跟大家分享下
Jenkins API for java 的github地址:https://github.com/jenkinsci/java-client-api
首先需要pom.xml文件中添加maven依赖com.offbytwo.jenkins的jar包:
<!-- https://mvnrepository.com/artifact/com.offbytwo.jenkins/jenkins-client -->
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.2.0</version>
</dependency>
在需要用到这个jar包的文件里面添加下面的语句:
import com.offbytwo.jenkins.*;
基本用法:
String username=***;
String password=***;
String url="****";
JenkinsServer jenkins=new JenkinsServer(new URI(url),username,password);
Map<String,Job> jobs=jenkins.getJobs();
for(String keymame:jobs.keysets()){
System.out.println(keyname)
}
JobWithDetails jobdetails=null;
Job job;
job=jobs.get("******");
jobdetails=job.details();
Build lastbuild=jobdetails.getLastBuild();
BuildWithDetails buildDetails=lastbuild.details();
if(buildDetails.isBuilding()){
System.out.println("最后一次构建还没完成,还在构建中");
}else{
System.out.println("最后一次构建已完成");
}