最后
经过日积月累, 以下是小编归纳整理的深入了解Java虚拟机文档,希望可以帮助大家过关斩将顺利通过面试。
由于整个文档比较全面,内容比较多,篇幅不允许,下面以截图方式展示 。
由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
<assignedLabel>
<name>master</name>
</assignedLabel>
<mode>NORMAL</mode>
<nodeDescription>Jenkins的master节点</nodeDescription>
<nodeName></nodeName>
<numExecutors>1</numExecutors>
<job _class='org.jenkinsci.plugins.workflow.job.WorkflowJob'>
<name>deploy-test2</name>
<url>http://127.0.0.1:8080/jenkins/job/deploy-test2/</url>
<color>red</color>
</job>
<job _class='org.jenkinsci.plugins.workflow.job.WorkflowJob'>
<name>test-job</name>
<url>http://127.0.0.1:8080/jenkins/job/test-job/</url>
<color>blue</color>
</job>
<quietingDown>false</quietingDown>
<slaveAgentPort>50000</slaveAgentPort>
<unlabeledLoad _class='jenkins.model.UnlabeledLoadStatistics'></unlabeledLoad>
<useCrumbs>false</useCrumbs>
<useSecurity>true</useSecurity>
<view _class='hudson.model.AllView'>
<name>all</name>
<url>http://127.0.0.1:8080/jenkins/</url>
</view>
* JenkinsURL/api/xml显示:
{
“_class”: “hudson.model.Hudson”,
“assignedLabels”: [
{
“name”: “master”
}
],
“mode”: “NORMAL”,
“nodeDescription”: “Jenkins的master节点”,
“nodeName”: “”,
“numExecutors”: 1,
“description”: null,
“jobs”: [
{
“_class”: “org.jenkinsci.plugins.workflow.job.WorkflowJob”,
“name”: “deploy-test2”,
“url”: “http://127.0.0.1:8080/jenkins/job/deploy-test2/”,
“color”: “red”
},
{
“_class”: “org.jenkinsci.plugins.workflow.job.WorkflowJob”,
“name”: “test-job”,
“url”: “http://127.0.0.1:8080/jenkins/job/test-job/”,
“color”: “blue”
}
],
“overallLoad”: {},
“primaryView”: {
“_class”: “hudson.model.AllView”,
“name”: “all”,
“url”: “http://127.0.0.1:8080/jenkins/”
},
“quietingDown”: false,
“slaveAgentPort”: 50000,
“unlabeledLoad”: {
“_class”: “jenkins.model.UnlabeledLoadStatistics”
},
“useCrumbs”: false,
“useSecurity”: true,
“views”: [
{
“_class”: “hudson.model.AllView”,
“name”: “all”,
“url”: “http://192.168.2.11:8080/jenkins/”
}
]
}
还可以访问View、Job等API信息,例如:
- View API: /view//api/json
- Job API: /job//api/xml
- build API: /job///
### `三、调用接口前对Jenkins参数调整`
**1.关闭CSRF**
由于在调用Jenkins中,操作执行Job一些命令时会用到Post方式命令,所以需要关闭Jenkins的CSRF选项。
关闭 系统管理->全局安全配置->跨站请求伪造保护 选项
**2.系统设置中和jenkins地址一致**
设置 系统管理->系统设置->Jenkins Location 的 URL 和 Jenkins 访问地址保持一致
### `四、使用Java调用Jenkins API示例`
下面将演示如何通过Java调用Jenkins API 来对Jenkins进行操作,在注释中有详细描述,就不在外面一步步说明了。
本示例项目Github地址:https://github.com/my-dlq/blog-example/tree/master/jenkins-api-demo
**1.Maven引入工具**
在Jenkins Github中查找到已经有人做了封装调用Jenkins API的工具Jar,这里只需要我们Maven中引入这个工具java-client-api,就可以方便的调用Jenkins API了。
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<groupId>club.mydlq</groupId>
<artifactId>jenkins-api-demo</artifactId>
<version>0.0.1</version>
<name>jenkins-api-demo</name>
<description>jenkins api demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--jenkins-java-client-->
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>
</dependencies>
2.连接Jenkins工具类
JenkinsConnect.java
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import java.net.URI;
import java.net.URISyntaxException;/**
• 连接 Jenkins
*/
public class JenkinsConnect {
private JenkinsConnect(){}
// 连接 Jenkins 需要设置的信息
static final String JENKINS_URL = “http://192.168.2.11:8080/jenkins/”;
static final String JENKINS_USERNAME = “admin”;
static final String JENKINS_PASSWORD = “123456”;
/**• Http 客户端工具
•
• 如果有些 API 该Jar工具包未提供,可以用此Http客户端操作远程接口,执行命令
• @return
*/
public static JenkinsHttpClient getClient(){
JenkinsHttpClient jenkinsHttpClient = null;
try {
jenkinsHttpClient = new JenkinsHttpClient(new URI(JENKINS_URL), JENKINS_USERNAME, JENKINS_PASSWORD);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return jenkinsHttpClient;
}/**
• 连接 Jenkins
*/
public static JenkinsServer connection() {
JenkinsServer jenkinsServer = null;
try {
jenkinsServer = new JenkinsServer(new URI(JENKINS_URL), JENKINS_USERNAME, JENKINS_PASSWORD);
} catch (URISyntaxException e) {
e.printStackTrace();
}
return jenkinsServer;
}
}
3.操作视图
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.View;
import java.io.IOException;/**
• View(视图) 相关操作
•
• 例如对视图的增、删、改、查等操作
*/
public class ViewApi {
// Jenkins 对象
private JenkinsServer jenkinsServer;
// http 客户端对象
private JenkinsHttpClient jenkinsHttpClient;
/**• 构造方法中调用连接 Jenkins 方法
*/
ViewApi() {
JenkinsApi jenkinsApi = new JenkinsApi();
// 连接 Jenkins
jenkinsServer = JenkinsConnect.connection();
// 设置客户端连接 Jenkins
jenkinsHttpClient = JenkinsConnect.getClient();
}/**
• 创建视图
*/
public void createView() {
try {
// 创建一个 xml 字符串,里面设置一个 view 描述信息
String xml = “<listView _class=“hudson.model.ListView”>\n” +
“用于测试的视图\n” +
“”;
// 创建 view
jenkinsServer.createView(“test-view”, xml);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取视图基本信息
*/
public void getView() {
try {
// 视图名
String viewName = “test-view”;
// 获取视图基本信息
View view = jenkinsServer.getView(viewName);
System.out.println(view.getName());
System.out.println(view.getUrl());
System.out.println(view.getDescription());
// 获取视图xml信息
String viewXml = jenkinsHttpClient.get(“/view/” + viewName + “/api/xml”);
System.out.println(viewXml);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取视图配置 XML 信息
*/
public void getViewConfig() {
try {
// 视图名
String viewName = “test-view”;
// 获取视图配置xml信息
String viewConfigXml = jenkinsHttpClient.get(“/view/” + viewName + “/config.xml”);
System.out.println(viewConfigXml);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 更新视图信息
*/
public void updateView() {
try {
// 创建一个 xml 字符串,里面设置一个要修改的某些字段,具体xml可以到jenkins查看
// 例如,下面xml文件是从地址:https://Jenkins-IP/jenkins/view/test-view/config.xml 获取的
String xml = “<hudson.model.ListView>\n” +
“test-view\n” +
“用于测试的视图1111\n” +
“false\n” +
“false\n” +
“<properties class=“hudson.model.View$PropertyList”/>\n” +
“\n” +
“<comparator class=“hudson.util.CaseInsensitiveComparator”/>\n” +
“\n” +
“\n” +
“\n” +
“<hudson.views.StatusColumn/>\n” +
“<hudson.views.WeatherColumn/>\n” +
“<hudson.views.JobColumn/>\n” +
“<hudson.views.LastSuccessColumn/>\n” +
“<hudson.views.LastFailureColumn/>\n” +
“<hudson.views.LastDurationColumn/>\n” +
“<hudson.views.BuildButtonColumn/>\n” +
“<hudson.plugins.favorite.column.FavoriteColumn plugin=“favorite@2.3.2”/>\n” +
“\n” +
“false\n” +
“</hudson.model.ListView>”;
jenkinsServer.updateView(“test-view”, xml);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 删除视图
*/
public void deleteView() {
try {
String viewName = “test-view”;
jenkinsHttpClient.post(“/view/” + viewName + “/doDelete”);
} catch (IOException e) {
e.printStackTrace();
}
}public static void main(String[] args) {
ViewApi viewApi = new ViewApi();
// 创建视图
//viewApi.createView();
// 获取视图信息
//viewApi.getView();
// 获取视图配置xml信息
//viewApi.getViewConfig();
// 更新视图信息
//viewApi.updateView();
// 删除视图
//viewApi.deleteView();
}
}
4.操作任务(Job)
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.MavenJobWithDetails;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;/**
• Job(任务) 相关操作
•
• 例如对任务的增、删、改、查等操作
*/
public class JobApi {
// Jenkins 对象
private JenkinsServer jenkinsServer;
// http 客户端对象
private JenkinsHttpClient jenkinsHttpClient;
/**• 构造方法中调用连接 Jenkins 方法
*/
JobApi() {
JenkinsApi jenkinsApi = new JenkinsApi();
// 连接 Jenkins
jenkinsServer = JenkinsConnect.connection();
// 设置客户端连接 Jenkins
jenkinsHttpClient = JenkinsConnect.getClient();
}/**
• 创建 Job
*/
public void ceateJob(){
try {
/创建一个流水线任务,且设置一个简单的脚本/
// 创建 Pipeline 脚本
String script = “node(){ \n” +
“echo ‘hello world!’ \n” +
“}”;
// xml配置文件,且将脚本加入到配置中
String xml = “<flow-definition plugin=“workflow-job@2.32”>\n” +
“测试项目\n” +
“<definition class=“org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition” plugin=“workflow-cps@2.66”>\n” +
“\n” +
“true\n” +
“\n” +
“false\n” +
“”;
// 创建 Job
jenkinsServer.updateJob(“test-job”,xml);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取 Job 基本信息
*/
public void getJob(){
try {
// 获取 Job 信息
JobWithDetails job = jenkinsServer.getJob(“test-job”);
// 获取 Job 名称
System.out.println(job.getName());
// 获取 Job URL
System.out.println(job.getUrl());
// 获取 Job 下一个 build 编号
System.out.println(job.getNextBuildNumber());
// 获取 Job 显示的名称
System.out.println(job.getDisplayName());
// 输出 Job 描述信息
System.out.println(job.getDescription());
// 获取 Job 下游任务列表
System.out.println(job.getDownstreamProjects());
// 获取 Job 上游任务列表
System.out.println(job.getUpstreamProjects());
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取 Maven Job 信息
*/
public void getMavenJob(){
try {
// 获取 Job 信息
MavenJobWithDetails job = jenkinsServer.getMavenJob(“test-job”);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取 Job 列表
*/
public void getJobList(){
try {
// 获取 Job 列表
Map<String,Job> jobs = jenkinsServer.getJobs();
for (Job job:jobs.values()){
System.out.println(job.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取 View 名称获取 Job 列表
*/
public void getJobListByView(){
try {
// 获取 Job 列表
Map<String,Job> jobs = jenkinsServer.getJobs(“all”);
for (Job job:jobs.values()){
System.out.println(job.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 查看 Job XML 信息
*/
public void getJobConfig(){
try {
String xml = jenkinsServer.getJobXml(“test-job”);
System.out.println(xml);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 执行无参数 Job build
*/
public void buildJob(){
try {
jenkinsServer.getJob(“test-job”).build();
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 执行带参数 Job build
/
public void buildParamJob(){
try {
/*
* 例如,现有一个job,拥有一个字符参数"key"
* 现在对这个值进行设置,然后执行一个输出这个值的脚本
*/
// 设置参数值
Map<String,String> param = new HashMap<>();
param.put(“key”,“hello world!”);
// 执行 build 任务
jenkinsServer.getJob(“test-job”).build(param);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 停止最后构建的 Job Build
*/
public void stopLastJobBuild(){
try {
// 获取最后的 build 信息
Build build = jenkinsServer.getJob(“test-job”).getLastBuild();
// 停止最后的 build
build.Stop();
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 删除 Job
*/
public void deleteJob(){
try {
jenkinsServer.deleteJob(“test-job”);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 禁用 Job
*/
public void disableJob(){
try {
jenkinsServer.disableJob(“test-job”);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 启用 Job
*/
public void enableJob(){
try {
jenkinsServer.enableJob(“test-job”);
} catch (IOException e) {
e.printStackTrace();
}
}public static void main(String[] args) {
JobApi jobApi = new JobApi();
// 创建 Job
jobApi.ceateJob();
// 构建无参数的 Job
jobApi.buildJob();
// 构建带参数的 Job
jobApi.buildParamJob();
// 停止最后构建的 Job Build
jobApi.stopLastJobBuild();
// 更新 Job
jobApi.updateJob();
// 获取 Job 信息
jobApi.getJob();
// 获取 Maven 项目 Job
jobApi.getMavenJob();
// 获取 Job 配置xml
jobApi.getJobConfig();
// 获取全部 Job 列表
jobApi.getJobList();
// 根据 view 名称获取 Job 列表
jobApi.getJobListByView();
// 禁用 Job
jobApi.disableJob();
// 启用 Job
jobApi.enableJob();
// 删除 Job
jobApi.deleteJob();
}}
5.操作编译(build)
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.helper.Range;
import com.offbytwo.jenkins.model.*;
import java.io.IOException;
import java.util.List;/**
• Job Build(任务构建) 相关操作
•
• 例如对任务 Build 相关的信息进行获取操作、例如获取构建日志
*/
public class JobBuildApi {
// Jenkins 对象
private JenkinsServer jenkinsServer;
// http 客户端对象
private JenkinsHttpClient jenkinsHttpClient;
/**• 构造方法中调用连接 Jenkins 方法
*/
JobBuildApi() {
JenkinsApi jenkinsApi = new JenkinsApi();
// 连接 Jenkins
jenkinsServer = JenkinsConnect.connection();
// 设置客户端连接 Jenkins
jenkinsHttpClient = JenkinsConnect.getClient();
}/**
• 获取 Job 最后的 Build
*/
public void getJobLastBuild(){
try {
// 获取 Job 信息
JobWithDetails job = jenkinsServer.getJob(“test-job”);
// 获得最后编译信息
Build lastBuild = job.getLastBuild();
// 获取最后成功的编译信息
Build lastSuccessfulBuild = job.getLastSuccessfulBuild();
// 获取最后事变的编译信息
Build lastFailedBuild = job.getLastFailedBuild();
// 获取最后完成的编译信息
Build lastCompletedBuild = job.getLastCompletedBuild();
// 获取最后稳定的编译信息
Build lastStableBuild = job.getLastStableBuild();
// 获取最后不稳定的编译信息
Build lastUnstableBuild = job.getLastUnstableBuild();
// 获取最后未成功的编译信息
Build lastUnsuccessfulBuild = job.getLastUnsuccessfulBuild();
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取 Job 首次 Build
*/
public void getJobFirstBuild(){
try {
// 获取 Job 信息
JobWithDetails job = jenkinsServer.getJob(“test-job”);
// 获得首次编译信息
Build firstBuild = job.getFirstBuild();
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 根据 Job Build 编号获取编译信息
*/
public void getJobByNumber(){
try {
// 获取 Job 信息
JobWithDetails job = jenkinsServer.getJob(“test-job”);
// 根据
Build numberBuild = job.getBuildByNumber(1);
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取全部 Job Build列表
*/
public void getJobBuildListAll(){
try {
// 获取 Job 信息
JobWithDetails job = jenkinsServer.getJob(“test-job”);
// 获取全部 Build 信息
List builds = job.getAllBuilds();
for (Build build:builds){
System.out.println(build.getNumber());
}
} catch (IOException e) {
e.printStackTrace();
}
}/**
• 获取 Job 一定范围的 Build 列表
*/
public void getJobBuildListRange(){
try {
// 获取 Job 信息
JobWithDetails job = jenkinsServer.getJob(“test-job”);
// 设定范围
Range range = Range.build().from(1).to(2);
System.err.println(range.getRangeString());