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