Packagecom.vip.webpagetest.utils;ImportJava.io.InputStream;Importjava.io.UnsupportedEncodingException;Importjava.util.List;Importorg.apache.commons.httpclient.HttpClient;Importorg.apache.commons.httpclient.UsernamePasswordCredentials;ImportOrg.apache.commons.httpclient.auth.AuthScope;ImportOrg.apache.commons.httpclient.methods.GetMethod;Importorg.apache.commons.httpclient.methods.InputStreamRequestEntity;ImportOrg.apache.commons.httpclient.methods.PostMethod;Importorg.apache.commons.httpclient.methods.RequestEntity;Importorg.apache.commons.httpclient.methods.StringRequestEntity;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.Resource;Import StaticCom.jayway.restassured.path.json.JsonPath.with; Public classJenkinsutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (jenkinsutil.class); StaticString Jenkinsbaseurl = Classpathpropertiesutils.getproperty ("Jenkins.server"); StaticString userName = Classpathpropertiesutils.getproperty ("Jenkins.username"); StaticString ApiToken = Classpathpropertiesutils.getproperty ("Jenkins.apitoken"); /*** Create a Jenkins Job *@paramJobName *@throwsException*/ Public Static voidCreatjenkinsjob (String jobName)throwsexception{if(Isjenkinsjobexist (jobName)) {Logger.info ("Job already exists:" +jobName); }Else{HttpClient Client=NewHttpClient (); Usernamepasswordcredentials creds=Newusernamepasswordcredentials (Username,apitoken); Client.getstate (). SetCredentials (Authscope.any,creds); Client.getparams (). Setauthenticationpreemptive (true); Postmethod Post=NewPostmethod (jenkinsbaseurl+ "/createitem?name=" +jobName); Post.setdoauthentication (true); Resource Resource=NewClasspathresource ("Config + +"); InputStream FileInput=Resource.getinputstream (); Inputstreamrequestentity requestentity=NewInputstreamrequestentity (FileInput, "text/xml; Charset=utf-8 "); Post.setrequestentity (requestentity); intCode =Client.executemethod (POST); Logger.info ("Job created successfully:" +jobName); } } /*** Query for the existence of a job named JobName *@paramJobName *@return * @throwsException*/ Public Static BooleanIsjenkinsjobexist (String jobName)throwsexception{HttpClient Client=NewHttpClient (); Usernamepasswordcredentials creds=Newusernamepasswordcredentials (Username,apitoken); Client.getstate (). SetCredentials (Authscope.any,creds); Client.getparams (). Setauthenticationpreemptive (true); GetMethod Get=NewGetMethod (jenkinsbaseurl+ "/api/json"); Get.setdoauthentication (true); Client.executemethod (get); String result=get.getresponsebodyasstring (); List<String> joblist = with (result). GetList ("Jobs.name"); for(String job:joblist) {if(Jobname.equals (Job)) {return true; } } return false; } /*** Delete Jenkins Job *@paramJobName *@throwsException*/ Public Static voidDeletejenkinsjob (String jobName)throwsexception{if(!isjenkinsjobexist (JobName)) {Logger.info ("Job not present:" +jobName); }Else{HttpClient Client=NewHttpClient (); Usernamepasswordcredentials creds=Newusernamepasswordcredentials (Username,apitoken); Client.getstate (). SetCredentials (Authscope.any,creds); Client.getparams (). Setauthenticationpreemptive (true); Postmethod Post=NewPostmethod (jenkinsbaseurl+ "/job/" +jobname+ "/dodelete"); Post.setdoauthentication (true); Client.executemethod (POST); } } /*** Build Trigger Jenkins Job *@paramJobName *@throwsException*/ Public Static BooleanBuildjenkinsjob (String jobName)throwsexception{if(!isjenkinsjobexist (JobName)) {Logger.info ("Job not present:" +jobName); return false; }Else{HttpClient Client=NewHttpClient (); Usernamepasswordcredentials creds=Newusernamepasswordcredentials (Username,apitoken); Client.getstate (). SetCredentials (Authscope.any,creds); Client.getparams (). Setauthenticationpreemptive (true); Postmethod Post=NewPostmethod (jenkinsbaseurl+ "/job/" +jobname+ "/build"); Post.setdoauthentication (true); intCode =Client.executemethod (POST); if(Code = = 201) {Logger.info ("The build job has started:" +jobName); return true; } } return false; } /*** Built with parameters *@paramJobName *@paramparamsjson--{"parameter": {"name": "XXX", "value": "XXX"}} *@return * @throwsException*/ Public Static BooleanBuildjenkinsjobwithparameters (String jobname,string Paramsjson)throwsexception{if(!isjenkinsjobexist (JobName)) {Logger.info ("Job not present:" +jobName); return false; }Else{HttpClient Client=NewHttpClient (); Usernamepasswordcredentials creds=Newusernamepasswordcredentials (Username,apitoken); Client.getstate (). SetCredentials (Authscope.any,creds); Client.getparams (). Setauthenticationpreemptive (true); Postmethod Post=NewPostmethod (jenkinsbaseurl+ "/job/" +jobname+ "/build"); Requestentity Entity=NewStringrequestentity (Paramsjson, "Application/json", "UTF-8"); Post.setrequestentity (entity); Post.setrequestheader ("Content-type", "Application/json;charset=utf-8"); Client.executemethod (POST); return true; } } /*** Terminate Jenkins job Build *@paramJobName *@return * @throwsException*/ Public Static BooleanStopjenkinsjob (String jobName)throwsexception{if(!isjenkinsjobexist (JobName)) {Logger.info ("Job not present:" +jobName); return false; }Else{HttpClient Client=NewHttpClient (); Usernamepasswordcredentials creds=Newusernamepasswordcredentials (Username,apitoken); Client.getstate (). SetCredentials (Authscope.any,creds); Client.getparams (). Setauthenticationpreemptive (true); Postmethod Post=NewPostmethod (jenkinsbaseurl+ "/job/" +jobname+ "/api/json"); Post.setdoauthentication (true); Client.executemethod (POST); String result=post.getresponsebodyasstring (); intBuildNumber = with (result). Get ("Lastbuild.number"); Postmethod stopjenkinsrequest=NewPostmethod (jenkinsbaseurl+ "/job/" +jobname+ "/" +buildnumber+ "/stop"); Client.executemethod (stopjenkinsrequest); return true; } } Public Static voidMain (string[] args)throwsException {creatjenkinsjob ("Yytest"); }}
A Java Jenkinsutil tool class that creates, deletes, and constructs functions like Jenkins.