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