我正在使用这个 Maven 库连接到 Jenkins:
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.8</version>
</dependency>
然后我试图在 Jenkins 中获得所有可用的工作,并且通过以下方式在 Jenkins 中获得所有可用的工作:
JenkinsServer jServer = new JenkinsServer(new URI(JENKINS_URL), JENKINS_USER_NAME, JENKINS_PASSWORD);
Map<String, Job> jobs = jServer.getJobs();
jobs.forEach((k, v) -> {
System.out.println("Job -> " + k.toString());
});
JobWithDetails job = jobs.get(JENKINS_JOB_NAME).details();
job.getBuilds().forEach(b -> System.out.println("Build Details : " + b.getNumber()));
现在,我想在 Jenkins 中创建一个作业,从而调用 API 中提供的以下代码:
try {
jServer.createJob("test-job", "src/main/resources/config.xml", true);
} catch (Exception e) {
e.printStackTrace();
}
配置.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<actions />
<description>test job from java api</description>
<keepDependencies>false</keepDependencies>
<properties />
<scm class="hudson.scm.NullSCM" />
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers />
<concurrentBuild>false</concurrentBuild>
<builders />
<publishers />
<buildWrappers />
<crumb>xxxxxxx</crumb>
<crumbRequestField>Jenkins-Crumb</crumbRequestField>
</project>
当我通过调用 createJob() 运行这个程序时,我得到了异常:
org.apache.http.client.HttpResponseException: Forbidden
at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11)
at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375)
at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389)
at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:359)
at com.xxx.App.main(App.java:34)
调用 getJobXml() 时我也遇到异常:
Exception in thread "main" org.apache.http.client.HttpResponseException: Forbidden
at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11)
at com.offbytwo.jenkins.client.JenkinsHttpClient.get(JenkinsHttpClient.java:171)
at com.offbytwo.jenkins.JenkinsServer.getJobXml(JenkinsServer.java:515)
at com.offbytwo.jenkins.JenkinsServer.getJobXml(JenkinsServer.java:503)