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.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 1740869 2016-04-25 18:08:04Z 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( defaultValue = "${project.build.finalName}", readonly = true )
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 {@link {MavenProject}.
84       */
85      @Parameter( defaultValue = "${project}", readonly = true, required = true )
86      private MavenProject project;
87  
88      /**
89       * The {@link MavenSession}.
90       */
91      @Parameter( defaultValue = "${session}", readonly = true, required = true )
92      private MavenSession session;
93  
94      /**
95       * The archive configuration to use. See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
96       * Archiver Reference</a>.
97       */
98      @Parameter
99      private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
100 
101     /**
102      * Using this property will fail your build cause it has been removed from the plugin configuration. See the
103      * <a href="https://maven.apache.org/plugins/maven-jar-plugin/">Major Version Upgrade to version 3.0.0</a> for the
104      * plugin.
105      * 
106      * @deprecated For version 3.0.0 this parameter is only defined here to break the build if you use it!
107      */
108     @Parameter( property = "jar.useDefaultManifestFile", defaultValue = "false" )
109     private boolean useDefaultManifestFile;
110 
111     /**
112      *
113      */
114     @Component
115     private MavenProjectHelper projectHelper;
116 
117     /**
118      * Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this
119      * plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the
120      * plugin skips creation of the jar. This does not work when other plugins, like the maven-shade-plugin, are
121      * configured to post-process the jar. This plugin can not detect the post-processing, and so leaves the
122      * post-processed jar in place. This can lead to failures when those plugins do not expect to find their own output
123      * as an input. Set this parameter to <tt>true</tt> to avoid these problems by forcing this plugin to recreate the
124      * jar every time.<br/>
125      * Starting with <b>3.0.0</b> the property has been renamed from <code>jar.forceCreation</code> to
126      * <code>maven.jar.forceCreation</code>.
127      */
128     @Parameter( property = "maven.jar.forceCreation", defaultValue = "false" )
129     private boolean forceCreation;
130 
131     /**
132      * Skip creating empty archives.
133      */
134     @Parameter( defaultValue = "false" )
135     private boolean skipIfEmpty;
136 
137     /**
138      * Return the specific output directory to serve as the root for the archive.
139      * @return get classes directory.
140      */
141     protected abstract File getClassesDirectory();
142 
143     /**
144      * @return the {@link #project}
145      */
146     protected final MavenProject getProject()
147     {
148         return project;
149     }
150 
151     /**
152      * Overload this to produce a jar with another classifier, for example a test-jar.
153      * @return get the classifier.
154      */
155     protected abstract String getClassifier();
156 
157     /**
158      * Overload this to produce a test-jar, for example.
159      * @return return the type.
160      */
161     protected abstract String getType();
162 
163     /**
164      * Returns the Jar file to generate, based on an optional classifier.
165      *
166      * @param basedir the output directory
167      * @param resultFinalName the name of the ear file
168      * @param classifier an optional classifier
169      * @return the file to generate
170      */
171     protected File getJarFile( File basedir, String resultFinalName, String classifier )
172     {
173         if ( basedir == null )
174         {
175             throw new IllegalArgumentException( "basedir is not allowed to be null" );
176         }
177         if ( resultFinalName == null )
178         {
179             throw new IllegalArgumentException( "finalName is not allowed to be null" );
180         }
181 
182         StringBuilder fileName = new StringBuilder( resultFinalName );
183 
184         if ( hasClassifier() )
185         {
186             fileName.append( "-" ).append( classifier );
187         }
188 
189         fileName.append( ".jar" );
190 
191         return new File( basedir, fileName.toString() );
192     }
193 
194     /**
195      * Generates the JAR.
196      * @return The instance of File for the created archive file.
197      * @throws MojoExecutionException in case of an error.
198      */
199     public File createArchive()
200         throws MojoExecutionException
201     {
202         File jarFile = getJarFile( outputDirectory, finalName, getClassifier() );
203 
204         MavenArchiver archiver = new MavenArchiver();
205 
206         archiver.setArchiver( jarArchiver );
207 
208         archiver.setOutputFile( jarFile );
209 
210         archive.setForced( forceCreation );
211 
212         try
213         {
214             File contentDirectory = getClassesDirectory();
215             if ( !contentDirectory.exists() )
216             {
217                 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
218             }
219             else
220             {
221                 archiver.getArchiver().addDirectory( contentDirectory, getIncludes(), getExcludes() );
222             }
223 
224             archiver.createArchive( session, project, archive );
225 
226             return jarFile;
227         }
228         catch ( Exception e )
229         {
230             // TODO: improve error handling
231             throw new MojoExecutionException( "Error assembling JAR", e );
232         }
233     }
234 
235     /**
236      * Generates the JAR.
237      * @throws MojoExecutionException in case of an error.
238      */
239     public void execute()
240         throws MojoExecutionException
241     {
242         if ( useDefaultManifestFile )
243         {
244             throw new MojoExecutionException( "You are using 'useDefaultManifestFile' which has been removed"
245                 + " from the maven-jar-plugin. "
246                 + "Please see the >>Major Version Upgrade to version 3.0.0<< on the plugin site." );
247         }
248 
249         if ( skipIfEmpty && ( !getClassesDirectory().exists() || getClassesDirectory().list().length < 1 ) )
250         {
251             getLog().info( "Skipping packaging of the " + getType() );
252         }
253         else
254         {
255             File jarFile = createArchive();
256 
257             if ( hasClassifier() )
258             {
259                 projectHelper.attachArtifact( getProject(), getType(), getClassifier(), jarFile );
260             }
261             else
262             {
263                 if ( projectHasAlreadySetAnArtifact() )
264                 {
265                     throw new MojoExecutionException( "You have to use a classifier "
266                         + "to attach supplemental artifacts to the project instead of replacing them." );
267                 }
268                 getProject().getArtifact().setFile( jarFile );
269             }
270         }
271     }
272 
273     private boolean projectHasAlreadySetAnArtifact()
274     {
275         if ( getProject().getArtifact().getFile() != null )
276         {
277             return getProject().getArtifact().getFile().isFile();
278         }
279         else
280         {
281             return false;
282         }
283     }
284 
285     /**
286      * @return true in case where the classifier is not {@code null} and contains something else than white spaces.
287      */
288     protected boolean hasClassifier()
289     {
290         boolean result = false;
291         if ( getClassifier() != null && getClassifier().trim().length() > 0 )
292         {
293             result = true;
294         }
295 
296         return result;
297     }
298 
299     private String[] getIncludes()
300     {
301         if ( includes != null && includes.length > 0 )
302         {
303             return includes;
304         }
305         return DEFAULT_INCLUDES;
306     }
307 
308     private String[] getExcludes()
309     {
310         if ( excludes != null && excludes.length > 0 )
311         {
312             return excludes;
313         }
314         return DEFAULT_EXCLUDES;
315     }
316 }