GetLatestVersionMojo.java

  1. /*
  2.  * Copyright 2013 1&1.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package net.oneandone.maven.plugins.attachqars;

  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.net.HttpURLConnection;
  20. import java.net.MalformedURLException;
  21. import java.net.URI;
  22. import java.net.URL;
  23. import org.apache.maven.plugin.AbstractMojo;
  24. import org.apache.maven.plugin.MojoExecutionException;
  25. import org.apache.maven.plugin.MojoFailureException;
  26. import org.apache.maven.plugins.annotations.LifecyclePhase;
  27. import org.apache.maven.plugins.annotations.Mojo;
  28. import org.apache.maven.plugins.annotations.Parameter;
  29. import org.apache.maven.project.MavenProject;
  30. import org.codehaus.plexus.util.IOUtil;

  31. /**
  32.  * Retrieves the latest version of the project from Artifactory and put it into a property
  33.  * for later usage in other plugins.
  34.  *
  35.  * <p>Similar to the
  36.  * <a href="http://mojo.codehaus.org/buildnumber-maven-plugin/create-mojo.html">
  37.  * <tt>buildnumber:create</tt></a> Mojo.
  38.  * Invokes something like
  39.  * <tt>http://host/artifactory/api/search/latestVersion?reposname=repo1&amp;g=commons-logging&amp;a=commons-logging&amp;remote=1</tt>.
  40.  * For more information take a look at the documention of the
  41.  * <a href="http://wiki.jfrog.org/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-ArtifactLatestVersionSearch">
  42.  * REST-API</a> of Artifactory.</p>
  43.  *
  44.  * @author Mirko Friedenhagen
  45.  */
  46. @Mojo(name = "latest-version", aggregator = false, defaultPhase = LifecyclePhase.INSTALL)
  47. public class GetLatestVersionMojo extends AbstractMojo {

  48.     /**
  49.      * The Maven project.
  50.      */
  51.     @Parameter(defaultValue = "${project}", readonly = true)
  52.     private final MavenProject project;

  53.     /**
  54.      * URI pointing to Artifactory, e.g. http://localhost:8081/artifactory/.
  55.      */
  56.     @Parameter(property = "attach-qar.artifactory-uri", required = true)
  57.     private final URI artifactoryUri;

  58.     /**
  59.      * Name of the property holding the latest version from the repository.
  60.      */
  61.     @Parameter(defaultValue = "latestVersionFromRepository", property = "attach-qar.latest-version-property-name",
  62.             required = true)
  63.     private final String latestVersionPropertyName;

  64.     /**
  65.      * Name of the repository for resolution of the latest version.
  66.      */
  67.     @Parameter(defaultValue = "libs-release-local", property = "attach-qar.repos-name", required = true)
  68.     private final String reposName;
  69.     /**
  70.      * Constructor for tests.
  71.      *
  72.      * @param project to inspect.
  73.      * @param artifactoryUri see {@link GetLatestVersionMojo#artifactoryUri}.
  74.      * @param reposName see {@link GetLatestVersionMojo#reposName}.
  75.      */
  76.     GetLatestVersionMojo(MavenProject project, URI artifactoryUri, String reposName) {
  77.         this.project = project;
  78.         this.artifactoryUri = artifactoryUri;
  79.         this.reposName = reposName;
  80.         this.latestVersionPropertyName = "latestVersionFromRepository";
  81.     }

  82.     @Override
  83.     public void execute() throws MojoExecutionException, MojoFailureException {
  84.         final String groupId = project.getGroupId();
  85.         final String artifactId = project.getArtifactId();
  86.         final URI searchUri = createUri(groupId, artifactId);
  87.         try {
  88.             final URL searchUrl = searchUri.toURL();
  89.             final HttpURLConnection openConnection = openConnection(searchUrl);
  90.             final InputStream in = openConnection.getInputStream();
  91.             try {
  92.                 project.getProperties().setProperty(latestVersionPropertyName, IOUtil.toString(in));
  93.             } finally {
  94.                 in.close();
  95.             }
  96.         } catch (MalformedURLException ex) {
  97.             throw new MojoFailureException("Could not convert " + searchUri + " to URL", ex);
  98.         } catch (IOException ex) {
  99.             throw new MojoExecutionException("Could not fetch latestVersion: " + searchUri, ex);
  100.         }
  101.     }

  102.     /**
  103.      * Creates the search URI.
  104.      *
  105.      * @param groupId of the project.
  106.      * @param artifactId of the project.
  107.      * @return complete search URI.
  108.      */
  109.     URI createUri(final String groupId, final String artifactId) {
  110.         final String searchPart =
  111.                 "api/search/latestVersion?repos=" + reposName + "&g=" + groupId + "&a=" + artifactId + "&remote=1";
  112.         if (artifactoryUri.getPath().endsWith("/")) {
  113.             return artifactoryUri.resolve(searchPart);
  114.         } else {
  115.             return URI.create(artifactoryUri.toString() + "/").resolve(searchPart);
  116.         }
  117.     }
  118.     /**
  119.      * Open a connection, used for tests.
  120.      * @param searchUrl to connect to.
  121.      * @return connection.
  122.      * @throws IOException when the connection could not be opened.
  123.      */
  124.     HttpURLConnection openConnection(final URL searchUrl) throws IOException {
  125.         return (HttpURLConnection) searchUrl.openConnection();
  126.     }
  127. }