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 public class EjbMojo
46 extends AbstractMojo
47 {
48 private static final String EJB_JAR_XML = "META-INF/ejb-jar.xml";
49
50
51 private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
52
53 private static final String[] DEFAULT_EXCLUDES = new String[]{EJB_JAR_XML, "**/package.html"};
54
55 private static final String[] DEFAULT_CLIENT_EXCLUDES =
56 new String[]{"**/*Bean.class", "**/*CMP.class", "**/*Session.class", "**/package.html"};
57
58 private static final String[] EMPTY_STRING_ARRAY = new String[0];
59
60
61
62
63
64
65
66
67 private File basedir;
68
69
70
71
72
73
74
75 private File outputDirectory;
76
77
78
79
80
81
82
83 private String jarName;
84
85
86
87
88
89
90
91 private String classifier;
92
93
94
95
96
97
98 private boolean generateClient;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 private List clientExcludes;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 private List clientIncludes;
130
131
132
133
134
135
136
137
138
139
140
141
142
143 private List excludes;
144
145
146
147
148
149
150
151
152 private MavenProject project;
153
154
155
156
157
158
159 private JarArchiver jarArchiver;
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 private String ejbVersion;
176
177
178
179
180
181
182 private JarArchiver clientJarArchiver;
183
184
185
186
187
188
189 private MavenProjectHelper projectHelper;
190
191
192
193
194
195
196
197
198 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
199
200
201
202
203
204
205 public void execute()
206 throws MojoExecutionException
207 {
208 if ( getLog().isInfoEnabled() )
209 {
210 getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion );
211 }
212
213 File jarFile = getEJBJarFile( basedir, jarName, classifier );
214
215 MavenArchiver archiver = new MavenArchiver();
216
217 archiver.setArchiver( jarArchiver );
218
219 archiver.setOutputFile( jarFile );
220
221 File deploymentDescriptor = new File( outputDirectory, EJB_JAR_XML );
222
223
224 if ( !ejbVersion.matches( "\\A[2-3]\\.[0-9]\\z" ) )
225 {
226 throw new MojoExecutionException(
227 "ejbVersion is not valid: " + ejbVersion + ". Must be 2.x or 3.x (where x is a digit)" );
228 }
229
230 if ( ejbVersion.matches( "\\A2\\.[0-9]\\z" ) && !deploymentDescriptor.exists() )
231 {
232 throw new MojoExecutionException(
233 "Error assembling EJB: " + EJB_JAR_XML + " is required for ejbVersion 2.x" );
234 }
235
236 try
237 {
238 String[] mainJarExcludes = DEFAULT_EXCLUDES;
239
240 if ( excludes != null && !excludes.isEmpty() ) {
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 }