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