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 java.io.File;
23 import org.apache.maven.archiver.MavenArchiveConfiguration;
24 import org.apache.maven.archiver.MavenArchiver;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.project.MavenProjectHelper;
30 import org.codehaus.plexus.archiver.jar.JarArchiver;
31
32
33
34
35
36
37
38 public abstract class AbstractJarMojo
39 extends AbstractMojo
40 {
41
42 private static final String[] DEFAULT_EXCLUDES = new String[] { "**/package.html" };
43
44 private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
45
46
47
48
49
50
51
52 private String[] includes;
53
54
55
56
57
58
59
60 private String[] excludes;
61
62
63
64
65
66
67
68 private File outputDirectory;
69
70
71
72
73
74
75
76 private String finalName;
77
78
79
80
81
82
83 private JarArchiver jarArchiver;
84
85
86
87
88
89
90
91
92 private MavenProject project;
93
94
95
96
97
98
99 private MavenSession session;
100
101
102
103
104
105
106
107 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
108
109
110
111
112
113
114
115
116
117
118 private File defaultManifestFile;
119
120
121
122
123
124
125
126
127 private boolean useDefaultManifestFile;
128
129
130
131
132 private MavenProjectHelper projectHelper;
133
134
135
136
137
138
139 private boolean forceCreation;
140
141
142
143
144
145
146 private boolean skipIfEmpty;
147
148
149
150
151 protected abstract File getClassesDirectory();
152
153 protected final MavenProject getProject()
154 {
155 return project;
156 }
157
158
159
160
161 protected abstract String getClassifier();
162
163
164
165
166 protected abstract String getType();
167
168 protected static File getJarFile( File basedir, String finalName, String classifier )
169 {
170 if ( classifier == null )
171 {
172 classifier = "";
173 }
174 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
175 {
176 classifier = "-" + classifier;
177 }
178
179 return new File( basedir, finalName + classifier + ".jar" );
180 }
181
182
183
184
185
186 protected File getDefaultManifestFile()
187 {
188 return defaultManifestFile;
189 }
190
191
192
193
194
195
196
197 public File createArchive()
198 throws MojoExecutionException
199 {
200 File jarFile = getJarFile( outputDirectory, finalName, getClassifier() );
201
202 MavenArchiver archiver = new MavenArchiver();
203
204 archiver.setArchiver( jarArchiver );
205
206 archiver.setOutputFile( jarFile );
207
208 archive.setForced( forceCreation );
209
210 try
211 {
212 File contentDirectory = getClassesDirectory();
213 if ( !contentDirectory.exists() )
214 {
215 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
216 }
217 else
218 {
219 archiver.getArchiver().addDirectory( contentDirectory, getIncludes(), getExcludes() );
220 }
221
222 File existingManifest = getDefaultManifestFile();
223
224 if ( useDefaultManifestFile && existingManifest.exists() && archive.getManifestFile() == null )
225 {
226 getLog().info( "Adding existing MANIFEST to archive. Found under: " + existingManifest.getPath() );
227 archive.setManifestFile( existingManifest );
228 }
229
230 archiver.createArchive( session, project, archive );
231
232 return jarFile;
233 }
234 catch ( Exception e )
235 {
236
237 throw new MojoExecutionException( "Error assembling JAR", e );
238 }
239 }
240
241
242
243
244
245
246 public void execute()
247 throws MojoExecutionException
248 {
249 if ( skipIfEmpty && !getClassesDirectory().exists() )
250 {
251 getLog().info( "Skipping packaging of the test-jar" );
252 }
253 else
254 {
255 File jarFile = createArchive();
256
257 String classifier = getClassifier();
258 if ( classifier != null )
259 {
260 projectHelper.attachArtifact( getProject(), getType(), classifier, jarFile );
261 }
262 else
263 {
264 getProject().getArtifact().setFile( jarFile );
265 }
266 }
267 }
268
269 private String[] getIncludes()
270 {
271 if ( includes != null && includes.length > 0 )
272 {
273 return includes;
274 }
275 return DEFAULT_INCLUDES;
276 }
277
278 private String[] getExcludes()
279 {
280 if ( excludes != null && excludes.length > 0 )
281 {
282 return excludes;
283 }
284 return DEFAULT_EXCLUDES;
285 }
286 }