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