Coverage Report - org.apache.maven.archetype.creator.FilesetArchetypeCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
FilesetArchetypeCreator
78 %
571/731
50 %
180/356
6,167
 
 1  
 package org.apache.maven.archetype.creator;
 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.archetype.ArchetypeCreationRequest;
 23  
 import org.apache.maven.archetype.ArchetypeCreationResult;
 24  
 import org.apache.maven.archetype.common.ArchetypeFilesResolver;
 25  
 import org.apache.maven.archetype.common.Constants;
 26  
 import org.apache.maven.archetype.common.PomManager;
 27  
 import org.apache.maven.archetype.common.util.FileCharsetDetector;
 28  
 import org.apache.maven.archetype.common.util.ListScanner;
 29  
 import org.apache.maven.archetype.common.util.PathUtils;
 30  
 import org.apache.maven.archetype.exception.TemplateCreationException;
 31  
 import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
 32  
 import org.apache.maven.archetype.metadata.FileSet;
 33  
 import org.apache.maven.archetype.metadata.ModuleDescriptor;
 34  
 import org.apache.maven.archetype.metadata.RequiredProperty;
 35  
 import org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Writer;
 36  
 import org.apache.maven.artifact.Artifact;
 37  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 38  
 import org.apache.maven.model.Build;
 39  
 import org.apache.maven.model.Dependency;
 40  
 import org.apache.maven.model.Extension;
 41  
 import org.apache.maven.model.Model;
 42  
 import org.apache.maven.model.Plugin;
 43  
 import org.apache.maven.model.PluginManagement;
 44  
 import org.apache.maven.model.Profile;
 45  
 import org.apache.maven.project.MavenProject;
 46  
 import org.apache.maven.project.MavenProjectBuilder;
 47  
 import org.apache.maven.project.ProjectBuildingException;
 48  
 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
 49  
 import org.apache.maven.shared.invoker.DefaultInvoker;
 50  
 import org.apache.maven.shared.invoker.InvocationRequest;
 51  
 import org.apache.maven.shared.invoker.Invoker;
 52  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 53  
 import org.codehaus.plexus.util.DirectoryScanner;
 54  
 import org.codehaus.plexus.util.FileUtils;
 55  
 import org.codehaus.plexus.util.IOUtil;
 56  
 import org.codehaus.plexus.util.ReaderFactory;
 57  
 import org.codehaus.plexus.util.StringUtils;
 58  
 import org.codehaus.plexus.util.WriterFactory;
 59  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 60  
 
 61  
 import java.io.File;
 62  
 import java.io.FileInputStream;
 63  
 import java.io.FileNotFoundException;
 64  
 import java.io.FileOutputStream;
 65  
 import java.io.IOException;
 66  
 import java.io.InputStream;
 67  
 import java.io.OutputStream;
 68  
 import java.io.Reader;
 69  
 import java.io.Writer;
 70  
 import java.util.ArrayList;
 71  
 import java.util.Arrays;
 72  
 import java.util.Collections;
 73  
 import java.util.HashMap;
 74  
 import java.util.HashSet;
 75  
 import java.util.Iterator;
 76  
 import java.util.List;
 77  
 import java.util.Map;
 78  
 import java.util.Properties;
 79  
 import java.util.Set;
 80  
 
 81  
 /**
 82  
  * Create a 2.x Archetype project from a project. Since 2.0-alpha-5, an integration-test named "basic" is created along
 83  
  * the archetype itself to provide immediate test when building the archetype.
 84  
  *
 85  
  *  @plexus.component role-hint="fileset"
 86  
  */
 87  9
 public class FilesetArchetypeCreator
 88  
     extends AbstractLogEnabled
 89  
     implements ArchetypeCreator
 90  
 {
 91  1
     private static final String DEFAULT_OUTPUT_DIRECTORY = "target" + File.separator + "generated-sources"
 92  
         + File.separator + "archetype";
 93  
 
 94  
     /** @plexus.requirement */
 95  
     private ArchetypeFilesResolver archetypeFilesResolver;
 96  
 
 97  
     /** @plexus.requirement */
 98  
     private PomManager pomManager;
 99  
 
 100  
     /** @plexus.requirement */
 101  
     private MavenProjectBuilder projectBuilder;
 102  
 
 103  
     public void createArchetype( ArchetypeCreationRequest request, ArchetypeCreationResult result )
 104  
     {
 105  6
         MavenProject project = request.getProject();
 106  6
         List<String> languages = request.getLanguages();
 107  6
         List<String> filtereds = request.getFiltereds();
 108  6
         String defaultEncoding = request.getDefaultEncoding();
 109  6
         boolean preserveCData = request.isPreserveCData();
 110  6
         boolean keepParent = request.isKeepParent();
 111  6
         boolean partialArchetype = request.isPartialArchetype();
 112  6
         ArtifactRepository localRepository = request.getLocalRepository();
 113  6
         File outputDirectory = request.getOutputDirectory();
 114  6
         File basedir = project.getBasedir();
 115  
 
 116  6
         Properties properties = new Properties();
 117  6
         Properties configurationProperties = new Properties();
 118  6
         if ( request.getProperties() != null )
 119  
         {
 120  4
             properties.putAll( request.getProperties() );
 121  4
             configurationProperties.putAll( request.getProperties() );
 122  
         }
 123  
 
 124  6
         extractPropertiesFromProject( project, properties, configurationProperties, request.getPackageName() );
 125  
 
 126  6
         if ( outputDirectory == null )
 127  
         {
 128  6
             getLogger().debug( "No output directory defined, using default: " + DEFAULT_OUTPUT_DIRECTORY );
 129  6
             outputDirectory = FileUtils.resolveFile( basedir, DEFAULT_OUTPUT_DIRECTORY );
 130  
         }
 131  6
         outputDirectory.mkdirs();
 132  
 
 133  6
         getLogger().debug( "Creating archetype in " + outputDirectory );
 134  
 
 135  
         try
 136  
         {
 137  6
             File archetypePomFile =
 138  
                 createArchetypeProjectPom( project, localRepository, configurationProperties, outputDirectory );
 139  
 
 140  6
             File archetypeResourcesDirectory = new File( outputDirectory, getTemplateOutputDirectory() );
 141  
 
 142  6
             File archetypeFilesDirectory = new File( archetypeResourcesDirectory, Constants.ARCHETYPE_RESOURCES );
 143  6
             getLogger().debug( "Archetype's files output directory " + archetypeFilesDirectory );
 144  
 
 145  6
             File archetypeDescriptorFile = new File( archetypeResourcesDirectory, Constants.ARCHETYPE_DESCRIPTOR );
 146  6
             archetypeDescriptorFile.getParentFile().mkdirs();
 147  
 
 148  6
             getLogger().debug( "Starting archetype's descriptor " + project.getArtifactId() );
 149  6
             ArchetypeDescriptor archetypeDescriptor = new ArchetypeDescriptor();
 150  
 
 151  6
             archetypeDescriptor.setName( project.getArtifactId() );
 152  6
             archetypeDescriptor.setPartial( partialArchetype );
 153  
 
 154  6
             addRequiredProperties( archetypeDescriptor, properties );
 155  
 
 156  
             // TODO ensure reverseProperties contains NO dotted properties
 157  6
             Properties reverseProperties = getReversedProperties( archetypeDescriptor, properties );
 158  
             // reverseProperties.remove( Constants.GROUP_ID );
 159  
 
 160  
             // TODO ensure pomReversedProperties contains NO dotted properties
 161  6
             Properties pomReversedProperties = getReversedProperties( archetypeDescriptor, properties );
 162  
             // pomReversedProperties.remove( Constants.PACKAGE );
 163  
 
 164  6
             String packageName = configurationProperties.getProperty( Constants.PACKAGE );
 165  
 
 166  6
             Model pom = pomManager.readPom( FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ) );
 167  
 
 168  6
             List<String> fileNames = resolveFileNames( pom, basedir );
 169  6
             if ( getLogger().isDebugEnabled() )
 170  
             {
 171  0
                 getLogger().debug( "Scanned for files " + fileNames.size() );
 172  
 
 173  0
                 for ( String name : fileNames )
 174  
                 {
 175  0
                     getLogger().debug( "- " + name );
 176  
                 }
 177  
             }
 178  
 
 179  6
             List<FileSet> filesets = resolveFileSets( packageName, fileNames, languages, filtereds, defaultEncoding );
 180  6
             getLogger().debug( "Resolved filesets for " + archetypeDescriptor.getName() );
 181  
 
 182  6
             archetypeDescriptor.setFileSets( filesets );
 183  
 
 184  6
             createArchetypeFiles( reverseProperties, filesets, packageName, basedir, archetypeFilesDirectory,
 185  
                                   defaultEncoding );
 186  6
             getLogger().debug( "Created files for " + archetypeDescriptor.getName() );
 187  
 
 188  6
             setParentArtifactId( reverseProperties, configurationProperties.getProperty( Constants.ARTIFACT_ID ) );
 189  
 
 190  6
             for ( Iterator<String> modules = pom.getModules().iterator(); modules.hasNext(); )
 191  
             {
 192  8
                 String moduleId = (String) modules.next();
 193  8
                 String rootArtifactId = configurationProperties.getProperty( Constants.ARTIFACT_ID );
 194  8
                 String moduleIdDirectory = moduleId;
 195  
 
 196  8
                 if ( moduleId.indexOf( rootArtifactId ) >= 0 )
 197  
                 {
 198  5
                     moduleIdDirectory = StringUtils.replace( moduleId, rootArtifactId, "__rootArtifactId__" );
 199  
                 }
 200  
 
 201  8
                 getLogger().debug( "Creating module " + moduleId );
 202  
 
 203  8
                 ModuleDescriptor moduleDescriptor =
 204  
                     createModule( reverseProperties, rootArtifactId, moduleId, packageName,
 205  
                                   FileUtils.resolveFile( basedir, moduleId ),
 206  
                                   new File( archetypeFilesDirectory, moduleIdDirectory ), languages,
 207  
                                   filtereds, defaultEncoding, preserveCData, keepParent );
 208  
 
 209  8
                 archetypeDescriptor.addModule( moduleDescriptor );
 210  
 
 211  8
                 getLogger().debug(
 212  
                                    "Added module " + moduleDescriptor.getName() + " in "
 213  
                                        + archetypeDescriptor.getName() );
 214  8
             }
 215  
 
 216  6
             restoreParentArtifactId( reverseProperties, null );
 217  6
             restoreArtifactId( reverseProperties, configurationProperties.getProperty( Constants.ARTIFACT_ID ) );
 218  
 
 219  6
             createPoms( pom, configurationProperties.getProperty( Constants.ARTIFACT_ID ),
 220  
                         configurationProperties.getProperty( Constants.ARTIFACT_ID ), archetypeFilesDirectory, basedir,
 221  
                         pomReversedProperties, preserveCData, keepParent );
 222  6
             getLogger().debug( "Created Archetype " + archetypeDescriptor.getName() + " template pom(s)" );
 223  
 
 224  6
             Writer out = null;
 225  
             try
 226  
             {
 227  6
                 out = WriterFactory.newXmlWriter( archetypeDescriptorFile );
 228  
 
 229  6
                 ArchetypeDescriptorXpp3Writer writer = new ArchetypeDescriptorXpp3Writer();
 230  
 
 231  6
                 writer.write( out, archetypeDescriptor );
 232  
 
 233  6
                 getLogger().debug( "Archetype " + archetypeDescriptor.getName() + " descriptor written" );
 234  
             }
 235  
             finally
 236  
             {
 237  6
                 IOUtil.close( out );
 238  6
             }
 239  
 
 240  6
             createArchetypeBasicIt( archetypeDescriptor, outputDirectory );
 241  
 
 242  6
             InvocationRequest internalRequest = new DefaultInvocationRequest();
 243  6
             internalRequest.setPomFile( archetypePomFile );
 244  6
             internalRequest.setGoals( Collections.singletonList( request.getPostPhase() ) );
 245  
 
 246  6
             Invoker invoker = new DefaultInvoker();
 247  6
             invoker.execute( internalRequest );
 248  
         }
 249  0
         catch ( Exception e )
 250  
         {
 251  0
             result.setCause( e );
 252  6
         }
 253  6
     }
 254  
 
 255  
     /**
 256  
      * Create an archetype IT, ie goals.txt and archetype.properties in src/test/resources/projects/basic.
 257  
      *
 258  
      * @param archetypeDescriptor
 259  
      * @param generatedSourcesDirectory
 260  
      * @throws IOException
 261  
      *
 262  
      * @since 2.0-alpha-5
 263  
      */
 264  
     private void createArchetypeBasicIt( ArchetypeDescriptor archetypeDescriptor, File generatedSourcesDirectory )
 265  
         throws IOException
 266  
     {
 267  6
         String basic = Constants.SRC + File.separator + Constants.TEST + File.separator + Constants.RESOURCES
 268  
             + File.separator + "projects" + File.separator + "basic";
 269  6
         File basicItDirectory = new File( generatedSourcesDirectory, basic );
 270  6
         basicItDirectory.mkdirs();
 271  
 
 272  6
         InputStream in = null;
 273  6
         OutputStream out = null;
 274  
 
 275  
         try
 276  
         {
 277  6
             in = FilesetArchetypeCreator.class.getResourceAsStream( "archetype.properties" );
 278  
 
 279  6
             Properties archetypeProperties = new Properties();
 280  6
             archetypeProperties.load( in );
 281  
 
 282  6
             for ( RequiredProperty req : archetypeDescriptor.getRequiredProperties() )
 283  
             {
 284  4
                 archetypeProperties.put( req.getKey(), req.getDefaultValue() );
 285  
             }
 286  
 
 287  6
             out = new FileOutputStream( new File( basicItDirectory, "archetype.properties" ) );
 288  6
             archetypeProperties.store( out, null );
 289  
         }
 290  
         finally
 291  
         {
 292  6
             IOUtil.close( in );
 293  6
             IOUtil.close( out );
 294  6
         }
 295  6
         copyResource( "goal.txt", new File( basicItDirectory, "goal.txt" ) );
 296  
 
 297  6
         getLogger().debug( "Added basic integration test" );
 298  6
     }
 299  
 
 300  
     private void extractPropertiesFromProject( MavenProject project, Properties properties,
 301  
                                                Properties configurationProperties, String packageName )
 302  
     {
 303  6
         if ( !properties.containsKey( Constants.GROUP_ID ) )
 304  
         {
 305  3
             properties.setProperty( Constants.GROUP_ID, project.getGroupId() );
 306  
         }
 307  6
         configurationProperties.setProperty( Constants.GROUP_ID, properties.getProperty( Constants.GROUP_ID ) );
 308  
 
 309  6
         if ( !properties.containsKey( Constants.ARTIFACT_ID ) )
 310  
         {
 311  3
             properties.setProperty( Constants.ARTIFACT_ID, project.getArtifactId() );
 312  
         }
 313  6
         configurationProperties.setProperty( Constants.ARTIFACT_ID, properties.getProperty( Constants.ARTIFACT_ID ) );
 314  
 
 315  6
         if ( !properties.containsKey( Constants.VERSION ) )
 316  
         {
 317  3
             properties.setProperty( Constants.VERSION, project.getVersion() );
 318  
         }
 319  6
         configurationProperties.setProperty( Constants.VERSION, properties.getProperty( Constants.VERSION ) );
 320  
 
 321  6
         if ( packageName != null )
 322  
         {
 323  3
             properties.setProperty( Constants.PACKAGE, packageName );
 324  
         }
 325  3
         else if ( !properties.containsKey( Constants.PACKAGE ) )
 326  
         {
 327  3
             properties.setProperty( Constants.PACKAGE, project.getGroupId() );
 328  
         }
 329  6
         configurationProperties.setProperty( Constants.PACKAGE, properties.getProperty( Constants.PACKAGE ) );
 330  6
     }
 331  
 
 332  
     /**
 333  
      * Create the archetype project pom.xml file, that will be used to build the archetype.
 334  
      */
 335  
     private File createArchetypeProjectPom( MavenProject project, ArtifactRepository localRepository,
 336  
                                             Properties configurationProperties, File projectDir )
 337  
         throws TemplateCreationException, IOException
 338  
     {
 339  6
         Model model = new Model();
 340  6
         model.setModelVersion( "4.0.0" );
 341  
         // these values should be retrieved from the request with sensible defaults
 342  6
         model.setGroupId( configurationProperties.getProperty( Constants.ARCHETYPE_GROUP_ID, project.getGroupId() ) );
 343  6
         model.setArtifactId( configurationProperties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID,
 344  
                                                                   project.getArtifactId() ) );
 345  6
         model.setVersion( configurationProperties.getProperty( Constants.ARCHETYPE_VERSION, project.getVersion() ) );
 346  6
         model.setPackaging( "maven-archetype" );
 347  6
         model.setName( configurationProperties.getProperty( Constants.ARCHETYPE_ARTIFACT_ID, project.getArtifactId() ) );
 348  6
         model.setUrl( configurationProperties.getProperty( Constants.ARCHETYPE_URL, project.getUrl() ) );
 349  6
         model.setDescription( configurationProperties.getProperty( Constants.ARCHETYPE_DESCRIPTION,
 350  
                                                                    project.getDescription() ) );
 351  6
         model.setLicenses( project.getLicenses() );
 352  6
         model.setDevelopers( project.getDevelopers() );
 353  6
         model.setScm( project.getScm() );
 354  6
         Build build = new Build();
 355  6
         model.setBuild( build );
 356  
 
 357  
         // In many cases where we are behind a firewall making Archetypes for work mates we want
 358  
         // to simply be able to deploy the archetypes once we have created them. In order to do
 359  
         // this we want to utilize information from the project we are creating the archetype from.
 360  
         // This will be a fully working project that has been testing and inherits from a POM
 361  
         // that contains deployment information, along with any extensions required for deployment.
 362  
         // We don't want to create archetypes that cannot be deployed after we create them. People
 363  
         // might want to edit the archetype POM but they should not have too.
 364  
 
 365  6
         if ( project.getParent() != null )
 366  
         {
 367  0
             Artifact pa = project.getParentArtifact();
 368  
 
 369  
             try
 370  
             {
 371  0
                 MavenProject p =
 372  
                     projectBuilder.buildFromRepository( pa, project.getRemoteArtifactRepositories(), localRepository );
 373  
 
 374  0
                 if ( p.getDistributionManagement() != null )
 375  
                 {
 376  0
                     model.setDistributionManagement( p.getDistributionManagement() );
 377  
                 }
 378  
 
 379  0
                 if ( p.getBuildExtensions() != null )
 380  
                 {
 381  0
                     for ( Iterator<Extension> i = p.getBuildExtensions().iterator(); i.hasNext(); )
 382  
                     {
 383  0
                         Extension be = i.next();
 384  
 
 385  0
                         model.getBuild().addExtension( be );
 386  0
                     }
 387  
                 }
 388  
             }
 389  0
             catch ( ProjectBuildingException e )
 390  
             {
 391  0
                 throw new TemplateCreationException( "Error reading parent POM of project: " + pa.getGroupId() + ":"
 392  
                     + pa.getArtifactId() + ":" + pa.getVersion() );
 393  0
             }
 394  
         }
 395  
 
 396  6
         Extension extension = new Extension();
 397  6
         extension.setGroupId( "org.apache.maven.archetype" );
 398  6
         extension.setArtifactId( "archetype-packaging" );
 399  6
         extension.setVersion( getArchetypeVersion() );
 400  6
         model.getBuild().addExtension( extension );
 401  
 
 402  6
         Plugin plugin = new Plugin();
 403  6
         plugin.setGroupId( "org.apache.maven.plugins" );
 404  6
         plugin.setArtifactId( "maven-archetype-plugin" );
 405  6
         plugin.setVersion( getArchetypeVersion() );
 406  
 
 407  6
         PluginManagement pluginManagement = new PluginManagement();
 408  6
         pluginManagement.addPlugin( plugin );
 409  6
         model.getBuild().setPluginManagement( pluginManagement );
 410  
 
 411  6
         getLogger().debug( "Creating archetype's pom" );
 412  
 
 413  6
         File archetypePomFile = new File( projectDir, Constants.ARCHETYPE_POM );
 414  
 
 415  6
         archetypePomFile.getParentFile().mkdirs();
 416  
 
 417  6
         copyResource( "pom-prototype.xml", archetypePomFile );
 418  
 
 419  6
         pomManager.writePom( model, archetypePomFile, archetypePomFile );
 420  
 
 421  6
         return archetypePomFile;
 422  
     }
 423  
 
 424  
     private void copyResource( String name, File destination )
 425  
         throws IOException
 426  
     {
 427  12
         InputStream in = null;
 428  12
         OutputStream out = null;
 429  
 
 430  
         try
 431  
         {
 432  12
             in = FilesetArchetypeCreator.class.getResourceAsStream( name );
 433  12
             out = new FileOutputStream( destination );
 434  
 
 435  12
             IOUtil.copy( in, out );
 436  
         }
 437  
         finally
 438  
         {
 439  12
             IOUtil.close( in );
 440  12
             IOUtil.close( out );
 441  12
         }
 442  12
     }
 443  
 
 444  
     private void addRequiredProperties( ArchetypeDescriptor archetypeDescriptor, Properties properties )
 445  
     {
 446  6
         Properties requiredProperties = new Properties();
 447  6
         requiredProperties.putAll( properties );
 448  6
         requiredProperties.remove( Constants.ARCHETYPE_GROUP_ID );
 449  6
         requiredProperties.remove( Constants.ARCHETYPE_ARTIFACT_ID );
 450  6
         requiredProperties.remove( Constants.ARCHETYPE_VERSION );
 451  6
         requiredProperties.remove( Constants.GROUP_ID );
 452  6
         requiredProperties.remove( Constants.ARTIFACT_ID );
 453  6
         requiredProperties.remove( Constants.VERSION );
 454  6
         requiredProperties.remove( Constants.PACKAGE );
 455  
 
 456  6
         for ( Iterator<?> propertiesIterator = requiredProperties.keySet().iterator(); propertiesIterator.hasNext(); )
 457  
         {
 458  4
             String propertyKey = (String) propertiesIterator.next();
 459  
 
 460  4
             RequiredProperty requiredProperty = new RequiredProperty();
 461  4
             requiredProperty.setKey( propertyKey );
 462  4
             requiredProperty.setDefaultValue( requiredProperties.getProperty( propertyKey ) );
 463  
 
 464  4
             archetypeDescriptor.addRequiredProperty( requiredProperty );
 465  
 
 466  4
             getLogger().debug(
 467  
                                "Adding requiredProperty " + propertyKey + "="
 468  
                                    + requiredProperties.getProperty( propertyKey ) + " to archetype's descriptor" );
 469  4
         }
 470  6
     }
 471  
 
 472  
     private void createModulePoms( Properties pomReversedProperties, String rootArtifactId, String packageName,
 473  
                                    File basedir, File archetypeFilesDirectory, boolean preserveCData, boolean keepParent )
 474  
         throws FileNotFoundException, IOException, XmlPullParserException
 475  
     {
 476  12
         Model pom = pomManager.readPom( FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ) );
 477  
 
 478  12
         String parentArtifactId = pomReversedProperties.getProperty( Constants.PARENT_ARTIFACT_ID );
 479  12
         String artifactId = pom.getArtifactId();
 480  12
         setParentArtifactId( pomReversedProperties, pomReversedProperties.getProperty( Constants.ARTIFACT_ID ) );
 481  12
         setArtifactId( pomReversedProperties, pom.getArtifactId() );
 482  
 
 483  12
         for ( Iterator<String> modules = pom.getModules().iterator(); modules.hasNext(); )
 484  
         {
 485  4
             String subModuleId = modules.next();
 486  
 
 487  4
             String subModuleIdDirectory = subModuleId;
 488  
 
 489  4
             if ( subModuleId.indexOf( rootArtifactId ) >= 0 )
 490  
             {
 491  2
                 subModuleIdDirectory = StringUtils.replace( subModuleId, rootArtifactId, "__rootArtifactId__" );
 492  
             }
 493  
 
 494  4
             createModulePoms( pomReversedProperties, rootArtifactId, packageName,
 495  
                               FileUtils.resolveFile( basedir, subModuleId ),
 496  
                               FileUtils.resolveFile( archetypeFilesDirectory, subModuleIdDirectory ),
 497  
                               preserveCData, keepParent );
 498  4
         }
 499  
 
 500  12
         createModulePom( pom, rootArtifactId, archetypeFilesDirectory, pomReversedProperties,
 501  
                          FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ), preserveCData, keepParent );
 502  
 
 503  12
         restoreParentArtifactId( pomReversedProperties, parentArtifactId );
 504  12
         restoreArtifactId( pomReversedProperties, artifactId );
 505  12
     }
 506  
 
 507  
     private void createPoms( Model pom, String rootArtifactId, String artifactId, File archetypeFilesDirectory,
 508  
                              File basedir, Properties pomReversedProperties, boolean preserveCData, boolean keepParent )
 509  
         throws IOException, FileNotFoundException, XmlPullParserException
 510  
     {
 511  6
         setArtifactId( pomReversedProperties, pom.getArtifactId() );
 512  
 
 513  6
         for ( Iterator<String> modules = pom.getModules().iterator(); modules.hasNext(); )
 514  
         {
 515  8
             String moduleId = modules.next();
 516  
 
 517  8
             String moduleIdDirectory = moduleId;
 518  
 
 519  8
             if ( moduleId.indexOf( rootArtifactId ) >= 0 )
 520  
             {
 521  5
                 moduleIdDirectory = StringUtils.replace( moduleId, rootArtifactId, "__rootArtifactId__" );
 522  
             }
 523  
 
 524  8
             createModulePoms( pomReversedProperties, rootArtifactId, moduleId,
 525  
                               FileUtils.resolveFile( basedir, moduleId ),
 526  
                               new File( archetypeFilesDirectory, moduleIdDirectory ),
 527  
                               preserveCData, keepParent );
 528  8
         }
 529  
 
 530  6
         restoreParentArtifactId( pomReversedProperties, null );
 531  6
         restoreArtifactId( pomReversedProperties, artifactId );
 532  
 
 533  6
         createArchetypePom( pom, archetypeFilesDirectory, pomReversedProperties,
 534  
                             FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ), preserveCData, keepParent );
 535  6
     }
 536  
 
 537  
     private String getPackageInPathFormat( String aPackage )
 538  
     {
 539  12
         return StringUtils.replace( aPackage, ".", "/" );
 540  
     }
 541  
 
 542  
     private void rewriteReferences( Model pom, String rootArtifactId, String groupId )
 543  
     {
 544  
         // rewrite Dependencies
 545  18
         if ( pom.getDependencies() != null && !pom.getDependencies().isEmpty() )
 546  
         {
 547  7
             for ( Iterator<Dependency> dependencies = pom.getDependencies().iterator(); dependencies.hasNext(); )
 548  
             {
 549  9
                 Dependency dependency = dependencies.next();
 550  
 
 551  9
                 if ( dependency.getArtifactId() != null && dependency.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 552  
                 {
 553  7
                     if ( dependency.getGroupId() != null )
 554  
                     {
 555  7
                         dependency.setGroupId( StringUtils.replace( dependency.getGroupId(), groupId,
 556  
                                                                     "${" + Constants.GROUP_ID + "}" ) );
 557  
                     }
 558  
 
 559  7
                     dependency.setArtifactId( StringUtils.replace( dependency.getArtifactId(), rootArtifactId,
 560  
                                                                    "${rootArtifactId}" ) );
 561  
 
 562  7
                     if ( dependency.getVersion() != null )
 563  
                     {
 564  7
                         dependency.setVersion( "${" + Constants.VERSION + "}" );
 565  
                     }
 566  
                 }
 567  9
             }
 568  
         }
 569  
 
 570  
         // rewrite DependencyManagement
 571  18
         if ( pom.getDependencyManagement() != null && pom.getDependencyManagement().getDependencies() != null
 572  
             && !pom.getDependencyManagement().getDependencies().isEmpty() )
 573  
         {
 574  0
             for ( Iterator<Dependency> dependencies = pom.getDependencyManagement().getDependencies().iterator();
 575  0
                 dependencies.hasNext(); )
 576  
             {
 577  0
                 Dependency dependency = dependencies.next();
 578  
 
 579  0
                 if ( dependency.getArtifactId() != null && dependency.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 580  
                 {
 581  0
                     if ( dependency.getGroupId() != null )
 582  
                     {
 583  0
                         dependency.setGroupId( StringUtils.replace( dependency.getGroupId(), groupId,
 584  
                                                                     "${" + Constants.GROUP_ID + "}" ) );
 585  
                     }
 586  
 
 587  0
                     dependency.setArtifactId( StringUtils.replace( dependency.getArtifactId(), rootArtifactId,
 588  
                                                                    "${rootArtifactId}" ) );
 589  
 
 590  0
                     if ( dependency.getVersion() != null )
 591  
                     {
 592  0
                         dependency.setVersion( "${" + Constants.VERSION + "}" );
 593  
                     }
 594  
                 }
 595  0
             }
 596  
         }
 597  
 
 598  
         // rewrite Plugins
 599  18
         if ( pom.getBuild() != null && pom.getBuild().getPlugins() != null && !pom.getBuild().getPlugins().isEmpty() )
 600  
         {
 601  0
             for ( Iterator<Plugin> plugins = pom.getBuild().getPlugins().iterator(); plugins.hasNext(); )
 602  
             {
 603  0
                 Plugin plugin = plugins.next();
 604  
 
 605  0
                 if ( plugin.getArtifactId() != null && plugin.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 606  
                 {
 607  0
                     if ( plugin.getGroupId() != null )
 608  
                     {
 609  0
                         plugin.setGroupId( StringUtils.replace( plugin.getGroupId(), groupId,
 610  
                                                                 "${" + Constants.GROUP_ID + "}" ) );
 611  
                     }
 612  
 
 613  0
                     plugin.setArtifactId( StringUtils.replace( plugin.getArtifactId(), rootArtifactId,
 614  
                                                                "${rootArtifactId}" ) );
 615  
 
 616  0
                     if ( plugin.getVersion() != null )
 617  
                     {
 618  0
                         plugin.setVersion( "${" + Constants.VERSION + "}" );
 619  
                     }
 620  
                 }
 621  0
             }
 622  
         }
 623  
 
 624  
         // rewrite PluginManagement
 625  18
         if ( pom.getBuild() != null && pom.getBuild().getPluginManagement() != null
 626  
             && pom.getBuild().getPluginManagement().getPlugins() != null
 627  
             && !pom.getBuild().getPluginManagement().getPlugins().isEmpty() )
 628  
         {
 629  0
             for ( Iterator<Plugin> plugins = pom.getBuild().getPluginManagement().getPlugins().iterator(); plugins.hasNext(); )
 630  
             {
 631  0
                 Plugin plugin = plugins.next();
 632  
 
 633  0
                 if ( plugin.getArtifactId() != null && plugin.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 634  
                 {
 635  0
                     if ( plugin.getGroupId() != null )
 636  
                     {
 637  0
                         plugin.setGroupId( StringUtils.replace( plugin.getGroupId(), groupId,
 638  
                                                                 "${" + Constants.GROUP_ID + "}" ) );
 639  
                     }
 640  
 
 641  0
                     plugin.setArtifactId( StringUtils.replace( plugin.getArtifactId(), rootArtifactId,
 642  
                                                                "${rootArtifactId}" ) );
 643  
 
 644  0
                     if ( plugin.getVersion() != null )
 645  
                     {
 646  0
                         plugin.setVersion( "${" + Constants.VERSION + "}" );
 647  
                     }
 648  
                 }
 649  0
             }
 650  
         }
 651  
 
 652  
         // rewrite Profiles
 653  18
         if ( pom.getProfiles() != null )
 654  
         {
 655  18
             for ( Iterator<Profile> profiles = pom.getProfiles().iterator(); profiles.hasNext(); )
 656  
             {
 657  0
                 Profile profile = profiles.next();
 658  
 
 659  
                 // rewrite Dependencies
 660  0
                 if ( profile.getDependencies() != null && !profile.getDependencies().isEmpty() )
 661  
                 {
 662  0
                     for ( Iterator<Dependency> dependencies = profile.getDependencies().iterator(); dependencies.hasNext(); )
 663  
                     {
 664  0
                         Dependency dependency = dependencies.next();
 665  
 
 666  0
                         if ( dependency.getArtifactId() != null
 667  
                             && dependency.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 668  
                         {
 669  0
                             if ( dependency.getGroupId() != null )
 670  
                             {
 671  0
                                 dependency.setGroupId( StringUtils.replace( dependency.getGroupId(), groupId,
 672  
                                                                             "${" + Constants.GROUP_ID + "}" ) );
 673  
                             }
 674  
 
 675  0
                             dependency.setArtifactId( StringUtils.replace( dependency.getArtifactId(), rootArtifactId,
 676  
                                                                            "${rootArtifactId}" ) );
 677  
 
 678  0
                             if ( dependency.getVersion() != null )
 679  
                             {
 680  0
                                 dependency.setVersion( "${" + Constants.VERSION + "}" );
 681  
                             }
 682  
                         }
 683  0
                     }
 684  
                 }
 685  
 
 686  
                 // rewrite DependencyManagement
 687  0
                 if ( profile.getDependencyManagement() != null
 688  
                     && profile.getDependencyManagement().getDependencies() != null
 689  
                     && !profile.getDependencyManagement().getDependencies().isEmpty() )
 690  
                 {
 691  0
                     for ( Iterator<Dependency> dependencies =
 692  0
                         profile.getDependencyManagement().getDependencies().iterator(); dependencies.hasNext(); )
 693  
                     {
 694  0
                         Dependency dependency = dependencies.next();
 695  
 
 696  0
                         if ( dependency.getArtifactId() != null
 697  
                             && dependency.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 698  
                         {
 699  0
                             if ( dependency.getGroupId() != null )
 700  
                             {
 701  0
                                 dependency.setGroupId( StringUtils.replace( dependency.getGroupId(), groupId,
 702  
                                                                             "${" + Constants.GROUP_ID + "}" ) );
 703  
                             }
 704  
 
 705  0
                             dependency.setArtifactId( StringUtils.replace( dependency.getArtifactId(), rootArtifactId,
 706  
                                                                            "${rootArtifactId}" ) );
 707  
 
 708  0
                             if ( dependency.getVersion() != null )
 709  
                             {
 710  0
                                 dependency.setVersion( "${" + Constants.VERSION + "}" );
 711  
                             }
 712  
                         }
 713  0
                     }
 714  
                 }
 715  
 
 716  
                 // rewrite Plugins
 717  0
                 if ( profile.getBuild() != null && profile.getBuild().getPlugins() != null
 718  
                     && !profile.getBuild().getPlugins().isEmpty() )
 719  
                 {
 720  0
                     for ( Iterator<Plugin> plugins = profile.getBuild().getPlugins().iterator(); plugins.hasNext(); )
 721  
                     {
 722  0
                         Plugin plugin = plugins.next();
 723  
 
 724  0
                         if ( plugin.getArtifactId() != null && plugin.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 725  
                         {
 726  0
                             if ( plugin.getGroupId() != null )
 727  
                             {
 728  0
                                 plugin.setGroupId( StringUtils.replace( plugin.getGroupId(), groupId,
 729  
                                                                         "${" + Constants.GROUP_ID + "}" ) );
 730  
                             }
 731  
 
 732  0
                             plugin.setArtifactId( StringUtils.replace( plugin.getArtifactId(), rootArtifactId,
 733  
                                                                        "${rootArtifactId}" ) );
 734  
 
 735  0
                             if ( plugin.getVersion() != null )
 736  
                             {
 737  0
                                 plugin.setVersion( "${" + Constants.VERSION + "}" );
 738  
                             }
 739  
                         }
 740  0
                     }
 741  
                 }
 742  
 
 743  
                 // rewrite PluginManagement
 744  0
                 if ( profile.getBuild() != null && profile.getBuild().getPluginManagement() != null
 745  
                     && profile.getBuild().getPluginManagement().getPlugins() != null
 746  
                     && !profile.getBuild().getPluginManagement().getPlugins().isEmpty() )
 747  
                 {
 748  0
                     for ( Iterator<Plugin> plugins = profile.getBuild().getPluginManagement().getPlugins().iterator();
 749  0
                         plugins.hasNext(); )
 750  
                     {
 751  0
                         Plugin plugin = plugins.next();
 752  
 
 753  0
                         if ( plugin.getArtifactId() != null && plugin.getArtifactId().indexOf( rootArtifactId ) >= 0 )
 754  
                         {
 755  0
                             if ( plugin.getGroupId() != null )
 756  
                             {
 757  0
                                 plugin.setGroupId( StringUtils.replace( plugin.getGroupId(), groupId,
 758  
                                                                         "${" + Constants.GROUP_ID + "}" ) );
 759  
                             }
 760  
 
 761  0
                             plugin.setArtifactId( StringUtils.replace( plugin.getArtifactId(), rootArtifactId,
 762  
                                                                        "${rootArtifactId}" ) );
 763  
 
 764  0
                             if ( plugin.getVersion() != null )
 765  
                             {
 766  0
                                 plugin.setVersion( "${" + Constants.VERSION + "}" );
 767  
                             }
 768  
                         }
 769  0
                     }
 770  
                 }
 771  0
             }
 772  
         }
 773  18
     }
 774  
 
 775  
     private void setArtifactId( Properties properties, String artifactId )
 776  
     {
 777  30
         properties.setProperty( Constants.ARTIFACT_ID, artifactId );
 778  30
     }
 779  
 
 780  
     private List<String> concatenateToList( List<String> toConcatenate, String with )
 781  
     {
 782  57
         List<String> result = new ArrayList<String>( toConcatenate.size() );
 783  
 
 784  57
         for ( String concatenate : toConcatenate )
 785  
         {
 786  61
             result.add( ( ( with.length() > 0 ) ? ( with + "/" + concatenate ) : concatenate ) );
 787  
         }
 788  
 
 789  57
         return result;
 790  
     }
 791  
 
 792  
     private void copyFiles( File basedir, File archetypeFilesDirectory, String directory,
 793  
                             List<String> fileSetResources, boolean packaged, String packageName )
 794  
         throws IOException
 795  
     {
 796  11
         String packageAsDirectory = StringUtils.replace( packageName, ".", File.separator );
 797  
 
 798  11
         getLogger().debug( "Package as Directory: Package:" + packageName + "->" + packageAsDirectory );
 799  
 
 800  11
         for ( String inputFileName : fileSetResources )
 801  
         {
 802  12
             String outputFileName =
 803  
                 packaged ? StringUtils.replace( inputFileName, packageAsDirectory + File.separator, "" )
 804  
                                 : inputFileName;
 805  12
             getLogger().debug( "InputFileName:" + inputFileName );
 806  12
             getLogger().debug( "OutputFileName:" + outputFileName );
 807  
 
 808  12
             File outputFile = new File( archetypeFilesDirectory, outputFileName );
 809  
 
 810  12
             File inputFile = new File( basedir, inputFileName );
 811  
 
 812  12
             outputFile.getParentFile().mkdirs();
 813  
 
 814  12
             FileUtils.copyFile( inputFile, outputFile );
 815  12
         }
 816  11
     }
 817  
 
 818  
     private void createArchetypeFiles( Properties reverseProperties, List<FileSet> fileSets, String packageName,
 819  
                                        File basedir, File archetypeFilesDirectory, String defaultEncoding )
 820  
         throws IOException
 821  
     {
 822  18
         getLogger().debug( "Creating Archetype/Module files from " + basedir + " to " + archetypeFilesDirectory );
 823  
 
 824  18
         for ( FileSet fileSet : fileSets )
 825  
         {
 826  57
             DirectoryScanner scanner = new DirectoryScanner();
 827  57
             scanner.setBasedir( basedir );
 828  57
             scanner.setIncludes( (String[]) concatenateToList( fileSet.getIncludes(),
 829  
                                                                fileSet.getDirectory() ).toArray( new String[fileSet.getIncludes().size()] ) );
 830  57
             scanner.setExcludes( (String[]) fileSet.getExcludes().toArray( new String[fileSet.getExcludes().size()] ) );
 831  57
             scanner.addDefaultExcludes();
 832  57
             getLogger().debug( "Using fileset " + fileSet );
 833  57
             scanner.scan();
 834  
 
 835  57
             List<String> fileSetResources = Arrays.asList( scanner.getIncludedFiles() );
 836  57
             getLogger().debug( "Scanned " + fileSetResources.size() + " resources" );
 837  
 
 838  57
             if ( fileSet.isFiltered() )
 839  
             {
 840  46
                 processFileSet( basedir, archetypeFilesDirectory, fileSet.getDirectory(), fileSetResources,
 841  
                                 fileSet.isPackaged(), packageName, reverseProperties, defaultEncoding );
 842  46
                 getLogger().debug( "Processed " + fileSet.getDirectory() + " files" );
 843  
             }
 844  
             else
 845  
             {
 846  11
                 copyFiles( basedir, archetypeFilesDirectory, fileSet.getDirectory(), fileSetResources,
 847  
                            fileSet.isPackaged(), packageName );
 848  11
                 getLogger().debug( "Copied " + fileSet.getDirectory() + " files" );
 849  
             }
 850  57
         }
 851  18
     }
 852  
 
 853  
     private void createArchetypePom( Model pom, File archetypeFilesDirectory, Properties pomReversedProperties,
 854  
                                      File initialPomFile, boolean preserveCData, boolean keepParent )
 855  
         throws IOException
 856  
     {
 857  6
         File outputFile = FileUtils.resolveFile( archetypeFilesDirectory, Constants.ARCHETYPE_POM );
 858  
 
 859  6
         if ( preserveCData )
 860  
         {
 861  0
             getLogger().debug( "Preserving CDATA parts of pom" );
 862  0
             File inputFile = FileUtils.resolveFile( archetypeFilesDirectory, Constants.ARCHETYPE_POM + ".tmp" );
 863  
 
 864  0
             FileUtils.copyFile( initialPomFile, inputFile );
 865  
 
 866  0
             Reader in = null;
 867  0
             Writer out = null;
 868  
             try
 869  
             {
 870  0
                 in = ReaderFactory.newXmlReader( inputFile );
 871  
 
 872  0
                 String initialcontent = IOUtil.toString( in );
 873  
 
 874  0
                 String content = getReversedContent( initialcontent, pomReversedProperties );
 875  
 
 876  0
                 outputFile.getParentFile().mkdirs();
 877  
 
 878  0
                 out = WriterFactory.newXmlWriter( outputFile );
 879  
 
 880  0
                 IOUtil.copy( content, out );
 881  
             }
 882  
             finally
 883  
             {
 884  0
                 IOUtil.close( in );
 885  0
                 IOUtil.close( out );
 886  0
             }
 887  
 
 888  0
             inputFile.delete();
 889  0
         }
 890  
         else
 891  
         {
 892  6
             if ( !keepParent )
 893  
             {
 894  3
                 pom.setParent( null );
 895  
             }
 896  
 
 897  6
             pom.setModules( null );
 898  6
             pom.setGroupId( "${" + Constants.GROUP_ID + "}" );
 899  6
             pom.setArtifactId( "${" + Constants.ARTIFACT_ID + "}" );
 900  6
             pom.setVersion( "${" + Constants.VERSION + "}" );
 901  
 
 902  6
             rewriteReferences( pom, pomReversedProperties.getProperty( Constants.ARTIFACT_ID ),
 903  
                                pomReversedProperties.getProperty( Constants.GROUP_ID ) );
 904  
 
 905  6
             pomManager.writePom( pom, outputFile, initialPomFile );
 906  
         }
 907  
 
 908  6
         Reader in = null;
 909  
         try
 910  
         {
 911  6
             in = ReaderFactory.newXmlReader( initialPomFile );
 912  6
             String initialcontent = IOUtil.toString( in );
 913  
 
 914  6
             Iterator<?> properties = pomReversedProperties.keySet().iterator();
 915  39
             while ( properties.hasNext() )
 916  
             {
 917  33
                 String property = (String) properties.next();
 918  
 
 919  33
                 if ( initialcontent.indexOf( "${" + property + "}" ) > 0 )
 920  
                 {
 921  0
                     getLogger().warn(
 922  
                                       "Archetype uses ${" + property + "} for internal processing, but file "
 923  
                                           + initialPomFile + " contains this property already" );
 924  
                 }
 925  33
             }
 926  
         }
 927  
         finally
 928  
         {
 929  6
             IOUtil.close( in );
 930  6
         }
 931  6
     }
 932  
 
 933  
     private FileSet createFileSet( final List<String> excludes, final boolean packaged, final boolean filtered,
 934  
                                    final String group, final List<String> includes, String defaultEncoding )
 935  
     {
 936  57
         FileSet fileSet = new FileSet();
 937  
 
 938  57
         fileSet.setDirectory( group );
 939  57
         fileSet.setPackaged( packaged );
 940  57
         fileSet.setFiltered( filtered );
 941  57
         fileSet.setIncludes( includes );
 942  57
         fileSet.setExcludes( excludes );
 943  57
         fileSet.setEncoding( defaultEncoding );
 944  
 
 945  57
         getLogger().debug( "Created Fileset " + fileSet );
 946  
 
 947  57
         return fileSet;
 948  
     }
 949  
 
 950  
     private List<FileSet> createFileSets( List<String> files, int level, boolean packaged, String packageName,
 951  
                                           boolean filtered, String defaultEncoding )
 952  
     {
 953  47
         List<FileSet> fileSets = new ArrayList<FileSet>();
 954  
 
 955  47
         if ( !files.isEmpty() )
 956  
         {
 957  47
             getLogger().debug(
 958  
                                "Creating filesets" + ( packaged ? ( " packaged (" + packageName + ")" ) : "" )
 959  
                                    + ( filtered ? " filtered" : "" ) + " at level " + level );
 960  47
             if ( level == 0 )
 961  
             {
 962  15
                 List<String> includes = new ArrayList<String>( files );
 963  15
                 List<String> excludes = new ArrayList<String>();
 964  
 
 965  15
                 if ( !includes.isEmpty() )
 966  
                 {
 967  15
                     fileSets.add( createFileSet( excludes, packaged, filtered, "", includes, defaultEncoding ) );
 968  
                 }
 969  15
             }
 970  
             else
 971  
             {
 972  32
                 Map<String, List<String>> groups = getGroupsMap( files, level );
 973  
 
 974  32
                 for ( String group : groups.keySet() )
 975  
                 {
 976  38
                     getLogger().debug( "Creating filesets for group " + group );
 977  
 
 978  38
                     if ( !packaged )
 979  
                     {
 980  11
                         fileSets.add( getUnpackagedFileSet( filtered, group, groups.get( group ), defaultEncoding ) );
 981  
                     }
 982  
                     else
 983  
                     {
 984  27
                         fileSets.addAll( getPackagedFileSets( filtered, group, groups.get( group ), packageName,
 985  
                                                               defaultEncoding ) );
 986  
                     }
 987  
                 }
 988  
             } // end if
 989  
 
 990  47
             getLogger().debug( "Resolved fileSets " + fileSets );
 991  
         } // end if
 992  
 
 993  47
         return fileSets;
 994  
     }
 995  
 
 996  
     private ModuleDescriptor createModule( Properties reverseProperties, String rootArtifactId, String moduleId,
 997  
                                            String packageName, File basedir, File archetypeFilesDirectory,
 998  
                                            List<String> languages, List<String> filtereds, String defaultEncoding,
 999  
                                            boolean preserveCData, boolean keepParent )
 1000  
         throws IOException, XmlPullParserException
 1001  
     {
 1002  12
         ModuleDescriptor archetypeDescriptor = new ModuleDescriptor();
 1003  12
         getLogger().debug( "Starting module's descriptor " + moduleId );
 1004  
 
 1005  12
         archetypeFilesDirectory.mkdirs();
 1006  12
         getLogger().debug( "Module's files output directory " + archetypeFilesDirectory );
 1007  
 
 1008  12
         Model pom = pomManager.readPom( FileUtils.resolveFile( basedir, Constants.ARCHETYPE_POM ) );
 1009  12
         String replacementId = pom.getArtifactId();
 1010  12
         String moduleDirectory = pom.getArtifactId();
 1011  
 
 1012  12
         if ( replacementId.indexOf( rootArtifactId ) >= 0 )
 1013  
         {
 1014  12
             replacementId = StringUtils.replace( replacementId, rootArtifactId, "${rootArtifactId}" );
 1015  12
             moduleDirectory = StringUtils.replace( moduleId, rootArtifactId, "__rootArtifactId__" );
 1016  
         }
 1017  
 
 1018  12
         if ( moduleId.indexOf( rootArtifactId ) >= 0 )
 1019  
         {
 1020  7
             moduleDirectory = StringUtils.replace( moduleId, rootArtifactId, "__rootArtifactId__" );
 1021  
         }
 1022  
 
 1023  12
         archetypeDescriptor.setName( replacementId );
 1024  12
         archetypeDescriptor.setId( replacementId );
 1025  12
         archetypeDescriptor.setDir( moduleDirectory );
 1026  
 
 1027  12
         setArtifactId( reverseProperties, pom.getArtifactId() );
 1028  
 
 1029  12
         List<String> fileNames = resolveFileNames( pom, basedir );
 1030  
 
 1031  12
         List<FileSet> filesets = resolveFileSets( packageName, fileNames, languages, filtereds, defaultEncoding );
 1032  12
         getLogger().debug( "Resolved filesets for module " + archetypeDescriptor.getName() );
 1033  
 
 1034  12
         archetypeDescriptor.setFileSets( filesets );
 1035  
 
 1036  12
         createArchetypeFiles( reverseProperties, filesets, packageName, basedir, archetypeFilesDirectory,
 1037  
                               defaultEncoding );
 1038  12
         getLogger().debug( "Created files for module " + archetypeDescriptor.getName() );
 1039  
 
 1040  12
         String parentArtifactId = reverseProperties.getProperty( Constants.PARENT_ARTIFACT_ID );
 1041  12
         setParentArtifactId( reverseProperties, pom.getArtifactId() );
 1042  
 
 1043  12
         for ( Iterator<String> modules = pom.getModules().iterator(); modules.hasNext(); )
 1044  
         {
 1045  4
             String subModuleId = modules.next();
 1046  
 
 1047  4
             String subModuleIdDirectory = subModuleId;
 1048  4
             if ( subModuleId.indexOf( rootArtifactId ) >= 0 )
 1049  
             {
 1050  2
                 subModuleIdDirectory = StringUtils.replace( subModuleId, rootArtifactId, "__rootArtifactId__" );
 1051  
             }
 1052  
 
 1053  4
             getLogger().debug( "Creating module " + subModuleId );
 1054  
 
 1055  4
             ModuleDescriptor moduleDescriptor =
 1056  
                 createModule( reverseProperties, rootArtifactId, subModuleId, packageName,
 1057  
                               FileUtils.resolveFile( basedir, subModuleId ),
 1058  
                               FileUtils.resolveFile( archetypeFilesDirectory, subModuleIdDirectory ), languages,
 1059  
                               filtereds, defaultEncoding, preserveCData, keepParent );
 1060  
 
 1061  4
             archetypeDescriptor.addModule( moduleDescriptor );
 1062  
 
 1063  4
             getLogger().debug( "Added module " + moduleDescriptor.getName() + " in " + archetypeDescriptor.getName() );
 1064  4
         }
 1065  
 
 1066  12
         restoreParentArtifactId( reverseProperties, parentArtifactId );
 1067  12
         restoreArtifactId( reverseProperties, pom.getArtifactId() );
 1068  
 
 1069  12
         getLogger().debug( "Created Module " + archetypeDescriptor.getName() + " pom" );
 1070  
 
 1071  12
         return archetypeDescriptor;
 1072  
     }
 1073  
 
 1074  
     private void createModulePom( Model pom, String rootArtifactId, File archetypeFilesDirectory,
 1075  
                                   Properties pomReversedProperties, File initialPomFile, boolean preserveCData,
 1076  
                                   boolean keepParent )
 1077  
         throws IOException
 1078  
     {
 1079  12
         File outputFile = FileUtils.resolveFile( archetypeFilesDirectory, Constants.ARCHETYPE_POM );
 1080  
 
 1081  12
         if ( preserveCData )
 1082  
         {
 1083  0
             getLogger().debug( "Preserving CDATA parts of pom" );
 1084  0
             File inputFile = FileUtils.resolveFile( archetypeFilesDirectory, Constants.ARCHETYPE_POM + ".tmp" );
 1085  
 
 1086  0
             FileUtils.copyFile( initialPomFile, inputFile );
 1087  
 
 1088  0
             Reader in = null;
 1089  0
             Writer out = null;
 1090  
             try
 1091  
             {
 1092  0
                 in = ReaderFactory.newXmlReader( inputFile );
 1093  
 
 1094  0
                 String initialcontent = IOUtil.toString( in );
 1095  
 
 1096  0
                 String content = getReversedContent( initialcontent, pomReversedProperties );
 1097  
 
 1098  0
                 outputFile.getParentFile().mkdirs();
 1099  
 
 1100  0
                 out = WriterFactory.newXmlWriter( outputFile );
 1101  
 
 1102  0
                 IOUtil.copy( content, out );
 1103  
             }
 1104  
             finally
 1105  
             {
 1106  0
                 IOUtil.close( in );
 1107  0
                 IOUtil.close( out );
 1108  0
             }
 1109  
 
 1110  0
             inputFile.delete();
 1111  0
         }
 1112  
         else
 1113  
         {
 1114  12
             if ( pom.getParent() != null )
 1115  
             {
 1116  12
                 pom.getParent().setGroupId(
 1117  
                                             StringUtils.replace(
 1118  
                                                                  pom.getParent().getGroupId(),
 1119  
                                                                  pomReversedProperties.getProperty( Constants.GROUP_ID ),
 1120  
                                                                  "${" + Constants.GROUP_ID + "}" ) );
 1121  12
                 if ( pom.getParent().getArtifactId() != null
 1122  
                     && pom.getParent().getArtifactId().indexOf( rootArtifactId ) >= 0 )
 1123  
                 {
 1124  12
                     pom.getParent().setArtifactId(
 1125  
                                                    StringUtils.replace( pom.getParent().getArtifactId(),
 1126  
                                                                         rootArtifactId, "${rootArtifactId}" ) );
 1127  
                 }
 1128  12
                 if ( pom.getParent().getVersion() != null )
 1129  
                 {
 1130  12
                     pom.getParent().setVersion( "${" + Constants.VERSION + "}" );
 1131  
                 }
 1132  
             }
 1133  12
             pom.setModules( null );
 1134  
 
 1135  12
             if ( pom.getGroupId() != null )
 1136  
             {
 1137  0
                 pom.setGroupId( StringUtils.replace( pom.getGroupId(),
 1138  
                                                      pomReversedProperties.getProperty( Constants.GROUP_ID ), "${"
 1139  
                                                          + Constants.GROUP_ID + "}" ) );
 1140  
             }
 1141  
 
 1142  12
             pom.setArtifactId( "${" + Constants.ARTIFACT_ID + "}" );
 1143  
 
 1144  12
             if ( pom.getVersion() != null )
 1145  
             {
 1146  0
                 pom.setVersion( "${" + Constants.VERSION + "}" );
 1147  
             }
 1148  
 
 1149  12
             rewriteReferences( pom, rootArtifactId, pomReversedProperties.getProperty( Constants.GROUP_ID ) );
 1150  
 
 1151  12
             pomManager.writePom( pom, outputFile, initialPomFile );
 1152  
         }
 1153  
 
 1154  12
         Reader in = null;
 1155  
         try
 1156  
         {
 1157  12
             in = ReaderFactory.newXmlReader( initialPomFile );
 1158  12
             String initialcontent = IOUtil.toString( in );
 1159  
 
 1160  12
             for ( Iterator<?> properties = pomReversedProperties.keySet().iterator(); properties.hasNext(); )
 1161  
             {
 1162  77
                 String property = (String) properties.next();
 1163  
 
 1164  77
                 if ( initialcontent.indexOf( "${" + property + "}" ) > 0 )
 1165  
                 {
 1166  0
                     getLogger().warn(
 1167  
                                       "OldArchetype uses ${" + property + "} for internal processing, but file "
 1168  
                                           + initialPomFile + " contains this property already" );
 1169  
                 }
 1170  77
             }
 1171  
         }
 1172  
         finally
 1173  
         {
 1174  12
             IOUtil.close( in );
 1175  12
         }
 1176  12
     }
 1177  
 
 1178  
     private Set<String> getExtensions( List<String> files )
 1179  
     {
 1180  65
         Set<String> extensions = new HashSet<String>();
 1181  
 
 1182  65
         for ( String file : files )
 1183  
         {
 1184  48
             extensions.add( FileUtils.extension( file ) );
 1185  
         }
 1186  
 
 1187  65
         return extensions;
 1188  
     }
 1189  
 
 1190  
     private Map<String, List<String>> getGroupsMap( final List<String> files, final int level )
 1191  
     {
 1192  32
         Map<String, List<String>> groups = new HashMap<String, List<String>>();
 1193  
 
 1194  32
         for ( String file : files )
 1195  
         {
 1196  48
             String directory = PathUtils.getDirectory( file, level );
 1197  
             // make all groups have unix style
 1198  48
             directory = StringUtils.replace( directory, File.separator, "/" );
 1199  
 
 1200  48
             if ( !groups.containsKey( directory ) )
 1201  
             {
 1202  38
                 groups.put( directory, new ArrayList<String>() );
 1203  
             }
 1204  
 
 1205  48
             List<String> group = groups.get( directory );
 1206  
 
 1207  48
             String innerPath = file.substring( directory.length() + 1 );
 1208  
             // make all groups have unix style
 1209  48
             innerPath = StringUtils.replace( innerPath, File.separator, "/" );
 1210  
 
 1211  48
             group.add( innerPath );
 1212  48
         }
 1213  
 
 1214  32
         getLogger().debug( "Sorted " + groups.size() + " groups in " + files.size() + " files" );
 1215  32
         getLogger().debug( "Sorted Files: " + files );
 1216  
 
 1217  32
         return groups;
 1218  
     }
 1219  
 
 1220  
     private FileSet getPackagedFileSet( final boolean filtered, final Set<String> packagedExtensions,
 1221  
                                         final String group, final Set<String> unpackagedExtensions,
 1222  
                                         final List<String> unpackagedFiles, String defaultEncoding )
 1223  
     {
 1224  21
         List<String> includes = new ArrayList<String>();
 1225  21
         List<String> excludes = new ArrayList<String>();
 1226  
 
 1227  21
         for ( String extension : packagedExtensions )
 1228  
         {
 1229  23
             includes.add( "**/*." + extension );
 1230  
 
 1231  23
             if ( unpackagedExtensions.contains( extension ) )
 1232  
             {
 1233  3
                 excludes.addAll( archetypeFilesResolver.getFilesWithExtension( unpackagedFiles, extension ) );
 1234  
             }
 1235  
         }
 1236  
 
 1237  21
         return createFileSet( excludes, true, filtered, group, includes, defaultEncoding );
 1238  
     }
 1239  
 
 1240  
     private List<FileSet> getPackagedFileSets( final boolean filtered, final String group,
 1241  
                                                final List<String> groupFiles, final String packageName,
 1242  
                                                String defaultEncoding )
 1243  
     {
 1244  27
         String packageAsDir = StringUtils.replace( packageName, ".", "/" );
 1245  
 
 1246  27
         List<FileSet> packagedFileSets = new ArrayList<FileSet>();
 1247  27
         List<String> packagedFiles = archetypeFilesResolver.getPackagedFiles( groupFiles, packageAsDir );
 1248  27
         getLogger().debug( "Found packaged Files:" + packagedFiles );
 1249  
 
 1250  27
         List<String> unpackagedFiles = archetypeFilesResolver.getUnpackagedFiles( groupFiles, packageAsDir );
 1251  27
         getLogger().debug( "Found unpackaged Files:" + unpackagedFiles );
 1252  
 
 1253  27
         Set<String> packagedExtensions = getExtensions( packagedFiles );
 1254  27
         getLogger().debug( "Found packaged extensions " + packagedExtensions );
 1255  
 
 1256  27
         Set<String> unpackagedExtensions = getExtensions( unpackagedFiles );
 1257  
 
 1258  27
         if ( !packagedExtensions.isEmpty() )
 1259  
         {
 1260  21
             packagedFileSets.add( getPackagedFileSet( filtered, packagedExtensions, group, unpackagedExtensions,
 1261  
                                                       unpackagedFiles, defaultEncoding ) );
 1262  
         }
 1263  
 
 1264  27
         if ( !unpackagedExtensions.isEmpty() )
 1265  
         {
 1266  10
             getLogger().debug( "Found unpackaged extensions " + unpackagedExtensions );
 1267  
 
 1268  10
             packagedFileSets.add( getUnpackagedFileSet( filtered, unpackagedExtensions, unpackagedFiles, group,
 1269  
                                                         packagedExtensions, defaultEncoding ) );
 1270  
         }
 1271  
 
 1272  27
         return packagedFileSets;
 1273  
     }
 1274  
 
 1275  
     private void setParentArtifactId( Properties properties, String parentArtifactId )
 1276  
     {
 1277  30
         properties.setProperty( Constants.PARENT_ARTIFACT_ID, parentArtifactId );
 1278  30
     }
 1279  
 
 1280  
     private void processFileSet( File basedir, File archetypeFilesDirectory, String directory,
 1281  
                                  List<String> fileSetResources, boolean packaged, String packageName,
 1282  
                                  Properties reverseProperties, String defaultEncoding )
 1283  
         throws IOException
 1284  
     {
 1285  46
         String packageAsDirectory = StringUtils.replace( packageName, ".", File.separator );
 1286  
 
 1287  46
         getLogger().debug( "Package as Directory: Package:" + packageName + "->" + packageAsDirectory );
 1288  
 
 1289  46
         for ( String inputFileName : fileSetResources )
 1290  
         {
 1291  55
             String outputFileName =
 1292  
                 packaged ? StringUtils.replace( inputFileName, packageAsDirectory + File.separator, "" )
 1293  
                                 : inputFileName;
 1294  
 
 1295  55
             getLogger().debug( "InputFileName:" + inputFileName );
 1296  55
             getLogger().debug( "OutputFileName:" + outputFileName );
 1297  
 
 1298  55
             File outputFile = new File( archetypeFilesDirectory, outputFileName );
 1299  55
             File inputFile = new File( basedir, inputFileName );
 1300  
 
 1301  55
             FileCharsetDetector detector = new FileCharsetDetector( inputFile );
 1302  
 
 1303  55
             String fileEncoding = detector.isFound() ? detector.getCharset() : defaultEncoding;
 1304  
 
 1305  55
             String initialcontent = IOUtil.toString( new FileInputStream( inputFile ), fileEncoding );
 1306  
 
 1307  55
             for ( Iterator<?> properties = reverseProperties.keySet().iterator(); properties.hasNext(); )
 1308  
             {
 1309  336
                 String property = (String) properties.next();
 1310  
 
 1311  336
                 if ( initialcontent.indexOf( "${" + property + "}" ) > 0 )
 1312  
                 {
 1313  0
                     getLogger().warn(
 1314  
                                       "Archetype uses ${" + property + "} for internal processing, but file "
 1315  
                                           + inputFile + " contains this property already" );
 1316  
                 }
 1317  336
             }
 1318  
 
 1319  55
             String content = getReversedContent( initialcontent, reverseProperties );
 1320  
 
 1321  55
             outputFile.getParentFile().mkdirs();
 1322  
 
 1323  55
             org.apache.commons.io.IOUtils.write( content, new FileOutputStream( outputFile ), fileEncoding );
 1324  55
         }
 1325  46
     }
 1326  
 
 1327  
     private List<String> removePackage( List<String> sources, String packageAsDirectory )
 1328  
     {
 1329  0
         if ( sources == null )
 1330  
         {
 1331  0
             return null;
 1332  
         }
 1333  
 
 1334  0
         List<String> unpackagedSources = new ArrayList<String>( sources.size() );
 1335  
 
 1336  0
         for ( String source : sources )
 1337  
         {
 1338  0
             String unpackagedSource = StringUtils.replace( source, packageAsDirectory, "" );
 1339  
 
 1340  0
             unpackagedSources.add( unpackagedSource );
 1341  0
         }
 1342  
 
 1343  0
         return unpackagedSources;
 1344  
     }
 1345  
 
 1346  
     private Properties getReversedProperties( ArchetypeDescriptor archetypeDescriptor, Properties properties )
 1347  
     {
 1348  12
         Properties reversedProperties = new Properties();
 1349  
 
 1350  12
         reversedProperties.putAll( properties );
 1351  12
         reversedProperties.remove( Constants.ARCHETYPE_GROUP_ID );
 1352  12
         reversedProperties.remove( Constants.ARCHETYPE_ARTIFACT_ID );
 1353  12
         reversedProperties.remove( Constants.ARCHETYPE_VERSION );
 1354  
 
 1355  12
         String packageName = properties.getProperty( Constants.PACKAGE );
 1356  12
         String packageInPathFormat = getPackageInPathFormat( packageName );
 1357  12
         if ( !packageInPathFormat.equals( packageName ) )
 1358  
         {
 1359  10
             reversedProperties.setProperty( Constants.PACKAGE_IN_PATH_FORMAT, packageInPathFormat );
 1360  
         }
 1361  
 
 1362  
         // TODO check that reversed properties are all different and no one is a substring of another?
 1363  
         // to avoid wrong variable replacements
 1364  
 
 1365  12
         return reversedProperties;
 1366  
     }
 1367  
 
 1368  
     private List<String> resolveFileNames( final Model pom, final File basedir )
 1369  
         throws IOException
 1370  
     {
 1371  18
         getLogger().debug( "Resolving files for " + pom.getId() + " in " + basedir );
 1372  
 
 1373  18
         StringBuffer buff = new StringBuffer( "pom.xml*,archetype.properties*,target/**," );
 1374  18
         for ( Iterator<String> modules = pom.getModules().iterator(); modules.hasNext(); )
 1375  
         {
 1376  12
             buff.append( ',' ).append( modules.next() ).append( "/**" );
 1377  
         }
 1378  
 
 1379  432
         for ( String defaultExclude : ListScanner.DEFAULTEXCLUDES )
 1380  
         {
 1381  414
             buff.append( ',' ).append( defaultExclude ).append( "/**" );
 1382  
         }
 1383  
 
 1384  18
         String excludes = PathUtils.convertPathForOS( buff.toString() );
 1385  
 
 1386  18
         List<String> fileNames = FileUtils.getFileNames( basedir, "**,.*,**/.*", excludes, false );
 1387  
 
 1388  18
         getLogger().debug( "Resolved " + fileNames.size() + " files" );
 1389  18
         getLogger().debug( "Resolved Files:" + fileNames );
 1390  
 
 1391  18
         return fileNames;
 1392  
     }
 1393  
 
 1394  
     private List<FileSet> resolveFileSets( String packageName, List<String> fileNames, List<String> languages,
 1395  
                                            List<String> filtereds, String defaultEncoding )
 1396  
     {
 1397  18
         List<FileSet> resolvedFileSets = new ArrayList<FileSet>();
 1398  18
         getLogger().debug(
 1399  
                            "Resolving filesets with package=" + packageName + ", languages=" + languages
 1400  
                                + " and extentions=" + filtereds );
 1401  
 
 1402  18
         List<String> files = new ArrayList<String>( fileNames );
 1403  
 
 1404  18
         StringBuilder languageIncludes = new StringBuilder(  );
 1405  
 
 1406  18
         for ( String language : languages )
 1407  
         {
 1408  72
             languageIncludes .append( ( ( languageIncludes.length() == 0 ) ? "" : "," ) + language + "/**" );
 1409  
         }
 1410  
 
 1411  18
         getLogger().debug( "Using languages includes " + languageIncludes );
 1412  
 
 1413  18
         String filteredIncludes = "";
 1414  18
         for ( String filtered : filtereds )
 1415  
         {
 1416  256
             filteredIncludes +=
 1417  
                 ( ( filteredIncludes.length() == 0 ) ? "" : "," ) + "**/" + ( filtered.startsWith( "." ) ? "" : "*." )
 1418  
                     + filtered;
 1419  
         }
 1420  
 
 1421  18
         getLogger().debug( "Using filtered includes " + filteredIncludes );
 1422  
 
 1423  
         /* sourcesMainFiles */
 1424  18
         List<String> sourcesMainFiles = archetypeFilesResolver.findSourcesMainFiles( files, languageIncludes.toString() );
 1425  18
         if ( !sourcesMainFiles.isEmpty() )
 1426  
         {
 1427  12
             files.removeAll( sourcesMainFiles );
 1428  
 
 1429  12
             List<String> filteredFiles = archetypeFilesResolver.getFilteredFiles( sourcesMainFiles, filteredIncludes );
 1430  12
             sourcesMainFiles.removeAll( filteredFiles );
 1431  
 
 1432  12
             List<String> unfilteredFiles = sourcesMainFiles;
 1433  12
             if ( !filteredFiles.isEmpty() )
 1434  
             {
 1435  12
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 3, true, packageName, true, defaultEncoding ) );
 1436  
             }
 1437  
 
 1438  12
             if ( !unfilteredFiles.isEmpty() )
 1439  
             {
 1440  1
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 3, true, packageName, false, defaultEncoding ) );
 1441  
             }
 1442  
         }
 1443  
 
 1444  
         /* resourcesMainFiles */
 1445  18
         List<String> resourcesMainFiles =
 1446  
             archetypeFilesResolver.findResourcesMainFiles( files, languageIncludes.toString() );
 1447  18
         if ( !resourcesMainFiles.isEmpty() )
 1448  
         {
 1449  3
             files.removeAll( resourcesMainFiles );
 1450  
 
 1451  3
             List<String> filteredFiles = archetypeFilesResolver.getFilteredFiles( resourcesMainFiles, filteredIncludes );
 1452  3
             resourcesMainFiles.removeAll( filteredFiles );
 1453  
 
 1454  3
             List<String> unfilteredFiles = resourcesMainFiles;
 1455  3
             if ( !filteredFiles.isEmpty() )
 1456  
             {
 1457  3
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 3, false, packageName, true, defaultEncoding ) );
 1458  
             }
 1459  3
             if ( !unfilteredFiles.isEmpty() )
 1460  
             {
 1461  0
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 3, false, packageName, false, defaultEncoding ) );
 1462  
             }
 1463  
         }
 1464  
 
 1465  
         /* sourcesTestFiles */
 1466  18
         List<String> sourcesTestFiles = archetypeFilesResolver.findSourcesTestFiles( files, languageIncludes.toString() );
 1467  18
         if ( !sourcesTestFiles.isEmpty() )
 1468  
         {
 1469  6
             files.removeAll( sourcesTestFiles );
 1470  
 
 1471  6
             List<String> filteredFiles = archetypeFilesResolver.getFilteredFiles( sourcesTestFiles, filteredIncludes );
 1472  6
             sourcesTestFiles.removeAll( filteredFiles );
 1473  
 
 1474  6
             List<String> unfilteredFiles = sourcesTestFiles;
 1475  6
             if ( !filteredFiles.isEmpty() )
 1476  
             {
 1477  6
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 3, true, packageName, true, defaultEncoding ) );
 1478  
             }
 1479  6
             if ( !unfilteredFiles.isEmpty() )
 1480  
             {
 1481  0
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 3, true, packageName, false, defaultEncoding ) );
 1482  
             }
 1483  
         }
 1484  
 
 1485  
         /* ressourcesTestFiles */
 1486  18
         List<String> resourcesTestFiles =
 1487  
             archetypeFilesResolver.findResourcesTestFiles( files, languageIncludes.toString() );
 1488  18
         if ( !resourcesTestFiles.isEmpty() )
 1489  
         {
 1490  0
             files.removeAll( resourcesTestFiles );
 1491  
 
 1492  0
             List<String> filteredFiles = archetypeFilesResolver.getFilteredFiles( resourcesTestFiles, filteredIncludes );
 1493  0
             resourcesTestFiles.removeAll( filteredFiles );
 1494  
 
 1495  0
             List<String> unfilteredFiles = resourcesTestFiles;
 1496  0
             if ( !filteredFiles.isEmpty() )
 1497  
             {
 1498  0
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 3, false, packageName, true, defaultEncoding ) );
 1499  
             }
 1500  0
             if ( !unfilteredFiles.isEmpty() )
 1501  
             {
 1502  0
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 3, false, packageName, false, defaultEncoding ) );
 1503  
             }
 1504  
         }
 1505  
 
 1506  
         /* siteFiles */
 1507  18
         List<String> siteFiles = archetypeFilesResolver.findSiteFiles( files, languageIncludes.toString() );
 1508  18
         if ( !siteFiles.isEmpty() )
 1509  
         {
 1510  4
             files.removeAll( siteFiles );
 1511  
 
 1512  4
             List<String> filteredFiles = archetypeFilesResolver.getFilteredFiles( siteFiles, filteredIncludes );
 1513  4
             siteFiles.removeAll( filteredFiles );
 1514  
 
 1515  4
             List<String> unfilteredFiles = siteFiles;
 1516  4
             if ( !filteredFiles.isEmpty() )
 1517  
             {
 1518  4
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 2, false, packageName, true, defaultEncoding ) );
 1519  
             }
 1520  4
             if ( !unfilteredFiles.isEmpty() )
 1521  
             {
 1522  1
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 2, false, packageName, false, defaultEncoding ) );
 1523  
             }
 1524  
         }
 1525  
 
 1526  
         /* thirdLevelSourcesfiles */
 1527  18
         List<String> thirdLevelSourcesfiles =
 1528  
             archetypeFilesResolver.findOtherSources( 3, files, languageIncludes.toString() );
 1529  18
         if ( !thirdLevelSourcesfiles.isEmpty() )
 1530  
         {
 1531  2
             files.removeAll( thirdLevelSourcesfiles );
 1532  
 
 1533  2
             List<String> filteredFiles =
 1534  
                 archetypeFilesResolver.getFilteredFiles( thirdLevelSourcesfiles, filteredIncludes );
 1535  2
             thirdLevelSourcesfiles.removeAll( filteredFiles );
 1536  
 
 1537  2
             List<String> unfilteredFiles = thirdLevelSourcesfiles;
 1538  2
             if ( !filteredFiles.isEmpty() )
 1539  
             {
 1540  2
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 3, true, packageName, true, defaultEncoding ) );
 1541  
             }
 1542  2
             if ( !unfilteredFiles.isEmpty() )
 1543  
             {
 1544  0
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 3, true, packageName, false, defaultEncoding ) );
 1545  
             }
 1546  
 
 1547  
             /* thirdLevelResourcesfiles */
 1548  2
             List<String> thirdLevelResourcesfiles =
 1549  
                 archetypeFilesResolver.findOtherResources( 3, files, thirdLevelSourcesfiles, languageIncludes.toString() );
 1550  2
             if ( !thirdLevelResourcesfiles.isEmpty() )
 1551  
             {
 1552  2
                 files.removeAll( thirdLevelResourcesfiles );
 1553  2
                 filteredFiles = archetypeFilesResolver.getFilteredFiles( thirdLevelResourcesfiles, filteredIncludes );
 1554  2
                 thirdLevelResourcesfiles.removeAll( filteredFiles );
 1555  2
                 unfilteredFiles = thirdLevelResourcesfiles;
 1556  2
                 if ( !filteredFiles.isEmpty() )
 1557  
                 {
 1558  2
                     resolvedFileSets.addAll( createFileSets( filteredFiles, 3, false, packageName, true,
 1559  
                                                              defaultEncoding ) );
 1560  
                 }
 1561  2
                 if ( !unfilteredFiles.isEmpty() )
 1562  
                 {
 1563  0
                     resolvedFileSets.addAll( createFileSets( unfilteredFiles, 3, false, packageName, false,
 1564  
                                                              defaultEncoding ) );
 1565  
                 }
 1566  
             }
 1567  
         } // end if
 1568  
 
 1569  
         /* secondLevelSourcesfiles */
 1570  18
         List<String> secondLevelSourcesfiles =
 1571  
             archetypeFilesResolver.findOtherSources( 2, files, languageIncludes.toString() );
 1572  18
         if ( !secondLevelSourcesfiles.isEmpty() )
 1573  
         {
 1574  0
             files.removeAll( secondLevelSourcesfiles );
 1575  
 
 1576  0
             List<String> filteredFiles =
 1577  
                 archetypeFilesResolver.getFilteredFiles( secondLevelSourcesfiles, filteredIncludes );
 1578  0
             secondLevelSourcesfiles.removeAll( filteredFiles );
 1579  
 
 1580  0
             List<String> unfilteredFiles = secondLevelSourcesfiles;
 1581  0
             if ( !filteredFiles.isEmpty() )
 1582  
             {
 1583  0
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 2, true, packageName, true, defaultEncoding ) );
 1584  
             }
 1585  0
             if ( !unfilteredFiles.isEmpty() )
 1586  
             {
 1587  0
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 2, true, packageName, false, defaultEncoding ) );
 1588  
             }
 1589  
         }
 1590  
 
 1591  
         /* secondLevelResourcesfiles */
 1592  18
         List<String> secondLevelResourcesfiles =
 1593  
             archetypeFilesResolver.findOtherResources( 2, files, languageIncludes.toString() );
 1594  18
         if ( !secondLevelResourcesfiles.isEmpty() )
 1595  
         {
 1596  1
             files.removeAll( secondLevelResourcesfiles );
 1597  
 
 1598  1
             List<String> filteredFiles =
 1599  
                 archetypeFilesResolver.getFilteredFiles( secondLevelResourcesfiles, filteredIncludes );
 1600  1
             secondLevelResourcesfiles.removeAll( filteredFiles );
 1601  
 
 1602  1
             List<String> unfilteredFiles = secondLevelResourcesfiles;
 1603  1
             if ( !filteredFiles.isEmpty() )
 1604  
             {
 1605  0
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 2, false, packageName, true, defaultEncoding ) );
 1606  
             }
 1607  1
             if ( !unfilteredFiles.isEmpty() )
 1608  
             {
 1609  1
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 2, false, packageName, false, defaultEncoding ) );
 1610  
             }
 1611  
         }
 1612  
 
 1613  
         /* rootResourcesfiles */
 1614  18
         List<String> rootResourcesfiles =
 1615  
             archetypeFilesResolver.findOtherResources( 0, files, languageIncludes.toString() );
 1616  18
         if ( !rootResourcesfiles.isEmpty() )
 1617  
         {
 1618  8
             files.removeAll( rootResourcesfiles );
 1619  
 
 1620  8
             List<String> filteredFiles = archetypeFilesResolver.getFilteredFiles( rootResourcesfiles, filteredIncludes );
 1621  8
             rootResourcesfiles.removeAll( filteredFiles );
 1622  
 
 1623  8
             List<String> unfilteredFiles = rootResourcesfiles;
 1624  8
             if ( !filteredFiles.isEmpty() )
 1625  
             {
 1626  8
                 resolvedFileSets.addAll( createFileSets( filteredFiles, 0, false, packageName, true, defaultEncoding ) );
 1627  
             }
 1628  8
             if ( !unfilteredFiles.isEmpty() )
 1629  
             {
 1630  7
                 resolvedFileSets.addAll( createFileSets( unfilteredFiles, 0, false, packageName, false, defaultEncoding ) );
 1631  
             }
 1632  
         }
 1633  
 
 1634  
         /**/
 1635  18
         if ( !files.isEmpty() )
 1636  
         {
 1637  0
             getLogger().info( "Ignored files: " + files );
 1638  
         }
 1639  
 
 1640  18
         return resolvedFileSets;
 1641  
     }
 1642  
 
 1643  
     private void restoreArtifactId( Properties properties, String artifactId )
 1644  
     {
 1645  36
         if ( StringUtils.isEmpty( artifactId ) )
 1646  
         {
 1647  0
             properties.remove( Constants.ARTIFACT_ID );
 1648  
         }
 1649  
         else
 1650  
         {
 1651  36
             properties.setProperty( Constants.ARTIFACT_ID, artifactId );
 1652  
         }
 1653  36
     }
 1654  
 
 1655  
     private void restoreParentArtifactId( Properties properties, String parentArtifactId )
 1656  
     {
 1657  36
         if ( StringUtils.isEmpty( parentArtifactId ) )
 1658  
         {
 1659  20
             properties.remove( Constants.PARENT_ARTIFACT_ID );
 1660  
         }
 1661  
         else
 1662  
         {
 1663  16
             properties.setProperty( Constants.PARENT_ARTIFACT_ID, parentArtifactId );
 1664  
         }
 1665  36
     }
 1666  
 
 1667  
     private String getReversedContent( String content, Properties properties )
 1668  
     {
 1669  55
         String result =
 1670  
             StringUtils.replace( StringUtils.replace( content, "$", "${symbol_dollar}" ), "\\", "${symbol_escape}" );
 1671  
 
 1672  55
         for ( Iterator<?> propertyIterator = properties.keySet().iterator(); propertyIterator.hasNext(); )
 1673  
         {
 1674  336
             String propertyKey = (String) propertyIterator.next();
 1675  
 
 1676  336
             result = StringUtils.replace( result, properties.getProperty( propertyKey ), "${" + propertyKey + "}" );
 1677  336
         }
 1678  
         // TODO: Replace velocity to a better engine...
 1679  55
         return "#set( $symbol_pound = '#' )\n" + "#set( $symbol_dollar = '$' )\n" + "#set( $symbol_escape = '\\' )\n"
 1680  
             + StringUtils.replace( result, "#", "${symbol_pound}" );
 1681  
     }
 1682  
 
 1683  
     private String getTemplateOutputDirectory()
 1684  
     {
 1685  6
         return Constants.SRC + File.separator + Constants.MAIN + File.separator + Constants.RESOURCES;
 1686  
     }
 1687  
 
 1688  
     private FileSet getUnpackagedFileSet( final boolean filtered, final String group, final List<String> groupFiles,
 1689  
                                           String defaultEncoding )
 1690  
     {
 1691  11
         Set<String> extensions = getExtensions( groupFiles );
 1692  
 
 1693  11
         List<String> includes = new ArrayList<String>();
 1694  11
         List<String> excludes = new ArrayList<String>();
 1695  
 
 1696  11
         for ( String extension : extensions )
 1697  
         {
 1698  11
             includes.add( "**/*." + extension );
 1699  
         }
 1700  
 
 1701  11
         return createFileSet( excludes, false, filtered, group, includes, defaultEncoding );
 1702  
     }
 1703  
 
 1704  
     private FileSet getUnpackagedFileSet( final boolean filtered, final Set<String> unpackagedExtensions,
 1705  
                                           final List<String> unpackagedFiles, final String group,
 1706  
                                           final Set<String> packagedExtensions, String defaultEncoding )
 1707  
     {
 1708  10
         List<String> includes = new ArrayList<String>();
 1709  10
         List<String> excludes = new ArrayList<String>();
 1710  
 
 1711  10
         for ( String extension : unpackagedExtensions )
 1712  
         {
 1713  11
             if ( packagedExtensions.contains( extension ) )
 1714  
             {
 1715  3
                 includes.addAll( archetypeFilesResolver.getFilesWithExtension( unpackagedFiles, extension ) );
 1716  
             }
 1717  
             else
 1718  
             {
 1719  8
                 includes.add( "**/*." + extension );
 1720  
             }
 1721  
         }
 1722  
 
 1723  10
         return createFileSet( excludes, false, filtered, group, includes, defaultEncoding );
 1724  
     }
 1725  
 
 1726  
     private static final String MAVEN_PROPERTIES =
 1727  
         "META-INF/maven/org.apache.maven.archetype/archetype-common/pom.properties";
 1728  
 
 1729  
     public String getArchetypeVersion()
 1730  
     {
 1731  12
         InputStream is = null;
 1732  
 
 1733  
         // This should actually come from the pom.properties at testing but it's not generated and put into the JAR, it
 1734  
         // happens as part of the JAR plugin which is crap as it makes testing inconsistent.
 1735  12
         String version = "version";
 1736  
 
 1737  
         try
 1738  
         {
 1739  12
             Properties properties = new Properties();
 1740  
 
 1741  12
             is = getClass().getClassLoader().getResourceAsStream( MAVEN_PROPERTIES );
 1742  
 
 1743  12
             if ( is != null )
 1744  
             {
 1745  12
                 properties.load( is );
 1746  
 
 1747  12
                 String property = properties.getProperty( "version" );
 1748  
 
 1749  12
                 if ( property != null )
 1750  
                 {
 1751  12
                     return property;
 1752  
                 }
 1753  
             }
 1754  
 
 1755  0
             return version;
 1756  
         }
 1757  0
         catch ( IOException e )
 1758  
         {
 1759  0
             return version;
 1760  
         }
 1761  
         finally
 1762  
         {
 1763  12
             IOUtil.close( is );
 1764  
         }
 1765  
     }
 1766  
 }