1 package org.apache.maven.plugins.jar;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
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
52
53
54 @Parameter
55 private String[] includes;
56
57
58
59
60
61 @Parameter
62 private String[] excludes;
63
64
65
66
67 @Parameter( defaultValue = "${project.build.directory}", required = true )
68 private File outputDirectory;
69
70
71
72
73 @Parameter( defaultValue = "${project.build.finalName}", readonly = true )
74 private String finalName;
75
76
77
78
79 @Component( role = Archiver.class, hint = "jar" )
80 private JarArchiver jarArchiver;
81
82
83
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
96
97
98 @Parameter
99 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
100
101
102
103
104
105
106
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
119
120
121
122
123
124
125
126
127
128 @Parameter( property = "maven.jar.forceCreation", defaultValue = "false" )
129 private boolean forceCreation;
130
131
132
133
134 @Parameter( defaultValue = "false" )
135 private boolean skipIfEmpty;
136
137
138
139
140
141 protected abstract File getClassesDirectory();
142
143
144
145
146 protected final MavenProject getProject()
147 {
148 return project;
149 }
150
151
152
153
154
155 protected abstract String getClassifier();
156
157
158
159
160
161 protected abstract String getType();
162
163
164
165
166
167
168
169
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
196
197
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
231 throw new MojoExecutionException( "Error assembling JAR", e );
232 }
233 }
234
235
236
237
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
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 }