Trigger Maven Release Remotely

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?


好的,旧方法中缺少的参数是:

writer.write("specifyScmCredentials=on&");
writer.write("specifyScmCommentPrefix=on&");

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

  • Maven Release Plugin
  • Building a Maven Project in Jenkins
  • Providing Parameters to Jenkins Builds
  • 链接地址: http://www.djcxy.com/p/93202.html

    上一篇: 如何可视化张量流卷积滤波器?

    下一篇: 触发Maven远程释放