View Javadoc

1   package org.apache.maven.plugin.ear;
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.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugin.ear.util.JavaEEVersion;
25  import org.codehaus.plexus.configuration.PlexusConfiguration;
26  import org.codehaus.plexus.configuration.PlexusConfigurationException;
27  import org.codehaus.plexus.util.FileUtils;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * A Mojo that generates the EAR deployment descriptor file(s).
36   *
37   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
38   * @version $Id: GenerateApplicationXmlMojo.java 1050900 2010-12-19 17:10:52Z snicoll $
39   * @goal generate-application-xml
40   * @phase generate-resources
41   * @threadSafe
42   * @requiresDependencyResolution test
43   */
44  public class GenerateApplicationXmlMojo
45      extends AbstractEarMojo
46  {
47  
48  
49      /**
50       * Whether the application.xml should be generated or not.
51       *
52       * @parameter default-value="true"
53       */
54      private Boolean generateApplicationXml = Boolean.TRUE;
55  
56      /**
57       * Whether a module ID should be generated if none is specified.
58       *
59       * @parameter default-value="false"
60       */
61      private Boolean generateModuleId = Boolean.FALSE;
62  
63      /**
64       * Application name of the application to be used when the application.xml
65       * file is auto-generated. Since JavaEE6.
66       *
67       * @parameter
68       */
69      private String applicationName;
70  
71      /**
72       * Display name of the application to be used when the application.xml
73       * file is auto-generated.
74       *
75       * @parameter default-value="${project.artifactId}"
76       */
77      private String displayName;
78  
79      /**
80       * Description of the application to be used when the application.xml
81       * file is auto-generated.
82       *
83       * @parameter default-value="${project.description}"
84       */
85      private String description;
86  
87      /**
88       * Defines the value of the initialize in order element to be used when
89       * the application.xml file is auto-generated. When set to true, modules
90       * must be initialized in the order they're listed in this deployment descriptor,
91       * with the exception of application client modules, which can be
92       * initialized in any order. If initialize-in-order is not set or set to
93       * false, the order of initialization is unspecified and may be
94       * product-dependent. Since JavaEE6.
95       *
96       * @parameter
97       */
98      private Boolean initializeInOrder;
99  
100     /**
101      * The security-roles to be added to the auto-generated
102      * application.xml file.
103      *
104      * @parameter
105      */
106     private PlexusConfiguration security;
107 
108 
109     public void execute()
110         throws MojoExecutionException, MojoFailureException
111     {
112         // Initializes ear modules
113         super.execute();
114 
115         // Handle application.xml
116         if ( !generateApplicationXml.booleanValue() )
117         {
118             getLog().debug( "Generation of application.xml is disabled" );
119         }
120         else
121         {
122             final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
123 
124             // Generate deployment descriptor and copy it to the build directory
125             getLog().info( "Generating application.xml" );
126             try
127             {
128                 generateStandardDeploymentDescriptor( javaEEVersion );
129             }
130             catch ( EarPluginException e )
131             {
132                 throw new MojoExecutionException( "Failed to generate application.xml", e );
133             }
134 
135             try
136             {
137                 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "application.xml" ),
138                                                new File( getWorkDirectory(), "META-INF" ) );
139             }
140             catch ( IOException e )
141             {
142                 throw new MojoExecutionException( "Unable to copy application.xml to final destination", e );
143             }
144         }
145 
146         // Handle jboss-app.xml
147         if ( getJbossConfiguration() == null )
148         {
149             getLog().debug( "Generation of jboss-app.xml is disabled" );
150             return;
151         }
152         else
153         {
154             // Generate deployment descriptor and copy it to the build directory
155             getLog().info( "Generating jboss-app.xml" );
156             try
157             {
158                 generateJbossDeploymentDescriptor();
159             }
160             catch ( EarPluginException e )
161             {
162                 throw new MojoExecutionException( "Failed to generate jboss-app.xml", e );
163             }
164 
165             try
166             {
167                 FileUtils.copyFileToDirectory( new File( generatedDescriptorLocation, "jboss-app.xml" ),
168                                                new File( getWorkDirectory(), "META-INF" ) );
169             }
170             catch ( IOException e )
171             {
172                 throw new MojoExecutionException( "Unable to copy jboss-app.xml to final destination", e );
173             }
174         }
175     }
176 
177     /**
178      * Generates the deployment descriptor.
179      */
180     protected void generateStandardDeploymentDescriptor( JavaEEVersion javaEEVersion )
181         throws EarPluginException
182     {
183         File outputDir = new File( generatedDescriptorLocation );
184         if ( !outputDir.exists() )
185         {
186             outputDir.mkdirs();
187         }
188 
189         File descriptor = new File( outputDir, "application.xml" );
190 
191         final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId );
192         final ApplicationXmlWriterContext context =
193             new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), displayName, description,
194                                              defaultLibBundleDir, applicationName, initializeInOrder );
195         writer.write( context );
196     }
197 
198     /**
199      * Generates the jboss deployment descriptor.
200      */
201     protected void generateJbossDeploymentDescriptor()
202         throws EarPluginException
203     {
204         File outputDir = new File( generatedDescriptorLocation );
205         if ( !outputDir.exists() )
206         {
207             outputDir.mkdirs();
208         }
209 
210         File descriptor = new File( outputDir, "jboss-app.xml" );
211 
212         JbossAppXmlWriter writer = new JbossAppXmlWriter( encoding );
213         writer.write( descriptor, getJbossConfiguration(), getModules() );
214     }
215 
216     /**
217      * Builds the security roles based on the configuration.
218      *
219      * @return a list of SecurityRole object(s)
220      * @throws EarPluginException if the configuration is invalid
221      */
222     private List buildSecurityRoles()
223         throws EarPluginException
224     {
225         final List result = new ArrayList();
226         if ( security == null )
227         {
228             return result;
229         }
230         try
231         {
232             final PlexusConfiguration[] securityRoles = security.getChildren( SecurityRole.SECURITY_ROLE );
233 
234             for ( int i = 0; i < securityRoles.length; i++ )
235             {
236                 PlexusConfiguration securityRole = securityRoles[i];
237                 final String id = securityRole.getAttribute( SecurityRole.ID_ATTRIBUTE );
238                 final String roleName = securityRole.getChild( SecurityRole.ROLE_NAME ).getValue();
239                 final String roleNameId =
240                     securityRole.getChild( SecurityRole.ROLE_NAME ).getAttribute( SecurityRole.ID_ATTRIBUTE );
241                 final String description = securityRole.getChild( SecurityRole.DESCRIPTION ).getValue();
242                 final String descriptionId =
243                     securityRole.getChild( SecurityRole.DESCRIPTION ).getAttribute( SecurityRole.ID_ATTRIBUTE );
244 
245                 if ( roleName == null )
246                 {
247                     throw new EarPluginException( "Invalid security-role configuration, role-name could not be null." );
248                 }
249                 else
250                 {
251                     result.add( new SecurityRole( roleName, roleNameId, id, description, descriptionId ) );
252                 }
253             }
254             return result;
255         }
256         catch ( PlexusConfigurationException e )
257         {
258             throw new EarPluginException( "Invalid security-role configuration", e );
259         }
260 
261     }
262 }