8

I want to start a Maven release programmatically from a Java program. This webpage shows how that is done generally. So that's 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 suggests "just have a look at the form befor you do a release and you should be able to craft a cURL request", that's what I did to get that far. The release starts at least.

I just don't now how to escape everything. The browser shows spaces as "+", but it does not work if I send the data that way. In fact, neither " ", "+" nor "%20" works as a space.

Still I get Unable to commit files in the build, so I'm pretty sure something is wrong with the username / password / comment prefix. The Jenkins itself returns the log-in page ("Authentication required"), even though the log-in is send.

What is the correct way to trigger a Maven Release on a Jenkins?

Stefan S.
  • 3,950
  • 5
  • 25
  • 77

3 Answers3

4

Okay, the parameters missing in my old approach were:

writer.write("specifyScmCredentials=on&");
writer.write("specifyScmCommentPrefix=on&");
Stefan S.
  • 3,950
  • 5
  • 25
  • 77
0

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

The code you have posted simply invokes a Jenkins job called MyProject with the parameters releaseVersion and developmentVersion.

However the code has nothing to do with what MyProject does. It could be a job designed to build a Maven project, or a Gradle project, or a .NET project.

What you want to do (invoke the Maven release plugin) is the responsibility of the Jenkins job itself.

Have a look at the configuration of MyProject, specifically invoking a Maven build step that runs the release plugin.

Useful links

tschaible
  • 7,635
  • 1
  • 31
  • 34
  • The job is already a Maven project. And if "release" was a parameter, I'd be able to just set it. However it isn't. It's some arbitrary action that gets added once a job is declared as Maven job. – Stefan S. Sep 09 '16 at 12:26
0

Btw one more valuable thing to mention is that u can pass custom parameters as a part of this http://jenkins/job/MyProject/m2release/submit?json={} request. In order to do that u have to define query parameter json -> for example

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

At the moment of execution it will be treated as parametrized job and u will have your parameter together with

MVN_RELEASE_VERSION=X 
MVN_DEV_VERSION=X-SNAPSHOT
MVN_ISDRYRUN=false
CUSTOM_PARAM_1=CUSTOM_PARAM_1_VALUE
Dzmitry Hubin
  • 1,091
  • 12
  • 14