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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
29  import org.apache.maven.plugin.AbstractMojo;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.plugin.ear.util.ArtifactTypeMappingService;
33  import org.apache.maven.plugin.ear.util.JavaEEVersion;
34  import org.apache.maven.plugins.annotations.Component;
35  import org.apache.maven.plugins.annotations.Parameter;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.configuration.PlexusConfiguration;
38  import org.codehaus.plexus.configuration.PlexusConfigurationException;
39  
40  /**
41   * A base class for EAR-processing related tasks.
42   *
43   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
44   * @version $Id: AbstractEarMojo.java 1359710 2012-07-10 14:53:53Z tchemit $
45   */
46  public abstract class AbstractEarMojo
47      extends AbstractMojo
48  {
49      public static final String APPLICATION_XML_URI = "META-INF/application.xml";
50  
51      public static final String META_INF = "META-INF";
52  
53      public static final String UTF_8 = "UTF-8";
54  
55      /**
56       * The version of the application.xml to generate. Valid values
57       * are 1.3, 1.4, 5 and 6.
58       */
59      @Parameter( defaultValue = "1.3" )
60      protected String version;
61  
62      /**
63       * Character encoding for the auto-generated deployment file(s).
64       */
65      @Parameter( defaultValue = "UTF-8" )
66      protected String encoding;
67  
68      /**
69       * Directory where the deployment descriptor file(s) will be auto-generated.
70       */
71      @Parameter( defaultValue = "${project.build.directory}" )
72      protected String generatedDescriptorLocation;
73  
74      /**
75       * The maven project.
76       */
77      @Component
78      protected MavenProject project;
79  
80      /**
81       * The ear modules configuration.
82       */
83      @Parameter
84      private EarModule[] modules;
85  
86      /**
87       * The artifact type mappings.
88       */
89      @Parameter
90      protected PlexusConfiguration artifactTypeMappings;
91  
92      /**
93       * The default bundle dir for libraries.
94       */
95      @Parameter( alias = "defaultJavaBundleDir" )
96      protected String defaultLibBundleDir;
97  
98      /**
99       * Should libraries be added in application.xml
100      */
101     @Parameter( defaultValue = "false" )
102     private Boolean includeLibInApplicationXml = Boolean.FALSE;
103 
104     /**
105      * The file name mapping to use for all dependencies included
106      * in the EAR file.
107      */
108     @Parameter
109     private String fileNameMapping;
110 
111     /**
112      * Directory that resources are copied to during the build.
113      */
114     @Parameter( defaultValue = "${project.build.directory}/${project.build.finalName}", required = true )
115     private File workDirectory;
116 
117     /**
118      * The JBoss specific configuration.
119      *
120      * @parameter
121      */
122     @Parameter
123     private PlexusConfiguration jboss;
124 
125     /**
126      * The id to use to define the main artifact (e.g. the artifact without
127      * a classifier) when there is multiple candidates.
128      *
129      * @parameter
130      */
131     @Parameter
132     private String mainArtifactId = "none";
133 
134     private List<EarModule> earModules;
135 
136     private List<EarModule> allModules;
137 
138     private JbossConfiguration jbossConfiguration;
139 
140     @SuppressWarnings( "unchecked" )
141     public void execute()
142         throws MojoExecutionException, MojoFailureException
143     {
144         final JavaEEVersion javaEEVersion = JavaEEVersion.getJavaEEVersion( version );
145         getLog().debug( "Resolving artifact type mappings ..." );
146         ArtifactTypeMappingService typeMappingService;
147         try
148         {
149             typeMappingService = new ArtifactTypeMappingService();
150             typeMappingService.configure( artifactTypeMappings );
151         }
152         catch ( EarPluginException e )
153         {
154             throw new MojoExecutionException( "Failed to initialize artifact type mappings", e );
155         }
156         catch ( PlexusConfigurationException e )
157         {
158             throw new MojoExecutionException( "Invalid artifact type mappings configuration", e );
159         }
160 
161         getLog().debug( "Initializing JBoss configuration if necessary ..." );
162         try
163         {
164             initializeJbossConfiguration();
165         }
166         catch ( EarPluginException e )
167         {
168             throw new MojoExecutionException( "Failed to initialize JBoss configuration", e );
169         }
170 
171         getLog().debug( "Initializing ear execution context" );
172         EarExecutionContext earExecutionContext =
173             new EarExecutionContext( project, mainArtifactId, defaultLibBundleDir, jbossConfiguration, fileNameMapping,
174                                      typeMappingService );
175 
176         getLog().debug( "Resolving ear modules ..." );
177         allModules = new ArrayList<EarModule>();
178         try
179         {
180             if ( modules != null && modules.length > 0 )
181             {
182                 // Let's validate user-defined modules
183                 EarModule module = null;
184 
185                 for ( int i = 0; i < modules.length; i++ )
186                 {
187                     module = modules[i];
188                     getLog().debug( "Resolving ear module[" + module + "]" );
189                     module.setEarExecutionContext( earExecutionContext );
190                     module.resolveArtifact( project.getArtifacts() );
191                     allModules.add( module );
192                 }
193             }
194 
195             // Let's add other modules
196             Set<Artifact> artifacts = project.getArtifacts();
197             for ( Artifact artifact : artifacts )
198             {
199                 // If the artifact's type is POM, ignore and continue
200                 // since it's used for transitive deps only.
201                 if ( "pom".equals( artifact.getType() ) )
202                 {
203                     continue;
204                 }
205 
206                 // Artifact is not yet registered and it has neither test, nor a
207                 // provided scope, not is it optional
208                 ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
209                 if ( !isArtifactRegistered( artifact, allModules ) && !artifact.isOptional()
210                     && filter.include( artifact ) )
211                 {
212                     EarModule module = EarModuleFactory.newEarModule( artifact, javaEEVersion, defaultLibBundleDir,
213                                                                       includeLibInApplicationXml, typeMappingService );
214                     module.setEarExecutionContext( earExecutionContext );
215                     allModules.add( module );
216                 }
217             }
218         }
219         catch ( EarPluginException e )
220         {
221             throw new MojoExecutionException( "Failed to initialize ear modules", e );
222         }
223 
224         // Now we have everything let's built modules which have not been excluded
225         earModules = new ArrayList<EarModule>();
226         for ( EarModule earModule :  allModules )
227         {
228             if ( earModule.isExcluded() )
229             {
230                 getLog().debug( "Skipping ear module[" + earModule + "]" );
231             }
232             else
233             {
234                 earModules.add( earModule );
235             }
236         }
237 
238     }
239 
240     protected List<EarModule> getModules()
241     {
242         if ( earModules == null )
243         {
244             throw new IllegalStateException( "Ear modules have not been initialized" );
245         }
246         return earModules;
247     }
248 
249     protected MavenProject getProject()
250     {
251         return project;
252     }
253 
254     protected File getWorkDirectory()
255     {
256         return workDirectory;
257     }
258 
259     protected JbossConfiguration getJbossConfiguration()
260     {
261         return jbossConfiguration;
262     }
263 
264     private static boolean isArtifactRegistered( Artifact a, List<EarModule> currentList )
265     {
266         for( EarModule em : currentList )
267         {
268             if ( em.getArtifact().equals( a ) )
269             {
270                 return true;
271             }
272         }
273         return false;
274     }
275 
276     /**
277      * Initializes the JBoss configuration.
278      *
279      * @throws EarPluginException if the configuration is invalid
280      */
281     private void initializeJbossConfiguration()
282         throws EarPluginException
283     {
284         if ( jboss == null )
285         {
286             jbossConfiguration = null;
287         }
288         else
289         {
290             try
291             {
292                 String version = jboss.getChild( JbossConfiguration.VERSION ).getValue();
293                 if ( version == null )
294                 {
295                     getLog().info( "JBoss version not set, using JBoss 4 by default" );
296                     version = JbossConfiguration.VERSION_4;
297                 }
298                 final String securityDomain = jboss.getChild( JbossConfiguration.SECURITY_DOMAIN ).getValue();
299                 final String unauthenticatedPrincipal =
300                     jboss.getChild( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL ).getValue();
301 
302                 final PlexusConfiguration loaderRepositoryEl = jboss.getChild( JbossConfiguration.LOADER_REPOSITORY );
303                 final String loaderRepository = loaderRepositoryEl.getValue();
304                 final String loaderRepositoryClass =
305                     loaderRepositoryEl.getAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE );
306                 final PlexusConfiguration loaderRepositoryConfigEl =
307                     jboss.getChild( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
308                 final String loaderRepositoryConfig = loaderRepositoryConfigEl.getValue();
309                 final String configParserClass =
310                     loaderRepositoryConfigEl.getAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE );
311 
312                 final String jmxName = jboss.getChild( JbossConfiguration.JMX_NAME ).getValue();
313                 final String moduleOrder = jboss.getChild( JbossConfiguration.MODULE_ORDER ).getValue();
314 
315                 final List<String> dataSources = new ArrayList<String>();
316                 final PlexusConfiguration dataSourcesEl = jboss.getChild( JbossConfiguration.DATASOURCES );
317                 if ( dataSourcesEl != null )
318                 {
319 
320                     final PlexusConfiguration[] dataSourcesConfig =
321                         dataSourcesEl.getChildren( JbossConfiguration.DATASOURCE );
322                     for ( int i = 0; i < dataSourcesConfig.length; i++ )
323                     {
324                         PlexusConfiguration dataSourceConfig = dataSourcesConfig[i];
325                         dataSources.add( dataSourceConfig.getValue() );
326 
327                     }
328                 }
329                 final String libraryDirectory = jboss.getChild( JbossConfiguration.LIBRARY_DIRECTORY ).getValue();
330                 jbossConfiguration = new JbossConfiguration( version, securityDomain, unauthenticatedPrincipal, jmxName,
331                                                              loaderRepository, moduleOrder, dataSources,
332                                                              libraryDirectory, loaderRepositoryConfig,
333                                                              loaderRepositoryClass, configParserClass );
334             }
335             catch ( PlexusConfigurationException e )
336             {
337                 throw new EarPluginException( "Invalid JBoss configuration", e );
338             }
339         }
340     }
341 }