1 | |
package org.apache.maven.plugin.ejb; |
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.artifact.DependencyResolutionRequiredException; |
25 | |
import org.apache.maven.plugin.AbstractMojo; |
26 | |
import org.apache.maven.plugin.MojoExecutionException; |
27 | |
import org.apache.maven.project.MavenProject; |
28 | |
import org.apache.maven.project.MavenProjectHelper; |
29 | |
import org.codehaus.plexus.archiver.ArchiverException; |
30 | |
import org.codehaus.plexus.archiver.jar.JarArchiver; |
31 | |
import org.codehaus.plexus.archiver.jar.ManifestException; |
32 | |
|
33 | |
import java.io.File; |
34 | |
import java.io.IOException; |
35 | |
import java.util.List; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 15 | public class EjbMojo |
47 | |
extends AbstractMojo |
48 | |
{ |
49 | |
private static final String EJB_JAR_XML = "META-INF/ejb-jar.xml"; |
50 | |
|
51 | |
|
52 | 1 | private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"}; |
53 | |
|
54 | 1 | private static final String[] DEFAULT_EXCLUDES = new String[]{EJB_JAR_XML, "**/package.html"}; |
55 | |
|
56 | 1 | private static final String[] DEFAULT_CLIENT_EXCLUDES = |
57 | |
new String[]{"**/*Bean.class", "**/*CMP.class", "**/*Session.class", "**/package.html"}; |
58 | |
|
59 | 1 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private File basedir; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private File outputDirectory; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private String jarName; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private String classifier; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private boolean generateClient; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private List clientExcludes; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private List clientIncludes; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private List excludes; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private MavenProject project; |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private JarArchiver jarArchiver; |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
private String ejbVersion; |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
private JarArchiver clientJarArchiver; |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
private MavenProjectHelper projectHelper; |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | 15 | private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public void execute() |
204 | |
throws MojoExecutionException |
205 | |
{ |
206 | 14 | if ( getLog().isInfoEnabled() ) |
207 | |
{ |
208 | 14 | getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion ); |
209 | |
} |
210 | |
|
211 | 14 | File jarFile = getEJBJarFile( basedir, jarName, classifier ); |
212 | |
|
213 | 14 | MavenArchiver archiver = new MavenArchiver(); |
214 | |
|
215 | 14 | archiver.setArchiver( jarArchiver ); |
216 | |
|
217 | 14 | archiver.setOutputFile( jarFile ); |
218 | |
|
219 | 14 | File deploymentDescriptor = new File( outputDirectory, EJB_JAR_XML ); |
220 | |
|
221 | |
|
222 | 14 | if ( !ejbVersion.matches( "\\A[2-3]\\.[0-9]\\z" ) ) |
223 | |
{ |
224 | 0 | throw new MojoExecutionException( |
225 | |
"ejbVersion is not valid: " + ejbVersion + ". Must be 2.x or 3.x (where x is a digit)" ); |
226 | |
} |
227 | |
|
228 | 14 | if ( ejbVersion.matches( "\\A2\\.[0-9]\\z" ) && !deploymentDescriptor.exists() ) |
229 | |
{ |
230 | 1 | throw new MojoExecutionException( |
231 | |
"Error assembling EJB: " + EJB_JAR_XML + " is required for ejbVersion 2.x" ); |
232 | |
} |
233 | |
|
234 | |
try |
235 | |
{ |
236 | 13 | String[] mainJarExcludes = DEFAULT_EXCLUDES; |
237 | |
|
238 | 13 | if ( excludes != null && !excludes.isEmpty() ) |
239 | |
{ |
240 | 1 | excludes.add( EJB_JAR_XML ); |
241 | 1 | mainJarExcludes = (String[]) excludes.toArray( EMPTY_STRING_ARRAY ); |
242 | |
} |
243 | |
|
244 | 13 | archiver.getArchiver().addDirectory( outputDirectory, DEFAULT_INCLUDES, mainJarExcludes ); |
245 | |
|
246 | 13 | if ( deploymentDescriptor.exists() ) |
247 | |
{ |
248 | 12 | archiver.getArchiver().addFile( deploymentDescriptor, EJB_JAR_XML ); |
249 | |
} |
250 | |
|
251 | |
|
252 | 13 | archiver.createArchive( project, archive ); |
253 | |
} |
254 | 0 | catch ( ArchiverException e ) |
255 | |
{ |
256 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
257 | |
} |
258 | 0 | catch ( ManifestException e ) |
259 | |
{ |
260 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
261 | |
} |
262 | 0 | catch ( IOException e ) |
263 | |
{ |
264 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
265 | |
} |
266 | 0 | catch ( DependencyResolutionRequiredException e ) |
267 | |
{ |
268 | 0 | throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e ); |
269 | 13 | } |
270 | |
|
271 | |
|
272 | 13 | if ( classifier != null ) |
273 | |
{ |
274 | 2 | projectHelper.attachArtifact( project, "ejb", classifier, jarFile ); |
275 | |
} |
276 | |
else |
277 | |
{ |
278 | 11 | project.getArtifact().setFile( jarFile ); |
279 | |
} |
280 | |
|
281 | 13 | if ( generateClient ) |
282 | |
{ |
283 | 8 | String clientJarName = jarName; |
284 | 8 | if ( classifier != null ) |
285 | |
{ |
286 | 1 | clientJarName += "-" + classifier; |
287 | |
} |
288 | |
|
289 | 8 | getLog().info( "Building EJB client " + clientJarName + "-client" ); |
290 | |
|
291 | 8 | String[] excludes = DEFAULT_CLIENT_EXCLUDES; |
292 | 8 | String[] includes = DEFAULT_INCLUDES; |
293 | |
|
294 | 8 | if ( clientIncludes != null && !clientIncludes.isEmpty() ) |
295 | |
{ |
296 | 2 | includes = (String[]) clientIncludes.toArray( EMPTY_STRING_ARRAY ); |
297 | |
} |
298 | |
|
299 | 8 | if ( clientExcludes != null && !clientExcludes.isEmpty() ) |
300 | |
{ |
301 | 2 | excludes = (String[]) clientExcludes.toArray( EMPTY_STRING_ARRAY ); |
302 | |
} |
303 | |
|
304 | 8 | File clientJarFile = new File( basedir, clientJarName + "-client.jar" ); |
305 | |
|
306 | 8 | MavenArchiver clientArchiver = new MavenArchiver(); |
307 | |
|
308 | 8 | clientArchiver.setArchiver( clientJarArchiver ); |
309 | |
|
310 | 8 | clientArchiver.setOutputFile( clientJarFile ); |
311 | |
|
312 | |
try |
313 | |
{ |
314 | 8 | clientArchiver.getArchiver().addDirectory( outputDirectory, includes, excludes ); |
315 | |
|
316 | |
|
317 | 8 | clientArchiver.createArchive( project, archive ); |
318 | |
|
319 | |
} |
320 | 0 | catch ( ArchiverException e ) |
321 | |
{ |
322 | 0 | throw new MojoExecutionException( |
323 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
324 | |
} |
325 | 0 | catch ( ManifestException e ) |
326 | |
{ |
327 | 0 | throw new MojoExecutionException( |
328 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
329 | |
} |
330 | 0 | catch ( IOException e ) |
331 | |
{ |
332 | 0 | throw new MojoExecutionException( |
333 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
334 | |
} |
335 | 0 | catch ( DependencyResolutionRequiredException e ) |
336 | |
{ |
337 | 0 | throw new MojoExecutionException( |
338 | |
"There was a problem creating the EJB client archive: " + e.getMessage(), e ); |
339 | 8 | } |
340 | |
|
341 | |
|
342 | 8 | if ( classifier != null ) |
343 | |
{ |
344 | 1 | projectHelper.attachArtifact( project, "ejb-client", classifier + "-client", clientJarFile ); |
345 | |
} |
346 | |
else |
347 | |
{ |
348 | 7 | projectHelper.attachArtifact( project, "ejb-client", "client", clientJarFile ); |
349 | |
} |
350 | |
} |
351 | 13 | } |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
private static File getEJBJarFile( File basedir, String finalName, String classifier ) |
362 | |
{ |
363 | 14 | if ( classifier == null ) |
364 | |
{ |
365 | 12 | classifier = ""; |
366 | |
} |
367 | 2 | else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) ) |
368 | |
{ |
369 | 2 | classifier = "-" + classifier; |
370 | |
} |
371 | |
|
372 | 14 | return new File( basedir, finalName + classifier + ".jar" ); |
373 | |
} |
374 | |
|
375 | |
} |