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.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.project.MavenProject;
27 import org.apache.maven.project.MavenProjectHelper;
28 import org.codehaus.plexus.archiver.jar.JarArchiver;
29
30 import java.io.File;
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
100 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
101
102
103
104
105
106
107
108
109
110
111 private File defaultManifestFile;
112
113
114
115
116
117
118
119
120 private boolean useDefaultManifestFile;
121
122
123
124
125 private MavenProjectHelper projectHelper;
126
127
128
129
130
131
132 private boolean forceCreation;
133
134
135
136
137 protected abstract File getClassesDirectory();
138
139 protected final MavenProject getProject()
140 {
141 return project;
142 }
143
144
145
146
147 protected abstract String getClassifier();
148
149
150
151
152 protected abstract String getType();
153
154 protected static File getJarFile( File basedir, String finalName, String classifier )
155 {
156 if ( classifier == null )
157 {
158 classifier = "";
159 }
160 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
161 {
162 classifier = "-" + classifier;
163 }
164
165 return new File( basedir, finalName + classifier + ".jar" );
166 }
167
168
169
170
171
172 protected File getDefaultManifestFile()
173 {
174 return defaultManifestFile;
175 }
176
177
178
179
180
181
182
183 public File createArchive()
184 throws MojoExecutionException
185 {
186 File jarFile = getJarFile( outputDirectory, finalName, getClassifier() );
187
188 MavenArchiver archiver = new MavenArchiver();
189
190 archiver.setArchiver( jarArchiver );
191
192 archiver.setOutputFile( jarFile );
193
194 archive.setForced( forceCreation );
195
196 try
197 {
198 File contentDirectory = getClassesDirectory();
199 if ( !contentDirectory.exists() )
200 {
201 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
202 }
203 else
204 {
205 archiver.getArchiver().addDirectory( contentDirectory, getIncludes(), getExcludes() );
206 }
207
208 File existingManifest = getDefaultManifestFile();
209
210 if ( useDefaultManifestFile && existingManifest.exists() && archive.getManifestFile() == null )
211 {
212 getLog().info( "Adding existing MANIFEST to archive. Found under: " + existingManifest.getPath() );
213 archive.setManifestFile( existingManifest );
214 }
215
216 archiver.createArchive( project, archive );
217
218 return jarFile;
219 }
220 catch ( Exception e )
221 {
222
223 throw new MojoExecutionException( "Error assembling JAR", e );
224 }
225 }
226
227
228
229
230
231
232 public void execute()
233 throws MojoExecutionException
234 {
235 File jarFile = createArchive();
236
237 String classifier = getClassifier();
238 if ( classifier != null )
239 {
240 projectHelper.attachArtifact( getProject(), getType(), classifier, jarFile );
241 }
242 else
243 {
244 getProject().getArtifact().setFile( jarFile );
245 }
246 }
247
248 private String[] getIncludes()
249 {
250 if ( includes != null && includes.length > 0 )
251 {
252 return includes;
253 }
254 return DEFAULT_INCLUDES;
255 }
256
257 private String[] getExcludes()
258 {
259 if ( excludes != null && excludes.length > 0 )
260 {
261 return excludes;
262 }
263 return DEFAULT_EXCLUDES;
264 }
265 }