1 package org.apache.maven.plugin.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( alias = "jarName", property = "jar.finalName", defaultValue = "${project.build.finalName}" )
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 @Parameter( defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true,
108 readonly = true )
109 private File defaultManifestFile;
110
111
112
113
114
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
127
128
129
130
131
132
133
134
135 @Parameter( property = "jar.forceCreation", defaultValue = "false" )
136 private boolean forceCreation;
137
138
139
140
141 @Parameter( property = "jar.skipIfEmpty", defaultValue = "false" )
142 private boolean skipIfEmpty;
143
144
145
146
147 protected abstract File getClassesDirectory();
148
149 protected final MavenProject getProject()
150 {
151 return project;
152 }
153
154
155
156
157 protected abstract String getClassifier();
158
159
160
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
180
181
182 protected File getDefaultManifestFile()
183 {
184 return defaultManifestFile;
185 }
186
187
188
189
190
191
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
233 throw new MojoExecutionException( "Error assembling JAR", e );
234 }
235 }
236
237
238
239
240
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 }