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   
   22   import org.apache.maven.artifact.Artifact;
   23   import org.apache.maven.model.Resource;
   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.Bundle;
   27   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
   28   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.StartLevel;
   29   import org.codehaus.plexus.archiver.ArchiverException;
   30   import org.codehaus.plexus.archiver.jar.JarArchiver;
   31   import org.codehaus.plexus.util.DirectoryScanner;
   32   
   33   /**
   34    * Create and attach a JAR file containing the resolved artifacts from the
   35    * bundle list.
   36    *
   37    * @goal create-bundle-jar
   38    * @requiresDependencyResolution test
   39    * @phase package
   40    *
   41    */
   42   public class CreateBundleJarMojo extends AbstractLaunchpadFrameworkMojo {
   43   
   44   	/**
   45   	 * The list of resources we want to add to the bundle JAR file.
   46   	 *
   47   	 * @parameter
   48   	 */
   49   	private Resource[] resources;
   50   
   51   	/**
   52   	 * The output directory.
   53   	 *
   54   	 * @parameter default-value="${project.build.directory}"
   55   	 */
   56   	private File outputDirectory;
   57   
   58   	/**
   59   	 * Name of the generated JAR.
   60   	 *
   61   	 * @parameter default-value="${project.artifactId}-${project.version}"
   62   	 * @required
   63   	 */
   64   	private String jarName;
   65   
   66   	/**
   67   	 * The Jar archiver.
   68   	 *
   69   	 * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
   70   	 */
   71   	private JarArchiver jarArchiver;
   72   
   73   	private static final String CLASSIFIER = "bundles";
   74   
   75   	public static final String[] DEFAULT_INCLUDES = { "**/**" };
   76   
   77   	private void addBundles() throws MojoExecutionException {
   78   		BundleList bundles = getBundleList();
   79   
   80   		for (StartLevel level : bundles.getStartLevels()) {
   81   			for (Bundle bundle : level.getBundles()) {
   82   				Artifact artifact = getArtifact(new ArtifactDefinition(bundle,
   83   						level.getLevel()));
   84   				String destFileName = baseDestination + "/" + bundlesDirectory
   85   						+ "/" + level.getLevel() + "/"
   86   						+ artifact.getFile().getName();
   87   				try {
   88   					jarArchiver.addFile(artifact.getFile(), destFileName);
   89   				} catch (ArchiverException e) {
   90   					throw new MojoExecutionException(
   91   							"Unable to add file to bundle jar file: "
   92   									+ artifact.getFile().getAbsolutePath(), e);
   93   				}
   94   			}
   95   		}
   96   	}
   97   
   98   	private void addResources(Resource resource) throws MojoExecutionException {
   99   		getLog().info(
  100   				String.format("Adding resources [%s] to [%s]", resource
  101   						.getDirectory(), resource.getTargetPath()));
  102   		String[] fileNames = getFilesToCopy(resource);
  103   		for (int i = 0; i < fileNames.length; i++) {
  104   			String targetFileName = fileNames[i];
  105   			if (resource.getTargetPath() != null) {
  106   				targetFileName = resource.getTargetPath() + File.separator
  107   						+ targetFileName;
  108   			}
  109   
  110   			try {
  111   				jarArchiver.addFile(new File(resource.getDirectory(),
  112   						fileNames[i]), targetFileName);
  113   			} catch (ArchiverException e) {
  114   				throw new MojoExecutionException(
  115   						"Unable to add resources to JAR file", e);
  116   			}
  117   
  118   		}
  119   	}
  120   
  121   	private File createJARFile() throws MojoExecutionException {
  122   		File jarFile = new File(outputDirectory, jarName + "-" + CLASSIFIER
  123   				+ "." + JAR);
  124   		jarArchiver.setDestFile(jarFile);
  125   
  126   		addBundles();
  127   		addResources();
  128   
  129   		try {
  130   			jarArchiver.createArchive();
  131   		} catch (ArchiverException e) {
  132   			throw new MojoExecutionException(
  133   					"Unable to create bundle jar file", e);
  134   		} catch (IOException e) {
  135   			throw new MojoExecutionException(
  136   					"Unable to create bundle jar file", e);
  137   		}
  138   
  139   		return jarFile;
  140   	}
  141   
  142   	private void addResources() throws MojoExecutionException {
  143   		if (resources != null) {
  144   			for (Resource resource : resources) {
  145   				if (!(new File(resource.getDirectory())).isAbsolute()) {
  146   					resource.setDirectory(project.getBasedir() + File.separator
  147   							+ resource.getDirectory());
  148   				}
  149   				addResources(resource);
  150   			}
  151   		}
  152   	}
  153   
  154   	@Override
  155   	protected void executeWithArtifacts() throws MojoExecutionException,
  156   			MojoFailureException {
  157   		File jarFile = createJARFile();
  158   		projectHelper.attachArtifact(project, JAR, CLASSIFIER, jarFile);
  159   	}
  160   
  161   	/**
  162   	 * Returns a list of filenames that should be copied over to the destination
  163   	 * directory.
  164   	 *
  165   	 * @param resource
  166   	 *            the resource to be scanned
  167   	 * @return the array of filenames, relative to the sourceDir
  168   	 */
  169   	@SuppressWarnings("unchecked")
  170       private static String[] getFilesToCopy(Resource resource) {
  171   		DirectoryScanner scanner = new DirectoryScanner();
  172   		scanner.setBasedir(resource.getDirectory());
  173   		if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
  174   			scanner.setIncludes((String[]) resource.getIncludes().toArray(
  175   					new String[resource.getIncludes().size()]));
  176   		} else {
  177   			scanner.setIncludes(DEFAULT_INCLUDES);
  178   		}
  179   		if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) {
  180   			scanner.setExcludes((String[]) resource.getExcludes().toArray(
  181   					new String[resource.getExcludes().size()]));
  182   		}
  183   
  184   		scanner.addDefaultExcludes();
  185   
  186   		scanner.scan();
  187   
  188   		return scanner.getIncludedFiles();
  189   	}
  190   }

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