View Javadoc
1   package org.apache.maven.plugin.ejb;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.apache.commons.io.input.XmlStreamReader;
28  import org.apache.maven.archiver.MavenArchiveConfiguration;
29  import org.apache.maven.archiver.MavenArchiver;
30  import org.apache.maven.artifact.DependencyResolutionRequiredException;
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugins.annotations.Component;
35  import org.apache.maven.plugins.annotations.LifecyclePhase;
36  import org.apache.maven.plugins.annotations.Mojo;
37  import org.apache.maven.plugins.annotations.Parameter;
38  import org.apache.maven.plugins.annotations.ResolutionScope;
39  import org.apache.maven.project.MavenProject;
40  import org.apache.maven.project.MavenProjectHelper;
41  import org.apache.maven.shared.filtering.MavenFileFilter;
42  import org.apache.maven.shared.filtering.MavenFilteringException;
43  import org.apache.maven.shared.filtering.MavenResourcesExecution;
44  import org.apache.maven.shared.utils.io.FileUtils.FilterWrapper;
45  import org.codehaus.plexus.archiver.Archiver;
46  import org.codehaus.plexus.archiver.ArchiverException;
47  import org.codehaus.plexus.archiver.jar.JarArchiver;
48  import org.codehaus.plexus.archiver.jar.ManifestException;
49  import org.codehaus.plexus.util.FileUtils;
50  
51  /**
52   * Build an EJB (and optional client) from the current project.
53   *
54   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
55   * @version $Id: EjbMojo.java 1616870 2014-08-08 20:25:01Z khmarbaise $
56   */
57  @Mojo( name = "ejb", requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true, defaultPhase = LifecyclePhase.PACKAGE )
58  public class EjbMojo
59      extends AbstractMojo
60  {
61      // TODO: will null work instead?
62      private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
63  
64      private static final String[] DEFAULT_CLIENT_EXCLUDES = new String[] { "**/*Bean.class", "**/*CMP.class",
65          "**/*Session.class", "**/package.html" };
66  
67      /**
68       * The directory for the generated EJB.
69       */
70      @Parameter( defaultValue = "${project.build.directory}", required = true, readonly = true )
71      private File basedir;
72  
73      /**
74       * Directory that resources are copied to during the build.
75       */
76      @Parameter( property = "outputDirectory", defaultValue = "${project.build.outputDirectory}" )
77      private File outputDirectory;
78  
79      /**
80       * The name of the EJB file to generate.
81       */
82      @Parameter( property = "jarName", defaultValue = "${project.build.finalName}" )
83      private String jarName;
84  
85      /**
86       * Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
87       */
88      @Parameter( property = "ejb.classifier" )
89      private String classifier;
90  
91      /**
92       * You can define the location of <code>ejb-jar.xml</code> file.
93       */
94      @Parameter( property = "ejb.ejbJar", defaultValue = "META-INF/ejb-jar.xml" )
95      // The initalization is needed to get the unit tests running which seemed to lack lookup for the defaultValue.
96      private String ejbJar = "META-INF/ejb-jar.xml";
97  
98      /**
99       * Whether the EJB client jar should be generated or not.
100      */
101     @Parameter( property = "ejb.generateClient", defaultValue = "false" )
102     private boolean generateClient;
103 
104     /**
105      * The files and directories to exclude from the client jar. Usage:
106      * <p/>
107      * 
108      * <pre>
109      * &lt;clientExcludes&gt;
110      * &nbsp;&nbsp;&lt;clientExclude&gt;**&#47;*Ejb.class&lt;&#47;clientExclude&gt;
111      * &nbsp;&nbsp;&lt;clientExclude&gt;**&#47;*Bean.class&lt;&#47;clientExclude&gt;
112      * &lt;&#47;clientExcludes&gt;
113      * </pre>
114      * 
115      * <br/>
116      * Attribute is used only if client jar is generated. <br/>
117      * Default exclusions: **&#47;*Bean.class, **&#47;*CMP.class, **&#47;*Session.class, **&#47;package.html
118      */
119     @Parameter
120     private List<String> clientExcludes;
121 
122     /**
123      * The files and directories to include in the client jar. Usage:
124      * <p/>
125      * 
126      * <pre>
127      * &lt;clientIncludes&gt;
128      * &nbsp;&nbsp;&lt;clientInclude&gt;**&#47;*&lt;&#47;clientInclude&gt;
129      * &lt;&#47;clientIncludes&gt;
130      * </pre>
131      * 
132      * <br/>
133      * Attribute is used only if client jar is generated. <br/>
134      * Default value: **&#47;**
135      */
136     @Parameter
137     private List<String> clientIncludes;
138 
139     /**
140      * The files and directories to exclude from the main EJB jar. Usage:
141      * <p/>
142      * 
143      * <pre>
144      * &lt;excludes&gt;
145      *   &lt;exclude&gt;**&#47;*Ejb.class&lt;&#47;exclude&gt;
146      *   &lt;exclude&gt;**&#47;*Bean.class&lt;&#47;exclude&gt;
147      * &lt;&#47;excludes&gt;
148      * </pre>
149      * 
150      * <br/>
151      * Default exclusions: META-INF&#47;ejb-jar.xml, **&#47;package.html
152      */
153     @Parameter
154     private List<String> excludes;
155 
156     /**
157      * The Maven project.
158      */
159     @Parameter( defaultValue = "${project}", readonly = true, required = true )
160     private MavenProject project;
161 
162     /**
163      * The Jar archiver.
164      */
165     @Component( role = Archiver.class, hint = "jar" )
166     private JarArchiver jarArchiver;
167 
168     /**
169      * What EJB version should the EJB Plugin generate? Valid values are "2.x" or "3.x" (where x is a digit). When
170      * ejbVersion is "3.x", the <code>ejb-jar.xml</code> file is optional.
171      * <p/>
172      * Usage:
173      * 
174      * <pre>
175      * &lt;ejbVersion&gt;3.0&lt;&#47;ejbVersion&gt;
176      * </pre>
177      *
178      * @since 2.1
179      */
180     @Parameter( property = "ejb.ejbVersion", defaultValue = "2.1" )
181     private String ejbVersion;
182 
183     /**
184      * The client Jar archiver.
185      */
186     @Component( role = Archiver.class, hint = "jar" )
187     private JarArchiver clientJarArchiver;
188 
189     /**
190      * The Maven project's helper.
191      */
192     @Component
193     private MavenProjectHelper projectHelper;
194 
195     /**
196      * The archive configuration to use. See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven
197      * Archiver Reference</a>.
198      */
199     @Parameter
200     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
201 
202     /**
203      * To escape interpolated value with windows path. c:\foo\bar will be replaced with c:\\foo\\bar.
204      *
205      * @since 2.3
206      */
207     @Parameter( property = "ejb.escapeBackslashesInFilePath", defaultValue = "false" )
208     private boolean escapeBackslashesInFilePath;
209 
210     /**
211      * An expression preceded with this String won't be interpolated. \${foo} will be replaced with ${foo}.
212      *
213      * @since 2.3
214      */
215     @Parameter( property = "ejb.escapeString" )
216     protected String escapeString;
217 
218     /**
219      * To filter the deployment descriptor.
220      *
221      * @since 2.3
222      */
223     @Parameter( property = "ejb.filterDeploymentDescriptor", defaultValue = "false" )
224     private boolean filterDeploymentDescriptor;
225 
226     /**
227      * Filters (properties files) to include during the interpolation of the deployment descriptor.
228      *
229      * @since 2.3
230      */
231     @Parameter
232     private List filters;
233 
234     /**
235      * @since 2.3
236      */
237     @Component( role = MavenFileFilter.class, hint = "default" )
238     private MavenFileFilter mavenFileFilter;
239 
240     /**
241      * @since 2.3
242      */
243     @Parameter( defaultValue = "${session}", readonly = true, required = true )
244     private MavenSession session;
245 
246     /**
247      * Generates an EJB jar and optionally an ejb-client jar.
248      *
249      * @todo Add license files in META-INF directory.
250      */
251     public void execute()
252         throws MojoExecutionException
253     {
254 
255         if ( !outputDirectory.exists() )
256         {
257             getLog().warn( "The created EJB jar will be empty cause the " + outputDirectory.getPath()
258                                + " did not exist." );
259             outputDirectory.mkdirs();
260         }
261 
262         if ( getLog().isInfoEnabled() )
263         {
264             getLog().info( "Building EJB " + jarName + " with EJB version " + ejbVersion );
265         }
266 
267         File jarFile = getEJBJarFile( basedir, jarName, classifier );
268 
269         MavenArchiver archiver = new MavenArchiver();
270 
271         archiver.setArchiver( jarArchiver );
272 
273         archiver.setOutputFile( jarFile );
274 
275         File deploymentDescriptor = new File( outputDirectory, ejbJar );
276 
277         /* test EJB version compliance */
278         if ( !ejbVersion.matches( "\\A[2-3]\\.[0-9]\\z" ) )
279         {
280             throw new MojoExecutionException( "ejbVersion is not valid: " + ejbVersion
281                 + ". Must be 2.x or 3.x (where x is a digit)" );
282         }
283 
284         if ( ejbVersion.matches( "\\A2\\.[0-9]\\z" ) && !deploymentDescriptor.exists() )
285         {
286             throw new MojoExecutionException( "Error assembling EJB: " + ejbJar + " is required for ejbVersion 2.x" );
287         }
288 
289         try
290         {
291             // TODO: This should be handled different.
292             String[] mainJarExcludes = new String[] { ejbJar, "**/package.html" };
293 
294             if ( excludes != null && !excludes.isEmpty() )
295             {
296                 excludes.add( ejbJar );
297                 mainJarExcludes = (String[]) excludes.toArray( new String[excludes.size()] );
298             }
299 
300             archiver.getArchiver().addDirectory( outputDirectory, DEFAULT_INCLUDES, mainJarExcludes );
301 
302             if ( deploymentDescriptor.exists() )
303             {
304                 // EJB-34 Filter ejb-jar.xml
305                 if ( filterDeploymentDescriptor )
306                 {
307                     getLog().debug( "Filtering deployment descriptor." );
308                     MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution();
309                     mavenResourcesExecution.setEscapeString( escapeString );
310                     List<FilterWrapper> filterWrappers =
311                         mavenFileFilter.getDefaultFilterWrappers( project, filters, escapeBackslashesInFilePath,
312                                                                   this.session, mavenResourcesExecution );
313 
314                     // Create a temporary file that we can copy-and-filter
315                     File unfilteredDeploymentDescriptor = new File( outputDirectory, ejbJar + ".unfiltered" );
316                     FileUtils.copyFile( deploymentDescriptor, unfilteredDeploymentDescriptor );
317                     mavenFileFilter.copyFile( unfilteredDeploymentDescriptor, deploymentDescriptor, true,
318                                               filterWrappers, getEncoding( unfilteredDeploymentDescriptor ) );
319                     // Remove the temporary file
320                     FileUtils.forceDelete( unfilteredDeploymentDescriptor );
321                 }
322                 archiver.getArchiver().addFile( deploymentDescriptor, ejbJar );
323             }
324 
325             // create archive
326             archiver.createArchive( session, project, archive );
327         }
328         catch ( ArchiverException e )
329         {
330             throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
331         }
332         catch ( ManifestException e )
333         {
334             throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
335         }
336         catch ( IOException e )
337         {
338             throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
339         }
340         catch ( DependencyResolutionRequiredException e )
341         {
342             throw new MojoExecutionException( "There was a problem creating the EJB archive: " + e.getMessage(), e );
343         }
344         catch ( MavenFilteringException e )
345         {
346             throw new MojoExecutionException( "There was a problem filtering the deployment descriptor: "
347                 + e.getMessage(), e );
348         }
349 
350         // Handle the classifier if necessary
351         if ( classifier != null )
352         {
353             projectHelper.attachArtifact( project, "ejb", classifier, jarFile );
354         }
355         else
356         {
357             project.getArtifact().setFile( jarFile );
358         }
359 
360         if ( generateClient )
361         {
362             String clientJarName = jarName;
363             if ( classifier != null )
364             {
365                 clientJarName += "-" + classifier;
366             }
367 
368             getLog().info( "Building EJB client " + clientJarName + "-client" );
369 
370             String[] excludes = DEFAULT_CLIENT_EXCLUDES;
371             String[] includes = DEFAULT_INCLUDES;
372 
373             if ( clientIncludes != null && !clientIncludes.isEmpty() )
374             {
375                 includes = (String[]) clientIncludes.toArray( new String[clientIncludes.size()] );
376             }
377 
378             if ( clientExcludes != null && !clientExcludes.isEmpty() )
379             {
380                 excludes = (String[]) clientExcludes.toArray( new String[clientExcludes.size()] );
381             }
382 
383             File clientJarFile = new File( basedir, clientJarName + "-client.jar" );
384 
385             MavenArchiver clientArchiver = new MavenArchiver();
386 
387             clientArchiver.setArchiver( clientJarArchiver );
388 
389             clientArchiver.setOutputFile( clientJarFile );
390 
391             try
392             {
393                 clientArchiver.getArchiver().addDirectory( outputDirectory, includes, excludes );
394 
395                 // create archive
396                 clientArchiver.createArchive( session, project, archive );
397 
398             }
399             catch ( ArchiverException e )
400             {
401                 throw new MojoExecutionException( "There was a problem creating the EJB client archive: "
402                     + e.getMessage(), e );
403             }
404             catch ( ManifestException e )
405             {
406                 throw new MojoExecutionException( "There was a problem creating the EJB client archive: "
407                     + e.getMessage(), e );
408             }
409             catch ( IOException e )
410             {
411                 throw new MojoExecutionException( "There was a problem creating the EJB client archive: "
412                     + e.getMessage(), e );
413             }
414             catch ( DependencyResolutionRequiredException e )
415             {
416                 throw new MojoExecutionException( "There was a problem creating the EJB client archive: "
417                     + e.getMessage(), e );
418             }
419 
420             // TODO: shouldn't need classifer
421             if ( classifier != null )
422             {
423                 projectHelper.attachArtifact( project, "ejb-client", classifier + "-client", clientJarFile );
424             }
425             else
426             {
427                 projectHelper.attachArtifact( project, "ejb-client", "client", clientJarFile );
428             }
429         }
430     }
431 
432     /**
433      * Returns the EJB Jar file to generate, based on an optional classifier.
434      *
435      * @param basedir the output directory
436      * @param finalName the name of the ear file
437      * @param classifier an optional classifier
438      * @return the EJB file to generate
439      */
440     private static File getEJBJarFile( File basedir, String finalName, String classifier )
441     {
442         if ( classifier == null )
443         {
444             classifier = "";
445         }
446         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
447         {
448             classifier = "-" + classifier;
449         }
450 
451         return new File( basedir, finalName + classifier + ".jar" );
452     }
453 
454     /**
455      * Get the encoding from an XML-file.
456      *
457      * @param xmlFile the XML-file
458      * @return The encoding of the XML-file, or UTF-8 if it's not specified in the file
459      * @throws IOException if an error occurred while reading the file
460      */
461     private String getEncoding( File xmlFile )
462         throws IOException
463     {
464         XmlStreamReader xmlReader = null;
465         try
466         {
467             xmlReader = new XmlStreamReader( xmlFile );
468             return xmlReader.getEncoding();
469         }
470         finally
471         {
472             IOUtils.closeQuietly( xmlReader );
473         }
474     }
475 
476 }