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 public class EjbMojo
47 extends AbstractMojo
48 {
49 private static final String EJB_JAR_XML = "META-INF/ejb-jar.xml";
50
51
52 private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
53
54 private static final String[] DEFAULT_EXCLUDES = new String[]{EJB_JAR_XML, "**/package.html"};
55
56 private static final String[] DEFAULT_CLIENT_EXCLUDES =
57 new String[]{"**/*Bean.class", "**/*CMP.class", "**/*Session.class", "**/package.html"};
58
59 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 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
197
198
199
200
201
202
203 public void execute()
204 throws MojoExecutionException
205 {
206 if ( getLog().isInfoEnabled() )
207 {
208 getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion );
209 }
210
211 File jarFile = getEJBJarFile( basedir, jarName, classifier );
212
213 MavenArchiver archiver = new MavenArchiver();
214
215 archiver.setArchiver( jarArchiver );
216
217 archiver.setOutputFile( jarFile );
218
219 File deploymentDescriptor = new File( outputDirectory, EJB_JAR_XML );
220
221
222 if ( !ejbVersion.matches( "\\A[2-3]\\.[0-9]\\z" ) )
223 {
224 throw new MojoExecutionException(
225 "ejbVersion is not valid: " + ejbVersion + ". Must be 2.x or 3.x (where x is a digit)" );
226 }
227
228 if ( ejbVersion.matches( "\\A2\\.[0-9]\\z" ) && !deploymentDescriptor.exists() )
229 {
230 throw new MojoExecutionException(
231 "Error assembling EJB: " + EJB_JAR_XML + " is required for ejbVersion 2.x" );
232 }
233
234 try
235 {
236 String[] mainJarExcludes = DEFAULT_EXCLUDES;
237
238 if ( excludes != null && !excludes.isEmpty() )
239 {
240 excludes.add( EJB_JAR_XML );
241 mainJarExcludes = (String[]) excludes.toArray( EMPTY_STRING_ARRAY );
242 }
243
244 archiver.getArchiver().addDirectory( outputDirectory, DEFAULT_INCLUDES, mainJarExcludes );
245
246 if ( deploymentDescriptor.exists() )
247 {
248 archiver.getArchiver().addFile( deploymentDescriptor, EJB_JAR_XML );
249 }
250
251
252 archiver.createArchive( project, archive );
253 }
254 catch ( ArchiverException e )
255 {
256 throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
257 }
258 catch ( ManifestException e )
259 {
260 throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
261 }
262 catch ( IOException e )
263 {
264 throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
265 }
266 catch ( DependencyResolutionRequiredException e )
267 {
268 throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
269 }
270
271
272 if ( classifier != null )
273 {
274 projectHelper.attachArtifact( project, "ejb", classifier, jarFile );
275 }
276 else
277 {
278 project.getArtifact().setFile( jarFile );
279 }
280
281 if ( generateClient )
282 {
283 String clientJarName = jarName;
284 if ( classifier != null )
285 {
286 clientJarName += "-" + classifier;
287 }
288
289 getLog().info( "Building EJB client " + clientJarName + "-client" );
290
291 String[] excludes = DEFAULT_CLIENT_EXCLUDES;
292 String[] includes = DEFAULT_INCLUDES;
293
294 if ( clientIncludes != null && !clientIncludes.isEmpty() )
295 {
296 includes = (String[]) clientIncludes.toArray( EMPTY_STRING_ARRAY );
297 }
298
299 if ( clientExcludes != null && !clientExcludes.isEmpty() )
300 {
301 excludes = (String[]) clientExcludes.toArray( EMPTY_STRING_ARRAY );
302 }
303
304 File clientJarFile = new File( basedir, clientJarName + "-client.jar" );
305
306 MavenArchiver clientArchiver = new MavenArchiver();
307
308 clientArchiver.setArchiver( clientJarArchiver );
309
310 clientArchiver.setOutputFile( clientJarFile );
311
312 try
313 {
314 clientArchiver.getArchiver().addDirectory( outputDirectory, includes, excludes );
315
316
317 clientArchiver.createArchive( project, archive );
318
319 }
320 catch ( ArchiverException e )
321 {
322 throw new MojoExecutionException(
323 "There was a problem creating the EJB client archive: " + e.getMessage(), e );
324 }
325 catch ( ManifestException e )
326 {
327 throw new MojoExecutionException(
328 "There was a problem creating the EJB client archive: " + e.getMessage(), e );
329 }
330 catch ( IOException e )
331 {
332 throw new MojoExecutionException(
333 "There was a problem creating the EJB client archive: " + e.getMessage(), e );
334 }
335 catch ( DependencyResolutionRequiredException e )
336 {
337 throw new MojoExecutionException(
338 "There was a problem creating the EJB client archive: " + e.getMessage(), e );
339 }
340
341
342 if ( classifier != null )
343 {
344 projectHelper.attachArtifact( project, "ejb-client", classifier + "-client", clientJarFile );
345 }
346 else
347 {
348 projectHelper.attachArtifact( project, "ejb-client", "client", clientJarFile );
349 }
350 }
351 }
352
353
354
355
356
357
358
359
360
361 private static File getEJBJarFile( File basedir, String finalName, String classifier )
362 {
363 if ( classifier == null )
364 {
365 classifier = "";
366 }
367 else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
368 {
369 classifier = "-" + classifier;
370 }
371
372 return new File( basedir, finalName + classifier + ".jar" );
373 }
374
375 }