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