View Javadoc
1   package org.apache.maven.plugin.jar;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.archiver.MavenArchiver;
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.project.MavenProjectHelper;
31  import org.codehaus.plexus.archiver.Archiver;
32  import org.codehaus.plexus.archiver.jar.JarArchiver;
33  
34  import java.io.File;
35  
36  /**
37   * Base class for creating a jar from project classes.
38   *
39   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
40   * @version $Id: AbstractJarMojo.java 1636726 2014-11-04 20:11:52Z khmarbaise $
41   */
42  public abstract class AbstractJarMojo
43      extends AbstractMojo
44  {
45  
46      private static final String[] DEFAULT_EXCLUDES = new String[]{ "**/package.html" };
47  
48      private static final String[] DEFAULT_INCLUDES = new String[]{ "**/**" };
49  
50      /**
51       * List of files to include. Specified as fileset patterns which are relative to the input directory whose contents
52       * is being packaged into the JAR.
53       */
54      @Parameter
55      private String[] includes;
56  
57      /**
58       * List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents
59       * is being packaged into the JAR.
60       */
61      @Parameter
62      private String[] excludes;
63  
64      /**
65       * Directory containing the generated JAR.
66       */
67      @Parameter( defaultValue = "${project.build.directory}", required = true )
68      private File outputDirectory;
69  
70      /**
71       * Name of the generated JAR.
72       */
73      @Parameter( alias = "jarName", property = "jar.finalName", defaultValue = "${project.build.finalName}" )
74      private String finalName;
75  
76      /**
77       * The Jar archiver.
78       */
79      @Component( role = Archiver.class, hint = "jar" )
80      private JarArchiver jarArchiver;
81  
82      /**
83       * The Maven project.
84       */
85      @Parameter( defaultValue = "${project}", readonly = true, required = true )
86      private MavenProject project;
87  
88      /**
89       *
90       */
91      @Parameter( defaultValue = "${session}", readonly = true, required = true )
92      private MavenSession session;
93  
94      /**
95       * The archive configuration to use.
96       * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
97       */
98      @Parameter
99      private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
100 
101     /**
102      * Path to the default MANIFEST file to use. It will be used if
103      * <code>useDefaultManifestFile</code> is set to <code>true</code>.
104      *
105      * @since 2.2
106      */
107     @Parameter( defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true,
108                 readonly = true )
109     private File defaultManifestFile;
110 
111     /**
112      * Set this to <code>true</code> to enable the use of the <code>defaultManifestFile</code>.
113      *
114      * @since 2.2
115      */
116     @Parameter( property = "jar.useDefaultManifestFile", defaultValue = "false" )
117     private boolean useDefaultManifestFile;
118 
119     /**
120      *
121      */
122     @Component
123     private MavenProjectHelper projectHelper;
124 
125     /**
126      * Require the jar plugin to build a new JAR even if none of the contents appear to have changed.
127      * By default, this plugin looks to see if the output jar exists and inputs have not changed.
128      * If these conditions are true, the plugin skips creation of the jar. This does not work when
129      * other plugins, like the maven-shade-plugin, are configured to post-process the jar.
130      * This plugin can not detect the post-processing, and so leaves the post-processed jar in place.
131      * This can lead to failures when those plugins do not expect to find their own output
132      * as an input. Set this parameter to <tt>true</tt> to avoid these problems by forcing
133      * this plugin to recreate the jar every time.
134      */
135     @Parameter( property = "jar.forceCreation", defaultValue = "false" )
136     private boolean forceCreation;
137 
138     /**
139      * Skip creating empty archives
140      */
141     @Parameter( property = "jar.skipIfEmpty", defaultValue = "false" )
142     private boolean skipIfEmpty;
143 
144     /**
145      * Return the specific output directory to serve as the root for the archive.
146      */
147     protected abstract File getClassesDirectory();
148 
149     protected final MavenProject getProject()
150     {
151         return project;
152     }
153 
154     /**
155      * Overload this to produce a jar with another classifier, for example a test-jar.
156      */
157     protected abstract String getClassifier();
158 
159     /**
160      * Overload this to produce a test-jar, for example.
161      */
162     protected abstract String getType();
163 
164     protected static File getJarFile( File basedir, String finalName, String classifier )
165     {
166         if ( classifier == null )
167         {
168             classifier = "";
169         }
170         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
171         {
172             classifier = "-" + classifier;
173         }
174 
175         return new File( basedir, finalName + classifier + ".jar" );
176     }
177 
178     /**
179      * Default Manifest location. Can point to a non existing file.
180      * Cannot return null.
181      */
182     protected File getDefaultManifestFile()
183     {
184         return defaultManifestFile;
185     }
186 
187 
188     /**
189      * Generates the JAR.
190      *
191      * @todo Add license files in META-INF directory.
192      */
193     public File createArchive()
194         throws MojoExecutionException
195     {
196         File jarFile = getJarFile( outputDirectory, finalName, getClassifier() );
197 
198         MavenArchiver archiver = new MavenArchiver();
199 
200         archiver.setArchiver( jarArchiver );
201 
202         archiver.setOutputFile( jarFile );
203 
204         archive.setForced( forceCreation );
205 
206         try
207         {
208             File contentDirectory = getClassesDirectory();
209             if ( !contentDirectory.exists() )
210             {
211                 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
212             }
213             else
214             {
215                 archiver.getArchiver().addDirectory( contentDirectory, getIncludes(), getExcludes() );
216             }
217 
218             File existingManifest = getDefaultManifestFile();
219 
220             if ( useDefaultManifestFile && existingManifest.exists() && archive.getManifestFile() == null )
221             {
222                 getLog().info( "Adding existing MANIFEST to archive. Found under: " + existingManifest.getPath() );
223                 archive.setManifestFile( existingManifest );
224             }
225 
226             archiver.createArchive( session, project, archive );
227 
228             return jarFile;
229         }
230         catch ( Exception e )
231         {
232             // TODO: improve error handling
233             throw new MojoExecutionException( "Error assembling JAR", e );
234         }
235     }
236 
237     /**
238      * Generates the JAR.
239      *
240      * @todo Add license files in META-INF directory.
241      */
242     public void execute()
243         throws MojoExecutionException
244     {
245         if ( skipIfEmpty && ( !getClassesDirectory().exists() || getClassesDirectory().list().length < 1 ) )
246         {
247             getLog().info( "Skipping packaging of the " + getType() );
248         }
249         else
250         {
251             File jarFile = createArchive();
252 
253             String classifier = getClassifier();
254             if ( classifier != null )
255             {
256                 projectHelper.attachArtifact( getProject(), getType(), classifier, jarFile );
257             }
258             else
259             {
260                 getProject().getArtifact().setFile( jarFile );
261             }
262         }
263     }
264 
265     private String[] getIncludes()
266     {
267         if ( includes != null && includes.length > 0 )
268         {
269             return includes;
270         }
271         return DEFAULT_INCLUDES;
272     }
273 
274     private String[] getExcludes()
275     {
276         if ( excludes != null && excludes.length > 0 )
277         {
278             return excludes;
279         }
280         return DEFAULT_EXCLUDES;
281     }
282 }