001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.maven;
017:
018: // J2SE dependencies
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.Iterator;
022: import java.util.Set;
023:
024: // Maven and Plexus dependencies
025: import org.apache.maven.plugin.AbstractMojo;
026: import org.apache.maven.plugin.MojoExecutionException;
027: import org.apache.maven.project.MavenProject;
028: import org.apache.maven.artifact.Artifact;
029: import org.codehaus.plexus.util.FileUtils;
030:
031: // Note: javadoc in class and fields descriptions must be XHTML.
032: /**
033: * Copies <code>.jar</code> files in a single directory. Dependencies are copied as well,
034: * except if already presents.
035: *
036: * @goal collect
037: * @phase package
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/build/maven/jar-collector/src/main/java/org/geotools/maven/JarCollector.java $
039: * @version $Id: JarCollector.java 25335 2007-04-25 13:46:57Z desruisseaux $
040: * @author Martin Desruisseaux
041: */
042: public class JarCollector extends AbstractMojo {
043: /**
044: * The sub directory to create inside the "target" directory.
045: */
046: private static final String SUB_DIRECTORY = "binaries";
047:
048: /**
049: * The directory where JARs are to be copied. It should
050: * be the "target" directory of the parent {@code pom.xml}.
051: */
052: private String collectDirectory;
053:
054: /**
055: * Directory containing the generated JAR.
056: *
057: * @parameter expression="${project.build.directory}"
058: * @required
059: */
060: private String outputDirectory;
061:
062: /**
063: * Name of the generated JAR.
064: *
065: * @parameter expression="${project.build.finalName}"
066: * @required
067: */
068: private String jarName;
069:
070: /**
071: * Project dependencies.
072: *
073: * @parameter expression="${project.artifacts}"
074: * @required
075: */
076: private Set/*<Artifact>*/dependencies;
077:
078: /**
079: * The Maven project running this plugin.
080: *
081: * @parameter expression="${project}"
082: * @required
083: */
084: private MavenProject project;
085:
086: /**
087: * Copies the {@code .jar} files to the collect directory.
088: *
089: * @throws MojoExecutionException if the plugin execution failed.
090: */
091: public void execute() throws MojoExecutionException {
092: /*
093: * Gets the parent "target" directory.
094: */
095: MavenProject parent = project;
096: while (parent.hasParent()) {
097: parent = parent.getParent();
098: }
099: collectDirectory = parent.getBuild().getDirectory();
100: /*
101: * Now collects the JARs.
102: */
103: try {
104: collect();
105: } catch (IOException e) {
106: throw new MojoExecutionException(
107: "Error collecting the JAR file.", e);
108: }
109: }
110:
111: /**
112: * Implementation of the {@link #execute} method.
113: */
114: private void collect() throws MojoExecutionException, IOException {
115: /*
116: * Make sure that we are collecting the JAR file from a module which produced
117: * such file. Some modules use pom packaging, which do not produce any JAR file.
118: */
119: final File jarFile = new File(outputDirectory, jarName + ".jar");
120: if (!jarFile.isFile()) {
121: return;
122: }
123: /*
124: * Get the "target" directory of the parent pom.xml and make sure it exists.
125: */
126: File collect = new File(collectDirectory);
127: if (!collect.exists()) {
128: if (!collect.mkdir()) {
129: throw new MojoExecutionException(
130: "Failed to create target directory.");
131: }
132: }
133: if (collect.getCanonicalFile().equals(
134: jarFile.getParentFile().getCanonicalFile())) {
135: /*
136: * The parent's directory is the same one than this module's directory.
137: * In other words, this plugin is not executed from the parent POM. Do
138: * not copy anything, since this is not the place where we want to
139: * collect the JAR files.
140: */
141: return;
142: }
143: /*
144: * Creates a "binaries" subdirectory inside the "target" directory.
145: */
146: collect = new File(collect, SUB_DIRECTORY);
147: if (!collect.exists()) {
148: if (!collect.mkdir()) {
149: throw new MojoExecutionException(
150: "Failed to create binaries directory.");
151: }
152: }
153: int count = 1;
154: FileUtils.copyFileToDirectory(jarFile, collect);
155: if (dependencies != null) {
156: for (final Iterator it = dependencies.iterator(); it
157: .hasNext();) {
158: final Artifact artifact = (Artifact) it.next();
159: final String scope = artifact.getScope();
160: if (scope != null && // Maven 2.0.6 bug?
161: (scope.equalsIgnoreCase(Artifact.SCOPE_COMPILE) || scope
162: .equalsIgnoreCase(Artifact.SCOPE_RUNTIME))) {
163: final File file = artifact.getFile();
164: final File copy = new File(collect, file.getName());
165: if (!copy.exists()) {
166: /*
167: * Copies the dependency only if it was not already copied. Note that
168: * the module's JAR was copied inconditionnaly above (because it may
169: * be the result of a new compilation). If a Geotools JAR from the
170: * dependencies list changed, it will be copied inconditionnaly when
171: * the module for this JAR will be processed by Maven.
172: */
173: FileUtils.copyFileToDirectory(file, collect);
174: count++;
175: }
176: }
177: }
178: }
179: getLog().info("Copied " + count + " JAR to parent directory.");
180: }
181: }
|