Dienstag, 16. Februar 2016

Jenkins: Select Maven Project version depending on the previously selected artifact

If you want to build a property selector in Jenkins, which allows you to select an artifact and a depending artifact version, you can’t do that with default Jenkins property selectors. The following tutorial makes use of the “Active Choice” plugin, which allows a dynamic generation of selection lists and access to the other parameter fields in Jenkins. 

First we build an artifact selector to select the artifact. We do this with an “Active Choice Parameter” and a static list. Later we can replace the static list dynamically with requested artifacts from maven repository. Use the following groovy script to build the static list, and set property Name to “artifactId”:

Now we can add an “Active Choice Reactive Parameter” selector. Referenced Parameter must beset to “artifactId” to get access to the first defined property field. The following groovy script reads all available versions of artifact from maven repo and creates a selectable list.

You can copy the groovy script from here:

import java.util.regex.Matcher
import java.util.regex.Pattern
import javax.xml.xpath.*

// set target url to artifact versions in you maven repo
URL targetUrl = new URL("https://mvn.havre.de/repo/de/havre/" + artifactId + "/");

// resolve HTML version list
HttpURLConnection con = (HttpURLConnection) targetUrl.openConnection();
con.setRequestProperty("Authorization", "Basic ***********************");
int responseCode = con.getResponseCode();

// pars HTML to a simple list
if (responseCode == HttpURLConnection.HTTP_OK) {
               BufferedReader inStream = new BufferedReader(new InputStreamReader(con.getInputStream()));
               String inputLine;
               StringBuffer response = new StringBuffer();
               while ((inputLine = inStream.readLine()) != null) {

               Pattern TAG_REGEX = Pattern.compile("<text>([0-9]*\\.[0-9]*\\.[0-9]*)</text>");
               List tagValues = new ArrayList();
               final Matcher matcher = TAG_REGEX.matcher(response.toString());
               while (matcher.find()) {
        return tagValues;
} else {
               return['GET ' + targetUrl + ' not worked: ' + responseCode ];

 That’s it. Now you can use the "artifactId" and "artifactVersion" property for your build script. E.g. to deploy the concert artifact to an application server.

Keine Kommentare:

Kommentar veröffentlichen