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 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   * Build an EJB (and optional client) from the current project.
39   *
40   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
41   * @version $Id: EjbMojo.java 924415 2010-03-17 18:37:43Z bentmann $
42   * @goal ejb
43   * @requiresDependencyResolution runtime
44   * @phase package
45   */
46  public class EjbMojo
47      extends AbstractMojo
48  {
49      private static final String EJB_JAR_XML = "META-INF/ejb-jar.xml";
50  
51      // TODO: will null work instead?
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       * The directory for the generated EJB.
63       *
64       * @parameter default-value="${project.build.directory}"
65       * @required
66       * @readonly
67       */
68      private File basedir;
69  
70      /**
71       * Directory that resources are copied to during the build.
72       *
73       * @parameter default-value="${project.build.outputDirectory}" expression="${outputDirectory}"
74       */
75      private File outputDirectory;
76  
77      /**
78       * The name of the EJB file to generate.
79       *
80       * @parameter default-value="${project.build.finalName}" expression="${jarName}"
81       */
82      private String jarName;
83  
84      /**
85       * Classifier to add to the artifact generated. If given, the artifact will
86       * be an attachment instead.
87       *
88       * @parameter expression="${ejb.classifier}"
89       */
90      private String classifier;
91  
92      /**
93       * Whether the EJB client jar should be generated or not.
94       *
95       * @parameter default-value="false" expression="${ejb.generateClient}"
96       */
97      private boolean generateClient;
98  
99      /**
100      * The files and directories to exclude from the client jar. Usage:
101      *
102      * <pre>
103      * &lt;clientExcludes&gt;
104      * &nbsp;&nbsp;&lt;clientExclude&gt;**&#47;*Ejb.class&lt;&#47;clientExclude&gt;
105      * &nbsp;&nbsp;&lt;clientExclude&gt;**&#47;*Bean.class&lt;&#47;clientExclude&gt;
106      * &lt;&#47;clientExcludes&gt;
107      * </pre>
108      * <br/>Attribute is used only if client jar is generated.
109      * <br/>Default exclusions: **&#47;*Bean.class, **&#47;*CMP.class, **&#47;*Session.class, **&#47;package.html
110      *
111      * @parameter
112      */
113     private List clientExcludes;
114 
115     /**
116      * The files and directories to include in the client jar. Usage:
117      *
118      * <pre>
119      * &lt;clientIncludes&gt;
120      * &nbsp;&nbsp;&lt;clientInclude&gt;**&#47;*&lt;&#47;clientInclude&gt;
121      * &lt;&#47;clientIncludes&gt;
122      * </pre>
123      * <br/>Attribute is used only if client jar is generated.
124      * <br/>Default value: **&#47;**
125      *
126      * @parameter
127      */
128     private List clientIncludes;
129 
130     /**
131      * The files and directories to exclude from the main EJB jar. Usage:
132      *
133      * <pre>
134      * &lt;excludes&gt;
135      *   &lt;exclude&gt;**&#47;*Ejb.class&lt;&#47;exclude&gt;
136      *   &lt;exclude&gt;**&#47;*Bean.class&lt;&#47;exclude&gt;
137      * &lt;&#47;excludes&gt;
138      * </pre>
139      * <br/>Default exclusions: META-INF&#47;ejb-jar.xml, **&#47;package.html
140      * @parameter
141      */
142     private List excludes;
143 
144     /**
145      * The Maven project.
146      *
147      * @parameter default-value="${project}"
148      * @required
149      * @readonly
150      */
151     private MavenProject project;
152 
153     /**
154      * The Jar archiver.
155      *
156      * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
157      */
158     private JarArchiver jarArchiver;
159 
160     /**
161      * What EJB version should the EJB Plugin generate? Valid values are "2.x" or "3.x"
162      * (where x is a digit).  When ejbVersion is "3.x", the
163      * <code>ejb-jar.xml</code> file is optional.
164      * <p/>
165      * Usage:
166      * <pre>
167      * &lt;ejbVersion&gt;3.0&lt;&#47;ejbVersion&gt;
168      * </pre>
169      *
170      * @parameter default-value="2.1" expression="${ejb.ejbVersion}"
171      * @since 2.1
172      */
173     private String ejbVersion;
174 
175     /**
176      * The client Jar archiver.
177      *
178      * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
179      */
180     private JarArchiver clientJarArchiver;
181 
182     /**
183      * The Maven project's helper.
184      *
185      * @component
186      */
187     private MavenProjectHelper projectHelper;
188 
189     /**
190      * The archive configuration to use.
191      * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
192      * This version of the EJB Plugin uses Maven Archiver 2.4.
193      *
194      * @parameter
195      */
196     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
197 
198     /**
199      * Generates an EJB jar and optionally an ejb-client jar.
200      *
201      * @todo Add license files in META-INF directory.
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         /* test EJB version compliance */
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             // create archive
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         // Handle the classifier if necessary
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                 // create archive
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             // TODO: shouldn't need classifer
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      * Returns the EJB Jar file to generate, based on an optional classifier.
355      *
356      * @param basedir    the output directory
357      * @param finalName  the name of the ear file
358      * @param classifier an optional classifier
359      * @return the EJB file to generate
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 }