Maven trigger release remotely

I want to run the Maven release programmatically from a Java program. This web page shows how this is done in general. So what I did:

final URL url = new URL("http://jenkins/job/MyProject/m2release/submit"); final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); final String userpass = "User:Password"; final String authentication = "Basic " + DatatypeConverter.printBase64Binary(userpass.getBytes()); urlConnection.setRequestProperty("Authorization", authentication); try (final OutputStream os = urlConnection.getOutputStream(); final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));) { writer.write("releaseVersion=1.2.3&"); writer.write("developmentVersion=1.2.4-SNAPSHOT&"); // writer.write("isDryRun=on&"); // uncomment for dry run writer.write("scmUsername=User&"); writer.write("scmPassword=Password&"); writer.write("scmCommentPrefix=[test]&"); writer.write("json={\"releaseVersion\":\"1.2.3\",\"developmentVersion\":\"1.2.4-SNAPSHOT\",\"isDryRun\":false}&"); writer.write("Submit=Schedule Maven Release Build"); writer.flush(); } urlConnection.connect(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), "UTF-8"))) { for (String line; (line = reader.readLine()) != null;) { System.out.println(line); } } 

This forum offers "just take a look at the form before release and you can process the cURL request", which I did with go this far. The release starts at least.

I just don’t understand how to avoid everything. The browser shows spaces as "+", but it does not work if I send data this way. In fact, neither "", "+", nor "% 20" work like space.

However, I get Unable to commit files in the assembly, so I'm sure something is wrong with the username / password / comment prefix. Jenkins himself returns the login page ("Authentication Required"), even though the login is being sent.

What is the correct way to run Maven Release on Jenkins?

+8
jenkins
source share
3 answers

Well, the options missing from my old approach were:

 writer.write("specifyScmCredentials=on&"); writer.write("specifyScmCommentPrefix=on&"); 
+4
source share

The statement that comes to mind here is "Not all Jenkins assignments are equal."

The code you posted just invokes a Jenkins job called MyProject, with releaseVersion and developmentVersion parameters.

However, the code has nothing to do with what MyProject does. This could be a task designed to create a Maven project or a Gradle project or a .NET project.

What you want to do (invoke the Maven release plugin) lies with Jenkins itself.

Look at the configuration of MyProject, in particular, by invoking the Maven build step, which launches the release plugin.

useful links

0
source share

By the way, another valuable thing worth mentioning is that you can pass custom parameters as part of this http://jenkins/job/MyProject/m2release/submit?json={} . To do this, define the json request parameter β†’ for example

 json={"parameter": {"name":"CUSTOM_PARAM_1", "value":"CUSTOM_PARAM_1_VALUE"}} 

At the time of execution, this will be considered as a parameterized task, and you will have your parameter along with

 MVN_RELEASE_VERSION=X MVN_DEV_VERSION=X-SNAPSHOT MVN_ISDRYRUN=false CUSTOM_PARAM_1=CUSTOM_PARAM_1_VALUE 
0
source share

All Articles