java调用jenkins-client实现连接jenkins服务

  • java调用jenkins-client实现连接jenkins服务已关闭评论
  • 1,863 views
  • A+
所属分类:未分类

jenkins-client 底层是使用 HttpClient HttpPost 发送post 请求实现的,我们直接用就行,直接实战吧

添加maven依赖

<dependency>
    <groupId>com.offbytwo.jenkins</groupId>
    <artifactId>jenkins-client</artifactId>
    <version>0.3.8</version>
</dependency>

下面是通过单例模式创建的JenkinsServer连接,代码如下:

package com.os.devops.jenkins.utils;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Job;
import com.os.devops.utils.Host;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
/**
 * Created by xubo-os on 2018/8/8.
 */
public class JenkinsClient {
    static final Logger LOG = LoggerFactory.getLogger(JenkinsClient.class);
    private JenkinsServer jenkinsServer = null;

    private JenkinsClient() {

    }

    /**
     * 静态内部类,单例
     */
    private static class JenkinsClientHandle {
        private static final JenkinsClient js = new JenkinsClient();
    }

    public static final JenkinsClient getInstance() {
        return JenkinsClientHandle.js;
    }

    /**
     * 创建jenkins连接
     *
     * @return
     */
    public JenkinsServer getConnection() {

        try {
            if (jenkinsServer == null) {
                this.jenkinsServer = new JenkinsServer(new URI(Host.getAsString("jenkinsurl")), Host.getAsString("jenkinsusername"),
                        Host.getAsString("jenkinspassword"));
            }

        } catch (Exception e) {
            LOG.error("jenkins连接异常", e);
        }
        LOG.info("jenkins连接成功");
        return this.jenkinsServer;
    }
}

注:

  • jenkinsurl为jenkins的访问url,比如:http://192.168.201.64:8899
  • jenkinsusername为jenkins的登录账户
  • jenkinspassword为jenkins的登录账户密码

好了,就是这么简单,下一篇我们将创建job

 

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar