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.IOException;
   21   import java.util.Properties;
   22   
   23   import org.apache.maven.artifact.Artifact;
   24   import org.apache.maven.plugin.MojoExecutionException;
   25   import org.apache.maven.plugin.MojoFailureException;
   26   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
   27   import org.codehaus.plexus.archiver.ArchiverException;
   28   import org.codehaus.plexus.archiver.UnArchiver;
   29   import org.codehaus.plexus.archiver.manager.ArchiverManager;
   30   import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
   31   import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
   32   import org.codehaus.plexus.util.FileUtils;
   33   import org.codehaus.plexus.util.StringUtils;
   34   
   35   /**
   36    * Initialize a Sling application project by extracting bundles into the correct
   37    * locations.
   38    *
   39    * @goal prepare-package
   40    * @requiresDependencyResolution test
   41    * @phase process-sources
   42    * @description initialize a Sling application project
   43    */
   44   public class PreparePackageMojo extends AbstractLaunchpadFrameworkMojo {
   45   
   46   	/**
   47   	 * The output directory for the default bundles in a WAR-packaged project,
   48   	 * the base JAR (in the subdirectory named in the baseDestination
   49   	 * parameter), and any additional bundles.
   50   	 *
   51   	 * @parameter default-value="${project.build.directory}/launchpad-bundles"
   52   	 */
   53   	private File warOutputDirectory;
   54   
   55   	/**
   56   	 * The project's packaging type.
   57   	 *
   58   	 * @parameter expression="${project.packaging}"
   59   	 */
   60   	private String packaging;
   61   
   62   	/**
   63   	 * The definition of the base JAR.
   64   	 *
   65   	 * @parameter
   66   	 */
   67   	private ArtifactDefinition base;
   68   
   69   	/**
   70   	 * The definition of the package to be included to provide web support for
   71   	 * JAR-packaged projects (i.e. pax-web).
   72   	 *
   73   	 * @parameter
   74   	 */
   75   	private ArtifactDefinition jarWebSupport;
   76   
   77   	/**
   78   	 * The project's build output directory (i.e. target/classes).
   79   	 *
   80   	 * @parameter expression="${project.build.outputDirectory}"
   81   	 * @readonly
   82   	 */
   83   	private File buildOutputDirectory;
   84   
   85       /**
   86        * To look up Archiver/UnArchiver implementations
   87        *
   88        * @component
   89        */
   90       private ArchiverManager archiverManager;
   91   
   92   	public void executeWithArtifacts() throws MojoExecutionException, MojoFailureException {
   93   		copyBaseArtifact();
   94   		copyBundles(getBundleList(), getOutputDirectory());
   95   		if (JAR.equals(packaging)) {
   96   			unpackBaseArtifact();
   97   		}
   98   	}
   99   
  100   	protected void initArtifactDefinitions(Properties dependencies) {
  101   		if (base == null) {
  102   			base = new ArtifactDefinition();
  103   		}
  104   		base.initDefaults(dependencies.getProperty("base"));
  105   
  106   		if (jarWebSupport == null) {
  107   			jarWebSupport = new ArtifactDefinition();
  108   		}
  109   		jarWebSupport.initDefaults(dependencies.getProperty("jarWebSupport"));
  110   	}
  111   
  112   	/**
  113        * Add the JAR Web Support bundle to the bundle list.
  114        */
  115       @Override
  116       protected void initBundleList(BundleList bundleList) {
  117           if (packaging.equals(JAR)) {
  118               bundleList.add(jarWebSupport.toBundle());
  119           }
  120       }
  121   
  122   	private void copyBaseArtifact() throws MojoExecutionException {
  123   		Artifact artifact = getBaseArtifact();
  124   		if (artifact == null) {
  125   			throw new MojoExecutionException(
  126   					String
  127   							.format(
  128   									"Project doesn't have a base dependency of groupId %s and artifactId %s",
  129   									base.getGroupId(), base.getArtifactId()));
  130   		}
  131   		File destinationDir = new File(getOutputDirectory(), baseDestination);
  132   		File destinationFile = new File(destinationDir, artifact
  133   				.getArtifactId()
  134   				+ "." + artifact.getArtifactHandler().getExtension());
  135   		if (shouldCopy(artifact.getFile(), destinationFile)) {
  136   			try {
  137   				getLog().info(
  138   						String.format("Copying base artifact from %s to %s.",
  139   								artifact.getFile(), destinationFile));
  140   				FileUtils.copyFile(artifact.getFile(), destinationFile);
  141   			} catch (IOException e) {
  142   				throw new MojoExecutionException(
  143   						"Unable to copy base artifact.", e);
  144   			}
  145   		} else {
  146   			getLog().debug(
  147   					String.format("Skipping copy of base artifact from %s.",
  148   							artifact.getFile()));
  149   		}
  150   	}
  151   
  152   	private Artifact getBaseArtifact() throws MojoExecutionException {
  153   		Artifact baseDependency = getBaseDependency();
  154   		if (baseDependency == null) {
  155   			return null;
  156   		}
  157   
  158   		return getArtifact(base.getGroupId(), base.getArtifactId(),
  159   				baseDependency.getVersion(), base.getType(), base
  160   						.getClassifier());
  161   
  162   	}
  163   
  164   	private Artifact getBaseDependency() {
  165   		return (Artifact) project.getArtifactMap().get(
  166   				base.getGroupId() + ":" + base.getArtifactId());
  167   	}
  168   
  169   	protected File getOutputDirectory() {
  170   		if (WAR.equals(packaging)) {
  171   			return warOutputDirectory;
  172   		} else {
  173   			return buildOutputDirectory;
  174   		}
  175   	}
  176   
  177   	protected void unpackBaseArtifact() throws MojoExecutionException {
  178   		Artifact artifact = getBaseDependency();
  179   		if (artifact == null) {
  180   			throw new MojoExecutionException(
  181   					String
  182   							.format(
  183   									"Project doesn't have a base dependency of groupId %s and artifactId %s",
  184   									base.getGroupId(), base.getArtifactId()));
  185   		}
  186   		unpack(artifact.getFile(), buildOutputDirectory, null, null);
  187   	}
  188   
  189       private void unpack(File source, File destination, String includes, String excludes)
  190               throws MojoExecutionException {
  191           getLog().info("Unpacking " + source.getPath() + " to\n  " + destination.getPath());
  192           try {
  193               destination.mkdirs();
  194   
  195               UnArchiver unArchiver = archiverManager.getUnArchiver(source);
  196   
  197               unArchiver.setSourceFile(source);
  198               unArchiver.setDestDirectory(destination);
  199   
  200               if (StringUtils.isNotEmpty(excludes) || StringUtils.isNotEmpty(includes)) {
  201                   IncludeExcludeFileSelector[] selectors = new IncludeExcludeFileSelector[] { new IncludeExcludeFileSelector() };
  202   
  203                   if (StringUtils.isNotEmpty(excludes)) {
  204                       selectors[0].setExcludes(excludes.split(","));
  205                   }
  206   
  207                   if (StringUtils.isNotEmpty(includes)) {
  208                       selectors[0].setIncludes(includes.split(","));
  209                   }
  210   
  211                   unArchiver.setFileSelectors(selectors);
  212               }
  213   
  214               unArchiver.extract();
  215           } catch (NoSuchArchiverException e) {
  216               throw new MojoExecutionException("Unable to find archiver for " + source.getPath(), e);
  217           } catch (ArchiverException e) {
  218               throw new MojoExecutionException("Unable to unpack " + source.getPath(), e);
  219           }
  220       }
  221   }

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