Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.sling.maven.projectsupport;
   18   
   19   import java.io.File;
   20   import java.io.FileInputStream;
   21   import java.io.IOException;
   22   import java.util.List;
   23   import java.util.Properties;
   24   
   25   import org.apache.maven.artifact.Artifact;
   26   import org.apache.maven.artifact.factory.ArtifactFactory;
   27   import org.apache.maven.artifact.repository.ArtifactRepository;
   28   import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
   29   import org.apache.maven.artifact.resolver.ArtifactResolutionException;
   30   import org.apache.maven.artifact.resolver.ArtifactResolver;
   31   import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
   32   import org.apache.maven.artifact.versioning.VersionRange;
   33   import org.apache.maven.plugin.AbstractMojo;
   34   import org.apache.maven.plugin.MojoExecutionException;
   35   import org.apache.maven.plugin.MojoFailureException;
   36   import org.apache.maven.project.MavenProject;
   37   import org.apache.maven.project.MavenProjectHelper;
   38   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
   39   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.io.xpp3.BundleListXpp3Reader;
   40   import org.codehaus.plexus.util.StringUtils;
   41   import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
   42   
   43   public abstract class AbstractBundleListMojo extends AbstractMojo {
   44   
   45       /**
   46        * JAR Packaging type.
   47        */
   48       protected static final String JAR = "jar";
   49   
   50       /**
   51        * WAR Packaging type.
   52        */
   53       protected static final String WAR = "war";
   54   
   55       protected static boolean shouldCopy(File source, File dest) {
   56           if (!dest.exists()) {
   57               return true;
   58           } else {
   59               return source.lastModified() > dest.lastModified();
   60           }
   61       }
   62   
   63       /**
   64        * @parameter default-value="${basedir}/src/main/bundles/list.xml"
   65        */
   66       protected File bundleListFile;
   67   
   68       /**
   69        * The definition of the defaultBundleList artifact.
   70        *
   71        * @parameter
   72        */
   73       protected ArtifactDefinition defaultBundleList;
   74   
   75       /**
   76        * The Maven project.
   77        *
   78        * @parameter expression="${project}"
   79        * @required
   80        * @readonly
   81        */
   82       protected MavenProject project;
   83   
   84       /**
   85        * @component
   86        */
   87       protected MavenProjectHelper projectHelper;
   88   
   89       /**
   90        * Any additional bundles to include in the project's bundles directory.
   91        *
   92        * @parameter
   93        */
   94       private ArtifactDefinition[] additionalBundles;
   95   
   96       private BundleList bundleList;
   97   
   98       /**
   99        * Bundles which should be removed from the project's bundles directory.
  100        *
  101        * @parameter
  102        */
  103       private ArtifactDefinition[] bundleExclusions;
  104   
  105       /**
  106        * Used to look up Artifacts in the remote repository.
  107        *
  108        * @component
  109        */
  110       private ArtifactFactory factory;
  111   
  112       /**
  113        * If true, include the default bundles.
  114        *
  115        * @parameter default-value="true"
  116        */
  117       private boolean includeDefaultBundles;
  118   
  119       /**
  120        * Location of the local repository.
  121        *
  122        * @parameter expression="${localRepository}"
  123        * @readonly
  124        * @required
  125        */
  126       private ArtifactRepository local;
  127   
  128       /**
  129        * List of Remote Repositories used by the resolver.
  130        *
  131        * @parameter expression="${project.remoteArtifactRepositories}"
  132        * @readonly
  133        * @required
  134        */
  135       private List remoteRepos;
  136   
  137       /**
  138        * Used to look up Artifacts in the remote repository.
  139        *
  140        * @component
  141        */
  142       private ArtifactResolver resolver;
  143   
  144       public final void execute() throws MojoFailureException, MojoExecutionException {
  145           try {
  146               initBundleList();
  147           } catch (Exception e) {
  148               throw new MojoExecutionException("Unable to load dependency information from properties file.", e);
  149           }
  150           executeWithArtifacts();
  151   
  152       }
  153   
  154       /**
  155        * Execute the logic of the plugin after the default artifacts have been
  156        * initialized.
  157        */
  158       protected abstract void executeWithArtifacts() throws MojoExecutionException, MojoFailureException;
  159   
  160       /**
  161        * Get a resolved Artifact from the coordinates found in the artifact
  162        * definition.
  163        *
  164        * @param def the artifact definition
  165        * @return the artifact, which has been resolved
  166        * @throws MojoExecutionException
  167        */
  168       protected Artifact getArtifact(ArtifactDefinition def) throws MojoExecutionException {
  169           return getArtifact(def.getGroupId(), def.getArtifactId(), def.getVersion(), def.getType(), def.getClassifier());
  170       }
  171   
  172       /**
  173        * Get a resolved Artifact from the coordinates provided
  174        *
  175        * @return the artifact, which has been resolved.
  176        * @throws MojoExecutionException
  177        */
  178       protected Artifact getArtifact(String groupId, String artifactId, String version, String type, String classifier)
  179               throws MojoExecutionException {
  180           Artifact artifact;
  181           VersionRange vr;
  182   
  183           try {
  184               vr = VersionRange.createFromVersionSpec(version);
  185           } catch (InvalidVersionSpecificationException e) {
  186               vr = VersionRange.createFromVersion(version);
  187           }
  188   
  189           if (StringUtils.isEmpty(classifier)) {
  190               artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, null, Artifact.SCOPE_COMPILE);
  191           } else {
  192               artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, classifier,
  193                       Artifact.SCOPE_COMPILE);
  194           }
  195           try {
  196               resolver.resolve(artifact, remoteRepos, local);
  197           } catch (ArtifactResolutionException e) {
  198               throw new MojoExecutionException("Unable to resolve artifact.", e);
  199           } catch (ArtifactNotFoundException e) {
  200               throw new MojoExecutionException("Unable to find artifact.", e);
  201           }
  202           return artifact;
  203       }
  204   
  205       protected BundleList getBundleList() {
  206           return bundleList;
  207       }
  208   
  209       /**
  210        * Hook methods for subclasses to initialize any additional artifact
  211        * definitions.
  212        *
  213        * @param dependencies the dependency properties loaded from the JAR file
  214        */
  215       protected void initArtifactDefinitions(Properties dependencies) {
  216       }
  217   
  218       /**
  219        * Hook methods for subclasses to initialize the bundle list.
  220        */
  221       protected void initBundleList(BundleList bundleList) {
  222       }
  223   
  224       protected boolean isCurrentArtifact(ArtifactDefinition def) {
  225           return (def.getGroupId().equals(project.getGroupId()) && def.getArtifactId().equals(project.getArtifactId()));
  226       }
  227   
  228       /**
  229        * Initialize the artifact definitions using defaults inside the plugin JAR.
  230        *
  231        * @throws IOException if the default properties can't be read
  232        * @throws XmlPullParserException
  233        * @throws MojoExecutionException
  234        */
  235       private final void initArtifactDefinitions() throws IOException {
  236           Properties dependencies = new Properties();
  237           dependencies.load(getClass().getResourceAsStream(
  238                   "/org/apache/sling/maven/projectsupport/dependencies.properties"));
  239   
  240           if (defaultBundleList == null) {
  241               defaultBundleList = new ArtifactDefinition();
  242           }
  243           defaultBundleList.initDefaults(dependencies.getProperty("defaultBundleList"));
  244   
  245           initArtifactDefinitions(dependencies);
  246       }
  247   
  248       private final void initBundleList() throws IOException, XmlPullParserException, MojoExecutionException {
  249           initArtifactDefinitions();
  250           if (isCurrentArtifact(defaultBundleList)) {
  251               bundleList = readBundleList(bundleListFile);
  252           } else {
  253               bundleList = new BundleList();
  254               if (includeDefaultBundles) {
  255                   Artifact artifact = getArtifact(defaultBundleList.getGroupId(), defaultBundleList.getArtifactId(),
  256                           defaultBundleList.getVersion(), defaultBundleList.getType(), defaultBundleList.getClassifier());
  257                   getLog().info("Using bundle list file from " + artifact.getFile().getAbsolutePath());
  258                   bundleList = readBundleList(artifact.getFile());
  259               }
  260   
  261               if (bundleListFile.exists()) {
  262                   bundleList.merge(readBundleList(bundleListFile));
  263               }
  264           }
  265           if (additionalBundles != null) {
  266               for (ArtifactDefinition def : additionalBundles) {
  267                   bundleList.add(def.toBundle());
  268               }
  269           }
  270           if (bundleExclusions != null) {
  271               for (ArtifactDefinition def : bundleExclusions) {
  272                   bundleList.remove(def.toBundle(), false);
  273               }
  274           }
  275           initBundleList(bundleList);
  276       }
  277   
  278       private BundleList readBundleList(File file) throws IOException, XmlPullParserException {
  279           BundleListXpp3Reader reader = new BundleListXpp3Reader();
  280           FileInputStream fis = new FileInputStream(file);
  281           try {
  282               return reader.read(fis);
  283           } finally {
  284               fis.close();
  285           }
  286       }
  287   
  288   }

Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]