如何使用Java获取Jenkins的工作清单? [英] how to get the list of jobs in Jenkins using java?

查看:125
本文介绍了如何使用Java获取Jenkins的工作清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在服务器中下载并配置了Jenkins,我的问题是我需要通过Java访问Jenkins来执行某些过程,例如开始工作,返回当前工作任务以及返回服务器中的工作列表(所有这些使用Json),我尝试了几种代码,例如,但是即时通讯没有结果,我也找不到一种清晰的方法来实现这一点,是否有任何清晰的API和示例可以做到这一点?

I have downloaded and configured Jenkins in a server, my problem is that i need to access Jenkins through Java to perform some process such as starting a job, returning the current working job and returning list of jobs in the server(all that using Json) i've tried several codes such as this but im getting no results, also i cant find a clear way to achieve that, is there is any clear API and example to do it?

推荐答案

您可以使用基于XML的詹金斯API :

    import org.dom4j.io.*;
    import org.dom4j.*;
    import java.net.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://your-hudson-server.local/hudson/api/xml");
            Document dom = new SAXReader().read(url);

            for( Element job : (List<Element>)dom.getRootElement().elements("job")) {
                System.out.println(String.format("Job %s has status %s",
                    job.elementText("name"), job.elementText("color")));
            }
        }
    }

可以在此处找到完整的示例(包括源代码) .

如果这些示例不起作用,则可能是Jenkins Security(您的客户端必须提供登录数据才能发送请求)或CSRF保护(您必须在第一个请求之前获取令牌并添加此令牌)有问题令牌作为每个请求的参数).

If these examples don't work, you might have problems with Jenkins Security (your client must provide login data before it can send the request)or with CSRF protection (you have to retrieve a token before the first request and add this token as a parameter to each request).

这篇关于如何使用Java获取Jenkins的工作清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆