首先,jenkins页面的构建按钮点击,都是一个get请求,知道了这一点我们就好办了。
第二步,就是用JAVA代码处理发送请求
/** * 向指定URL发送GET方法的请求 * 发起构建请求 * @param buildname * jenkins中的构建名称 * @param param * 延迟多少秒进行构建 * @return URL 所代表远程资源的响应结果 */ public static String sendBuilding(String buildname, int param) { String result = ""; BufferedReader in = null; try { final String jenkinsurl = "http://xx.xxx.xx.xx:18080/jenkins/job/"; String urlString = jenkinsurl+buildname + "/build?delay="+param+"sec"; URL realUrl = new URL(urlString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { JavaBaseTest.LogUtil.APP.info(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { JavaBaseTest.LogUtil.APP.error("发送构建请求(GET)时出现异常!", e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }
第三步,发送完GET请求,我们就要知道当前构建有没有成功呢,请往下看
/** * 向指定URL发送GET方法的请求 * 判断最后一次构建有没有成功 * @param buildname * jenkins中的构建名称 * @return URL 所代表远程资源的响应结果 * * alt="Success" alt="In progress" alt="Failed" */ public static String BuildingResult(String buildname) { String result = ""; BufferedReader in = null; try { final String jenkinsurl = "http://xx.xxx.xx.xx:18080/jenkins/job/"; String urlString = jenkinsurl+buildname + "/lastBuild/"; URL realUrl = new URL(urlString); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map<String, List<String>> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { JavaBaseTest.LogUtil.APP.info(key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { JavaBaseTest.LogUtil.APP.error("发送构建请求(GET)时出现异常!", e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }