View Javadoc
1   package org.apache.maven.plugins.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.apache.maven.shared.model.fileset.FileSet;
32  import org.apache.maven.shared.model.fileset.util.FileSetManager;
33  import org.codehaus.plexus.archiver.Archiver;
34  import org.codehaus.plexus.archiver.jar.JarArchiver;
35  
36  import java.io.File;
37  import java.util.Arrays;
38  import java.util.Map;
39  
40  /**
41   * Base class for creating a jar from project classes.
42   *
43   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
44   * @version $Id$
45   */
46  public abstract class AbstractJarMojo
47      extends AbstractMojo
48  {
49  
50      private static final String[] DEFAULT_EXCLUDES = new String[] { "**/package.html" };
51  
52      private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
53  
54      private static final String MODULE_DESCRIPTOR_FILE_NAME = "module-info.class";
55  
56      /**
57       * List of files to include. Specified as fileset patterns which are relative to the input directory whose contents
58       * is being packaged into the JAR.
59       */
60      @Parameter
61      private String[] includes;
62  
63      /**
64       * List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents
65       * is being packaged into the JAR.
66       */
67      @Parameter
68      private String[] excludes;
69  
70      /**
71       * Directory containing the generated JAR.
72       */
73      @Parameter( defaultValue = "${project.build.directory}", required = true )
74      private File outputDirectory;
75  
76      /**
77       * Name of the generated JAR.
78       */
79      @Parameter( defaultValue = "${project.build.finalName}", readonly = true )
80      private String finalName;
81  
82      /**
83       * The Jar archiver.
84       */
85      @Component
86      private Map<String, Archiver> archivers;
87  
88      /**
89       * The {@link {MavenProject}.
90       */
91      @Parameter( defaultValue = "${project}", readonly = true, required = true )
92      private MavenProject project;
93  
94      /**
95       * The {@link MavenSession}.
96       */
97      @Parameter( defaultValue = "${session}", readonly = true, required = true )
98      private MavenSession session;
99  
100     /**
101      * The archive configuration to use. See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
102      * Archiver Reference</a>.
103      */
104     @Parameter
105     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
106 
107     /**
108      * Using this property will fail your build cause it has been removed from the plugin configuration. See the
109      * <a href="https://maven.apache.org/plugins/maven-jar-plugin/">Major Version Upgrade to version 3.0.0</a> for the
110      * plugin.
111      * 
112      * @deprecated For version 3.0.0 this parameter is only defined here to break the build if you use it!
113      */
114     @Parameter( property = "jar.useDefaultManifestFile", defaultValue = "false" )
115     private boolean useDefaultManifestFile;
116 
117     /**
118      *
119      */
120     @Component
121     private MavenProjectHelper projectHelper;
122 
123     /**
124      * Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this
125      * plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the
126      * plugin skips creation of the jar. This does not work when other plugins, like the maven-shade-plugin, are
127      * configured to post-process the jar. This plugin can not detect the post-processing, and so leaves the
128      * post-processed jar in place. This can lead to failures when those plugins do not expect to find their own output
129      * as an input. Set this parameter to <tt>true</tt> to avoid these problems by forcing this plugin to recreate the
130      * jar every time.<br/>
131      * Starting with <b>3.0.0</b> the property has been renamed from <code>jar.forceCreation</code> to
132      * <code>maven.jar.forceCreation</code>.
133      */
134     @Parameter( property = "maven.jar.forceCreation", defaultValue = "false" )
135     private boolean forceCreation;
136 
137     /**
138      * Skip creating empty archives.
139      */
140     @Parameter( defaultValue = "false" )
141     private boolean skipIfEmpty;
142 
143     /**
144      * Timestamp for reproducible output archive entries, either formatted as ISO 8601
145      * <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
146      * <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
147      *
148      * @since 3.2.0
149      */
150     @Parameter( defaultValue = "${project.build.outputTimestamp}" )
151     private String outputTimestamp;
152 
153     /**
154      * Return the specific output directory to serve as the root for the archive.
155      * @return get classes directory.
156      */
157     protected abstract File getClassesDirectory();
158 
159     /**
160      * @return the {@link #project}
161      */
162     protected final MavenProject getProject()
163     {
164         return project;
165     }
166 
167     /**
168      * Overload this to produce a jar with another classifier, for example a test-jar.
169      * @return get the classifier.
170      */
171     protected abstract String getClassifier();
172 
173     /**
174      * Overload this to produce a test-jar, for example.
175      * @return return the type.
176      */
177     protected abstract String getType();
178 
179     /**
180      * Returns the Jar file to generate, based on an optional classifier.
181      *
182      * @param basedir the output directory
183      * @param resultFinalName the name of the ear file
184      * @param classifier an optional classifier
185      * @return the file to generate
186      */
187     protected File getJarFile( File basedir, String resultFinalName, String classifier )
188     {
189         if ( basedir == null )
190         {
191             throw new IllegalArgumentException( "basedir is not allowed to be null" );
192         }
193         if ( resultFinalName == null )
194         {
195             throw new IllegalArgumentException( "finalName is not allowed to be null" );
196         }
197 
198         StringBuilder fileName = new StringBuilder( resultFinalName );
199 
200         if ( hasClassifier() )
201         {
202             fileName.append( "-" ).append( classifier );
203         }
204 
205         fileName.append( ".jar" );
206 
207         return new File( basedir, fileName.toString() );
208     }
209 
210     /**
211      * Generates the JAR.
212      * @return The instance of File for the created archive file.
213      * @throws MojoExecutionException in case of an error.
214      */
215     public File createArchive()
216         throws MojoExecutionException
217     {
218         File jarFile = getJarFile( outputDirectory, finalName, getClassifier() );
219 
220         FileSetManager fileSetManager = new FileSetManager();
221         FileSet jarContentFileSet = new FileSet();
222         jarContentFileSet.setDirectory( getClassesDirectory().getAbsolutePath() );
223         jarContentFileSet.setIncludes( Arrays.asList( getIncludes() ) );
224         jarContentFileSet.setExcludes( Arrays.asList( getExcludes() ) );
225 
226         boolean containsModuleDescriptor = false;
227         String[] includedFiles = fileSetManager.getIncludedFiles( jarContentFileSet );
228         for ( String includedFile : includedFiles )
229         {
230             // May give false positives if the files is named as module descriptor
231             // but is not in the root of the archive or in the versioned area
232             // (and hence not actually a module descriptor).
233             // That is fine since the modular Jar archiver will gracefully
234             // handle such case.
235             // And also such case is unlikely to happen as file ending
236             // with "module-info.class" is unlikely to be included in Jar file
237             // unless it is a module descriptor.
238             if ( includedFile.endsWith( MODULE_DESCRIPTOR_FILE_NAME ) )
239             {
240                 containsModuleDescriptor = true;
241                 break;
242             }
243         }
244 
245         MavenArchiver archiver = new MavenArchiver();
246         archiver.setCreatedBy( "Maven Jar Plugin", "org.apache.maven.plugins", "maven-jar-plugin" );
247 
248         if ( containsModuleDescriptor )
249         {
250             archiver.setArchiver( (JarArchiver) archivers.get( "mjar" ) );
251         }
252         else
253         {
254             archiver.setArchiver( (JarArchiver) archivers.get( "jar" ) );
255         }
256 
257         archiver.setOutputFile( jarFile );
258 
259         // configure for Reproducible Builds based on outputTimestamp value
260         archiver.configureReproducible( outputTimestamp );
261 
262         archive.setForced( forceCreation );
263 
264         try
265         {
266             File contentDirectory = getClassesDirectory();
267             if ( !contentDirectory.exists() )
268             {
269                 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
270             }
271             else
272             {
273                 archiver.getArchiver().addDirectory( contentDirectory, getIncludes(), getExcludes() );
274             }
275 
276             archiver.createArchive( session, project, archive );
277 
278             return jarFile;
279         }
280         catch ( Exception e )
281         {
282             // TODO: improve error handling
283             throw new MojoExecutionException( "Error assembling JAR", e );
284         }
285     }
286 
287     /**
288      * Generates the JAR.
289      * @throws MojoExecutionException in case of an error.
290      */
291     public void execute()
292         throws MojoExecutionException
293     {
294         if ( useDefaultManifestFile )
295         {
296             throw new MojoExecutionException( "You are using 'useDefaultManifestFile' which has been removed"
297                 + " from the maven-jar-plugin. "
298                 + "Please see the >>Major Version Upgrade to version 3.0.0<< on the plugin site." );
299         }
300 
301         if ( skipIfEmpty && ( !getClassesDirectory().exists() || getClassesDirectory().list().length < 1 ) )
302         {
303             getLog().info( "Skipping packaging of the " + getType() );
304         }
305         else
306         {
307             File jarFile = createArchive();
308 
309             if ( hasClassifier() )
310             {
311                 projectHelper.attachArtifact( getProject(), getType(), getClassifier(), jarFile );
312             }
313             else
314             {
315                 if ( projectHasAlreadySetAnArtifact() )
316                 {
317                     throw new MojoExecutionException( "You have to use a classifier "
318                         + "to attach supplemental artifacts to the project instead of replacing them." );
319                 }
320                 getProject().getArtifact().setFile( jarFile );
321             }
322         }
323     }
324 
325     private boolean projectHasAlreadySetAnArtifact()
326     {
327         if ( getProject().getArtifact().getFile() != null )
328         {
329             return getProject().getArtifact().getFile().isFile();
330         }
331         else
332         {
333             return false;
334         }
335     }
336 
337     /**
338      * @return true in case where the classifier is not {@code null} and contains something else than white spaces.
339      */
340     protected boolean hasClassifier()
341     {
342         boolean result = false;
343         if ( getClassifier() != null && getClassifier().trim().length() > 0 )
344         {
345             result = true;
346         }
347 
348         return result;
349     }
350 
351     private String[] getIncludes()
352     {
353         if ( includes != null && includes.length > 0 )
354         {
355             return includes;
356         }
357         return DEFAULT_INCLUDES;
358     }
359 
360     private String[] getExcludes()
361     {
362         if ( excludes != null && excludes.length > 0 )
363         {
364             return excludes;
365         }
366         return DEFAULT_EXCLUDES;
367     }
368 }