You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2011/09/15 15:25:58 UTC

svn commit: r1171090 - /sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java

Author: justin
Date: Thu Sep 15 13:25:58 2011
New Revision: 1171090

URL: http://svn.apache.org/viewvc?rev=1171090&view=rev
Log:
SLING-2098 - allow the use of an artifact coordinates to be specified instead of a file path

Modified:
    sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java

Modified: sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java
URL: http://svn.apache.org/viewvc/sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java?rev=1171090&r1=1171089&r2=1171090&view=diff
==============================================================================
--- sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java (original)
+++ sling/trunk/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/BundleInstallFileMojo.java Thu Sep 15 13:25:58 2011
@@ -17,7 +17,21 @@
 
 package org.apache.sling.maven.bundlesupport;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Install an OSGi bundle to a running Sling instance.
@@ -34,13 +48,143 @@ public class BundleInstallFileMojo exten
      * @parameter expression="${sling.file}"
      */
     private String bundleFileName;
+
+    /**
+     * The groupId of the artifact to install
+     * @parameter expression="${sling.groupId}"
+     */
+    private String groupId;
+
+    /**
+     * The artifactId of the artifact to install
+     * @parameter expression="${sling.artifactId}"
+     */
+    private String artifactId;
+
+    /**
+     * The version of the artifact to install
+     * @parameter expression="${sling.version}"
+     */
+    private String version;
+
+    /**
+     * The packaging of the artifact to install
+     * @parameter expression="${sling.packaging}" default-value="jar"
+     */
+    private String packaging = "jar";
+
+    /**
+     * A string of the form groupId:artifactId:version[:packaging].
+     * @parameter expression="${sling.artifact}"
+     */
+    private String artifact;
+
+    /**
+     *
+     * @parameter expression="${project.remoteArtifactRepositories}"
+     * @required
+     * @readonly
+     */
+    private List pomRemoteRepositories;
+
+    /**
+     * The id of the repository from which we'll download the artifact
+     * @parameter expression="${sling.repoId}" default-value="temp"
+     */
+    private String repositoryId = "temp";
+
+    /**
+     * The url of the repository from which we'll download the artifact
+     * @parameter expression="${sling.repoUrl}"
+     */
+    private String repositoryUrl;
+
+    /**
+     * @component
+     * @readonly
+     */
+    private ArtifactFactory artifactFactory;
+
+    /**
+     * @component
+     * @readonly
+     */
+    private ArtifactResolver artifactResolver;
+
+    /**
+     * @component
+     * @readonly
+     */
+    private ArtifactRepositoryFactory artifactRepositoryFactory;
+
+    /**
+     * @component roleHint="default"
+     */
+    private ArtifactRepositoryLayout repositoryLayout;
+
+    /**
+     *
+     * @parameter expression="${localRepository}"
+     * @readonly
+     */
+    private ArtifactRepository localRepository;
     
     @Override
     protected String getBundleFileName() throws MojoExecutionException {
-        if (bundleFileName == null) {
-            throw new MojoExecutionException("Missing sling.file parameter");
+        String fileName = bundleFileName;
+        if (fileName == null) {
+            fileName = resolveBundleFileFromArtifact();
+
+            if (fileName == null) {
+                throw new MojoExecutionException("Must provide either sling.file or sling.artifact parameters");
+            }
         }
         
-        return bundleFileName;
+        return fileName;
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    private String resolveBundleFileFromArtifact() throws MojoExecutionException {
+        if (artifactId == null && artifact == null) {
+            return null;
+        }
+        if (artifactId == null) {
+            String[] tokens = StringUtils.split(artifact, ":");
+            if (tokens.length != 3 && tokens.length != 4) {
+                throw new MojoExecutionException("Invalid artifact, you must specify "
+                        + "groupId:artifactId:version[:packaging] " + artifact);
+            }
+            groupId = tokens[0];
+            artifactId = tokens[1];
+            version = tokens[2];
+            if (tokens.length == 4)
+                packaging = tokens[3];
+        }
+        Artifact packageArtifact = artifactFactory.createBuildArtifact(groupId, artifactId, version, packaging);
+
+        if (pomRemoteRepositories == null) {
+            pomRemoteRepositories = new ArrayList();
+        }
+
+        List repoList = new ArrayList(pomRemoteRepositories);
+
+        if (repositoryUrl != null) {
+            ArtifactRepositoryPolicy policy =
+                new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
+                                              ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN );
+            ArtifactRepository remoteRepo = artifactRepositoryFactory.createArtifactRepository(repositoryId, repositoryUrl,
+                    repositoryLayout, policy, policy);
+
+            repoList.add(remoteRepo);
+        }
+
+        try {
+            artifactResolver.resolve(packageArtifact, repoList, localRepository);
+            getLog().info("Resolved artifact to " + packageArtifact.getFile().getAbsolutePath());
+        } catch (AbstractArtifactResolutionException e) {
+            throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
+        }
+
+        return packageArtifact.getFile().getAbsolutePath();
     }
 }
\ No newline at end of file