• 大小: 5.33MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-08-12
  • 语言: Java
  • 标签: java  jenkins  api  

资源简介

附件是使用java调用jenkins api的demo,希望对您有所帮助

资源截图

代码片段和文件信息

package jenkinsApi;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class test {
// Credentials
static String username = “username“;
static String password = “password“;

// Jenkins url
static String jenkinsUrl = “http://localhost:8080“;

// Build name
static String jobName = “jobname“;

// Build token
static String buildToken = “buildToken“;

public static void main(String[] args) {
System.out.println(“start“);
try {
String result = createJob();//build the job
System.out.println(result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(“end“);
}

public static String getBuilds()throws ClientProtocolException IOException{
String urlString = jenkinsUrl + “/job/“+jobName+“/api/json?tree=builds[number]{010}“;

URI uri = URI.create(urlString);
HttpHost host = new HttpHost(uri.getHost() uri.getPort() uri.getScheme());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost() uri.getPort()) new UsernamePasswordCredentials(username password));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(host basicAuth);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
HttpGet httpGet = new HttpGet(uri);
//httpPost.setEntity(reqEntity);
// Add AuthCache to the execution context
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);

HttpResponse response = httpClient.execute(host httpGet localContext);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
return result;
}

public static String getJobs()throws ClientProtocolException IOException{
String urlString = jenkin

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-02-24 13:59  jenkinsApi\
     文件        1007  2016-02-25 08:56  jenkinsApi\.classpath
     目录           0  2016-02-25 15:25  jenkinsApi\.git\
     文件         566  2016-02-24 13:59  jenkinsApi\.git\config
     文件          73  2016-02-23 07:54  jenkinsApi\.git\description
     文件         206  2016-02-25 15:23  jenkinsApi\.git\FETCH_HEAD
     文件          24  2016-02-24 13:59  jenkinsApi\.git\HEAD
     目录           0  2016-02-23 07:54  jenkinsApi\.git\hooks\
     文件         452  2016-02-23 07:54  jenkinsApi\.git\hooks\applypatch-msg.sample
     文件         896  2016-02-23 07:54  jenkinsApi\.git\hooks\commit-msg.sample
     文件         189  2016-02-23 07:54  jenkinsApi\.git\hooks\post-update.sample
     文件         398  2016-02-23 07:54  jenkinsApi\.git\hooks\pre-applypatch.sample
     文件        1642  2016-02-23 07:54  jenkinsApi\.git\hooks\pre-commit.sample
     文件        1352  2016-02-23 07:54  jenkinsApi\.git\hooks\pre-push.sample
     文件        4951  2016-02-23 07:54  jenkinsApi\.git\hooks\pre-rebase.sample
     文件        1239  2016-02-23 07:54  jenkinsApi\.git\hooks\prepare-commit-msg.sample
     文件        3611  2016-02-23 07:54  jenkinsApi\.git\hooks\update.sample
     文件        1608  2016-02-25 14:51  jenkinsApi\.git\index
     目录           0  2016-02-23 07:54  jenkinsApi\.git\info\
     文件         240  2016-02-23 07:54  jenkinsApi\.git\info\exclude
     目录           0  2016-02-23 07:54  jenkinsApi\.git\logs\
     文件         354  2016-02-24 13:59  jenkinsApi\.git\logs\HEAD
     目录           0  2016-02-23 07:54  jenkinsApi\.git\logs\refs\
     目录           0  2016-02-24 13:59  jenkinsApi\.git\logs\refs\heads\
     文件         164  2016-02-24 13:59  jenkinsApi\.git\logs\refs\heads\develop
     文件         186  2016-02-23 07:54  jenkinsApi\.git\logs\refs\heads\master
     目录           0  2016-02-23 07:54  jenkinsApi\.git\logs\refs\remotes\
     目录           0  2016-02-23 15:39  jenkinsApi\.git\logs\refs\remotes\origin\
     文件         154  2016-02-23 15:39  jenkinsApi\.git\logs\refs\remotes\origin\develop
     文件         186  2016-02-23 07:54  jenkinsApi\.git\logs\refs\remotes\origin\HEAD
     目录           0  2016-02-23 15:39  jenkinsApi\.git\objects\
............此处省略104个文件信息

评论

共有 条评论