Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger Maven Release Remotely

Tags:

jenkins

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?

like image 866
Steffi S. Avatar asked Sep 09 '16 11:09

Steffi S.


People also ask

What is release plugin?

This plugin adds the ability to wrap your job with pre- and post- build steps which are only executed when a manual release build is triggered.

Why Maven plugins are used?

"Maven" is really just a core framework for a collection of Maven Plugins. In other words, plugins are where much of the real action is performed, plugins are used to: create jar files, create war files, compile code, unit test code, create project documentation, and on and on.


1 Answers

Okay, the parameters missing in my old approach were:

writer.write("specifyScmCredentials=on&");
writer.write("specifyScmCommentPrefix=on&");
like image 114
Steffi S. Avatar answered Oct 01 '22 20:10

Steffi S.