Coverage Report - org.apache.maven.archetype.generator.DefaultFilesetArchetypeGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultFilesetArchetypeGenerator
87 %
232/265
72 %
74/102
4,208
 
 1  
 package org.apache.maven.archetype.generator;
 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.common.ArchetypeArtifactManager;
 23  
 import org.apache.maven.archetype.common.ArchetypeFilesResolver;
 24  
 import org.apache.maven.archetype.common.Constants;
 25  
 import org.apache.maven.archetype.common.PomManager;
 26  
 import org.apache.maven.archetype.exception.ArchetypeGenerationFailure;
 27  
 import org.apache.maven.archetype.exception.ArchetypeNotConfigured;
 28  
 import org.apache.maven.archetype.exception.InvalidPackaging;
 29  
 import org.apache.maven.archetype.exception.OutputFileExists;
 30  
 import org.apache.maven.archetype.exception.PomFileExists;
 31  
 import org.apache.maven.archetype.exception.ProjectDirectoryExists;
 32  
 import org.apache.maven.archetype.exception.UnknownArchetype;
 33  
 import org.apache.maven.archetype.metadata.AbstractArchetypeDescriptor;
 34  
 import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
 35  
 import org.apache.maven.archetype.metadata.FileSet;
 36  
 import org.apache.maven.archetype.metadata.ModuleDescriptor;
 37  
 import org.apache.velocity.VelocityContext;
 38  
 import org.apache.velocity.context.Context;
 39  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 40  
 import org.codehaus.plexus.util.FileUtils;
 41  
 import org.codehaus.plexus.util.IOUtil;
 42  
 import org.codehaus.plexus.util.StringUtils;
 43  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 44  
 import org.codehaus.plexus.velocity.VelocityComponent;
 45  
 import org.dom4j.DocumentException;
 46  
 
 47  
 import java.io.File;
 48  
 import java.io.FileNotFoundException;
 49  
 import java.io.FileOutputStream;
 50  
 import java.io.IOException;
 51  
 import java.io.InputStream;
 52  
 import java.io.OutputStream;
 53  
 import java.io.OutputStreamWriter;
 54  
 import java.io.StringWriter;
 55  
 import java.io.Writer;
 56  
 import java.util.ArrayList;
 57  
 import java.util.Iterator;
 58  
 import java.util.List;
 59  
 import java.util.regex.Pattern;
 60  
 import java.util.zip.ZipEntry;
 61  
 import java.util.zip.ZipFile;
 62  
 import org.apache.maven.archetype.ArchetypeGenerationRequest;
 63  
 import org.apache.maven.archetype.metadata.RequiredProperty;
 64  
 
 65  
 /** @plexus.component */
 66  17
 public class DefaultFilesetArchetypeGenerator
 67  
     extends AbstractLogEnabled
 68  
     implements FilesetArchetypeGenerator
 69  
 {
 70  
     /** @plexus.requirement */
 71  
     private ArchetypeArtifactManager archetypeArtifactManager;
 72  
 
 73  
     /** @plexus.requirement */
 74  
     private ArchetypeFilesResolver archetypeFilesResolver;
 75  
 
 76  
     /** @plexus.requirement */
 77  
     private PomManager pomManager;
 78  
 
 79  
     /** @plexus.requirement */
 80  
     private VelocityComponent velocity;
 81  
 
 82  
     /**
 83  
      * Token delimiter.
 84  
      */
 85  
     private static final String DELIMITER = "__";
 86  
 
 87  
     /**
 88  
      * Pattern used to detect tokens in a string. Tokens are any text surrounded
 89  
      * by the delimiter.
 90  
      */
 91  1
     private static final Pattern TOKEN_PATTERN = Pattern.compile( ".*" + DELIMITER + ".*" + DELIMITER + ".*" );
 92  
 
 93  
     public void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile )
 94  
         throws UnknownArchetype, ArchetypeNotConfigured, ProjectDirectoryExists, PomFileExists, OutputFileExists,
 95  
         ArchetypeGenerationFailure
 96  
     {
 97  13
         ClassLoader old = Thread.currentThread().getContextClassLoader();
 98  
 
 99  
         try
 100  
         {
 101  13
             ArchetypeDescriptor archetypeDescriptor =
 102  
                 archetypeArtifactManager.getFileSetArchetypeDescriptor( archetypeFile );
 103  
 
 104  13
             if ( !isArchetypeConfigured( archetypeDescriptor, request ) )
 105  
             {
 106  1
                 if ( request.isInteractiveMode() )
 107  
                 {
 108  0
                     throw new ArchetypeNotConfigured( "No archetype was chosen.", null );
 109  
                 }
 110  
 
 111  1
                 StringBuffer exceptionMessage =
 112  
                     new StringBuffer( "Archetype " + request.getArchetypeGroupId() + ":"
 113  
                         + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() + " is not configured" );
 114  
 
 115  1
                 List<String> missingProperties = new ArrayList<String>( 0 );
 116  1
                 for ( RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties() )
 117  
                 {
 118  8
                     if ( StringUtils.isEmpty( request.getProperties().getProperty( requiredProperty.getKey() ) ) )
 119  
                     {
 120  8
                         exceptionMessage.append( "\n\tProperty " + requiredProperty.getKey() + " is missing." );
 121  
 
 122  8
                         missingProperties.add( requiredProperty.getKey() );
 123  
                     }
 124  
                 }
 125  
 
 126  1
                 throw new ArchetypeNotConfigured( exceptionMessage.toString(), missingProperties );
 127  
             }
 128  
 
 129  12
             Context context = prepareVelocityContext( request );
 130  
 
 131  12
             String packageName = request.getPackage();
 132  12
             String artifactId = request.getArtifactId();
 133  12
             File outputDirectoryFile = new File( request.getOutputDirectory(), artifactId );
 134  12
             File basedirPom = new File( request.getOutputDirectory(), Constants.ARCHETYPE_POM );
 135  12
             File pom = new File( outputDirectoryFile, Constants.ARCHETYPE_POM );
 136  
 
 137  12
             List<String> archetypeResources = archetypeArtifactManager.getFilesetArchetypeResources( archetypeFile );
 138  
 
 139  12
             ZipFile archetypeZipFile = archetypeArtifactManager.getArchetypeZipFile( archetypeFile );
 140  
 
 141  12
             ClassLoader archetypeJarLoader = archetypeArtifactManager.getArchetypeJarLoader( archetypeFile );
 142  
 
 143  12
             Thread.currentThread().setContextClassLoader( archetypeJarLoader );
 144  
 
 145  12
             if ( archetypeDescriptor.isPartial() )
 146  
             {
 147  4
                 getLogger().debug( "Processing partial archetype " + archetypeDescriptor.getName() );
 148  4
                 if ( outputDirectoryFile.exists() )
 149  
                 {
 150  2
                     if ( !pom.exists() )
 151  
                     {
 152  0
                         throw new PomFileExists( "This is a partial archetype and the pom.xml file doesn't exist." );
 153  
                     }
 154  
 
 155  2
                     processPomWithMerge( context, pom, "" );
 156  
 
 157  2
                     processArchetypeTemplatesWithWarning( archetypeDescriptor, archetypeResources, archetypeZipFile,
 158  
                                                           "", context, packageName, outputDirectoryFile );
 159  
                 }
 160  
                 else
 161  
                 {
 162  2
                     if ( basedirPom.exists() )
 163  
                     {
 164  1
                         processPomWithMerge( context, basedirPom, "" );
 165  
 
 166  1
                         processArchetypeTemplatesWithWarning( archetypeDescriptor, archetypeResources,
 167  
                                                               archetypeZipFile, "", context, packageName,
 168  
                                                               new File( request.getOutputDirectory() ) );
 169  
                     }
 170  
                     else
 171  
                     {
 172  1
                         processPom( context, pom, "" );
 173  
 
 174  1
                         processArchetypeTemplates( archetypeDescriptor, archetypeResources, archetypeZipFile, "",
 175  
                                                    context, packageName, outputDirectoryFile );
 176  
                     }
 177  
                 }
 178  
 
 179  4
                 if ( archetypeDescriptor.getModules().size() > 0 )
 180  
                 {
 181  0
                     getLogger().info( "Modules ignored in partial mode" );
 182  
                 }
 183  
             }
 184  
             else
 185  
             {
 186  8
                 getLogger().debug( "Processing complete archetype " + archetypeDescriptor.getName() );
 187  8
                 if ( outputDirectoryFile.exists() && pom.exists() )
 188  
                 {
 189  0
                     throw new ProjectDirectoryExists( "A Maven 2 project already exists in the directory "
 190  
                         + outputDirectoryFile.getPath() );
 191  
                 }
 192  
 
 193  8
                 if ( outputDirectoryFile.exists() )
 194  
                 {
 195  0
                     getLogger().warn( "The directory " + outputDirectoryFile.getPath() + " already exists." );
 196  
                 }
 197  
 
 198  8
                 context.put( "rootArtifactId", artifactId );
 199  
 
 200  8
                 processFilesetModule( artifactId, artifactId, archetypeResources, pom, archetypeZipFile, "",
 201  
                                       basedirPom, outputDirectoryFile, packageName, archetypeDescriptor, context );
 202  
             }
 203  
 
 204  
             // ----------------------------------------------------------------------
 205  
             // Log message on OldArchetype creation
 206  
             // ----------------------------------------------------------------------
 207  12
             if ( getLogger().isInfoEnabled() )
 208  
             {
 209  12
                 getLogger().info( "project created from Archetype in dir: " + outputDirectoryFile.getAbsolutePath() );
 210  
             }
 211  
         }
 212  0
         catch ( FileNotFoundException ex )
 213  
         {
 214  0
             throw new ArchetypeGenerationFailure( ex );
 215  
         }
 216  0
         catch ( IOException ex )
 217  
         {
 218  0
             throw new ArchetypeGenerationFailure( ex );
 219  
         }
 220  0
         catch ( XmlPullParserException ex )
 221  
         {
 222  0
             throw new ArchetypeGenerationFailure( ex );
 223  
         }
 224  0
         catch ( DocumentException ex )
 225  
         {
 226  0
             throw new ArchetypeGenerationFailure( ex );
 227  
         }
 228  0
         catch ( ArchetypeGenerationFailure ex )
 229  
         {
 230  0
             throw new ArchetypeGenerationFailure( ex );
 231  
         }
 232  0
         catch ( InvalidPackaging ex )
 233  
         {
 234  0
             throw new ArchetypeGenerationFailure( ex );
 235  
         }
 236  
         finally
 237  
         {
 238  13
             Thread.currentThread().setContextClassLoader( old );
 239  12
         }
 240  12
     }
 241  
 
 242  
     public String getPackageAsDirectory( String packageName )
 243  
     {
 244  76
         return StringUtils.replace( packageName, ".", "/" );
 245  
     }
 246  
 
 247  
     private boolean copyFile( final File outFile, final String template, final boolean failIfExists,
 248  
                            final ZipFile archetypeZipFile )
 249  
         throws FileNotFoundException, OutputFileExists, IOException
 250  
     {
 251  11
         getLogger().debug( "Copying file " + template );
 252  
 
 253  11
         if ( failIfExists && outFile.exists() )
 254  
         {
 255  0
             throw new OutputFileExists( "Don't rewrite file " + outFile.getName() );
 256  
         }
 257  11
         else if ( outFile.exists() )
 258  
         {
 259  0
             getLogger().warn( "CP Don't override file " + outFile );
 260  
 
 261  0
             return false;
 262  
         }
 263  
 
 264  11
         ZipEntry input = archetypeZipFile.getEntry( Constants.ARCHETYPE_RESOURCES + "/" + template );
 265  
 
 266  11
         if ( input.isDirectory() )
 267  
         {
 268  0
             outFile.mkdirs();
 269  
         }
 270  
         else
 271  
         {
 272  11
             InputStream inputStream = null;
 273  11
             OutputStream out = null;
 274  
             try
 275  
             {
 276  11
                 inputStream = archetypeZipFile.getInputStream( input );
 277  
 
 278  11
                 outFile.getParentFile().mkdirs();
 279  
 
 280  11
                 out = new FileOutputStream( outFile );
 281  
 
 282  11
                 IOUtil.copy( inputStream, out );
 283  
             }
 284  
             finally
 285  
             {
 286  11
                 IOUtil.close( inputStream );
 287  11
                 IOUtil.close( out );
 288  11
             }
 289  
         }
 290  
 
 291  11
         return true;
 292  
     }
 293  
 
 294  
     private int copyFiles( String directory, List<String> fileSetResources, boolean packaged, String packageName,
 295  
                            File outputDirectoryFile, ZipFile archetypeZipFile, String moduleOffset,
 296  
                            boolean failIfExists, Context context )
 297  
         throws OutputFileExists, FileNotFoundException, IOException
 298  
     {
 299  13
         int count = 0;
 300  
 
 301  13
         for ( String template : fileSetResources )
 302  
         {
 303  11
             File outputFile =
 304  
                 getOutputFile( template, directory, outputDirectoryFile, packaged, packageName, moduleOffset, context );
 305  
 
 306  11
             if ( copyFile( outputFile, template, failIfExists, archetypeZipFile ) )
 307  
             {
 308  11
                 count++;
 309  
             }
 310  11
         }
 311  
 
 312  13
         return count;
 313  
     }
 314  
 
 315  
     private String getEncoding( String archetypeEncoding )
 316  
     {
 317  80
         return StringUtils.isEmpty( archetypeEncoding ) ? "UTF-8" : archetypeEncoding;
 318  
     }
 319  
 
 320  
     private String getOffsetSeparator( String moduleOffset )
 321  
     {
 322  23
         return StringUtils.isEmpty( moduleOffset ) ? "/" : ( "/" + moduleOffset + "/" );
 323  
     }
 324  
 
 325  
     private File getOutputFile( String template, String directory, File outputDirectoryFile, boolean packaged,
 326  
                                 String packageName, String moduleOffset, Context context )
 327  
     {
 328  159
         String templateName = StringUtils.replaceOnce( template, directory, "" );
 329  
 
 330  159
         String outputFileName =
 331  
             directory + "/" + ( packaged ? getPackageAsDirectory( packageName ) : "" ) + "/"
 332  
                 + templateName.substring( moduleOffset.length() );
 333  
 
 334  159
         if ( TOKEN_PATTERN.matcher( outputFileName ).matches() )
 335  
         {
 336  10
             outputFileName = replaceFilenameTokens( outputFileName, context );
 337  
         }
 338  
 
 339  159
         return new File( outputDirectoryFile, outputFileName );
 340  
     }
 341  
 
 342  
     /**
 343  
      * Replaces all tokens (text surrounded by the {@link #DELIMITER}) within
 344  
      * the given string, using properties contained within the context. If a
 345  
      * property does not exist in the context, the token is left unmodified
 346  
      * and a warning is logged.
 347  
      *
 348  
      * @param filePath the file name and path to be interpolated
 349  
      * @param context contains the available properties
 350  
      */
 351  
     private String replaceFilenameTokens( final String filePath, final Context context )
 352  
     {
 353  10
         String interpolatedResult = filePath;
 354  
 
 355  10
         int start = 0;
 356  
 
 357  
         while ( true )
 358  
         {
 359  24
             start = interpolatedResult.indexOf( DELIMITER, start );
 360  
 
 361  24
             if ( start == -1 )
 362  
             {
 363  10
                 break;
 364  
             }
 365  
 
 366  14
             int end = interpolatedResult.indexOf( DELIMITER, start + DELIMITER.length() );
 367  
 
 368  14
             if ( end == -1 )
 369  
             {
 370  0
                 break;
 371  
             }
 372  
 
 373  14
             String propertyToken = interpolatedResult.substring( start + DELIMITER.length(), end );
 374  
 
 375  14
             String contextPropertyValue = (String) context.get( propertyToken );
 376  
 
 377  14
             if ( contextPropertyValue != null && contextPropertyValue.trim().length() > 0 )
 378  
             {
 379  12
                 String search = DELIMITER + propertyToken + DELIMITER;
 380  
 
 381  12
                 if ( getLogger().isDebugEnabled() )
 382  
                 {
 383  0
                     getLogger().debug( "Replacing '" + search + "' in file path '" + interpolatedResult
 384  
                                            + "' with value '" + contextPropertyValue + "'." );
 385  
                 }
 386  
 
 387  12
                 interpolatedResult = StringUtils.replace( interpolatedResult, search, contextPropertyValue );
 388  
 
 389  12
                 end = end + contextPropertyValue.length() - search.length();
 390  12
             }
 391  
             else
 392  
             {
 393  
                 // Need to skip the undefined property
 394  2
                 getLogger().warn( "Property '" + propertyToken + "' was not specified, so the token in '"
 395  
                                       + interpolatedResult + "' is not being replaced." );
 396  
             }
 397  
 
 398  14
             start = end + DELIMITER.length() + 1;
 399  14
         }
 400  
 
 401  10
         if ( getLogger().isDebugEnabled() )
 402  
         {
 403  0
             getLogger().debug( "Final interpolated file path: '" + interpolatedResult + "'" );
 404  
         }
 405  
 
 406  10
         return interpolatedResult;
 407  
     }
 408  
 
 409  
     private String getPackageInPathFormat( String aPackage )
 410  
     {
 411  12
         return StringUtils.replace( aPackage, ".", "/" );
 412  
     }
 413  
 
 414  
     private boolean isArchetypeConfigured( ArchetypeDescriptor archetypeDescriptor, ArchetypeGenerationRequest request )
 415  
     {
 416  13
         for ( RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties() )
 417  
         {
 418  42
             if ( StringUtils.isEmpty( request.getProperties().getProperty( requiredProperty.getKey() ) ) )
 419  
             {
 420  1
                 return false;
 421  
             }
 422  
         }
 423  
 
 424  12
         return true;
 425  
     }
 426  
 
 427  
     private void setParentArtifactId( Context context, String artifactId )
 428  
     {
 429  6
         context.put( Constants.PARENT_ARTIFACT_ID, artifactId );
 430  6
     }
 431  
 
 432  
     private Context prepareVelocityContext( ArchetypeGenerationRequest request )
 433  
     {
 434  12
         Context context = new VelocityContext();
 435  12
         context.put( Constants.GROUP_ID, request.getGroupId() );
 436  12
         context.put( Constants.ARTIFACT_ID, request.getArtifactId() );
 437  12
         context.put( Constants.VERSION, request.getVersion() );
 438  12
         context.put( Constants.PACKAGE, request.getPackage() );
 439  12
         final String packageInPathFormat = getPackageInPathFormat( request.getPackage() );
 440  12
         context.put( Constants.PACKAGE_IN_PATH_FORMAT, packageInPathFormat );
 441  
 
 442  12
         if ( getLogger().isInfoEnabled() )
 443  
         {
 444  12
             getLogger().info( "----------------------------------------------------------------------------" );
 445  
 
 446  12
             getLogger().info( "Using following parameters for creating project from Archetype: "
 447  
                                   + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() );
 448  
 
 449  12
             getLogger().info( "----------------------------------------------------------------------------" );
 450  12
             getLogger().info( "Parameter: " + Constants.GROUP_ID + ", Value: " + request.getGroupId() );
 451  12
             getLogger().info( "Parameter: " + Constants.ARTIFACT_ID + ", Value: " + request.getArtifactId() );
 452  12
             getLogger().info( "Parameter: " + Constants.VERSION + ", Value: " + request.getVersion() );
 453  12
             getLogger().info( "Parameter: " + Constants.PACKAGE + ", Value: " + request.getPackage() );
 454  12
             getLogger().info( "Parameter: " + Constants.PACKAGE_IN_PATH_FORMAT + ", Value: " + packageInPathFormat );
 455  
         }
 456  
 
 457  12
         for ( Iterator<?> iterator = request.getProperties().keySet().iterator(); iterator.hasNext(); )
 458  
         {
 459  73
             String key = (String) iterator.next();
 460  
 
 461  73
             Object value = request.getProperties().getProperty( key );
 462  
 
 463  73
             context.put( key, value );
 464  
 
 465  73
             if ( getLogger().isInfoEnabled() )
 466  
             {
 467  73
                 getLogger().info( "Parameter: " + key + ", Value: " + value );
 468  
             }
 469  73
         }
 470  12
         return context;
 471  
     }
 472  
 
 473  
     private void processArchetypeTemplates( AbstractArchetypeDescriptor archetypeDescriptor,
 474  
                                             List<String> archetypeResources, ZipFile archetypeZipFile,
 475  
                                             String moduleOffset, Context context, String packageName,
 476  
                                             File outputDirectoryFile )
 477  
         throws OutputFileExists, ArchetypeGenerationFailure, FileNotFoundException, IOException
 478  
     {
 479  20
         processTemplates( packageName, outputDirectoryFile, context, archetypeDescriptor, archetypeResources,
 480  
                           archetypeZipFile, moduleOffset, false );
 481  20
     }
 482  
 
 483  
     private void processArchetypeTemplatesWithWarning( ArchetypeDescriptor archetypeDescriptor,
 484  
                                                        List<String> archetypeResources, ZipFile archetypeZipFile,
 485  
                                                        String moduleOffset, Context context, String packageName,
 486  
                                                        File outputDirectoryFile )
 487  
         throws OutputFileExists, ArchetypeGenerationFailure, FileNotFoundException, IOException
 488  
     {
 489  3
         processTemplates( packageName, outputDirectoryFile, context, archetypeDescriptor, archetypeResources,
 490  
                           archetypeZipFile, moduleOffset, true );
 491  3
     }
 492  
 
 493  
     private int processFileSet( String directory, List<String> fileSetResources, boolean packaged, String packageName,
 494  
                                 Context context, File outputDirectoryFile, String moduleOffset,
 495  
                                 String archetypeEncoding, boolean failIfExists )
 496  
         throws OutputFileExists, ArchetypeGenerationFailure
 497  
     {
 498  57
         int count = 0;
 499  
 
 500  57
         for ( String template : fileSetResources )
 501  
         {
 502  78
             File outputFile =
 503  
                 getOutputFile( template, directory, outputDirectoryFile, packaged, packageName, moduleOffset, context );
 504  
 
 505  78
             if ( processTemplate( outputFile, context, Constants.ARCHETYPE_RESOURCES + "/" + template,
 506  
                                   archetypeEncoding, failIfExists ) )
 507  
             {
 508  76
                 count++;
 509  
             }
 510  78
         }
 511  
 
 512  57
         return count;
 513  
     }
 514  
 
 515  
     private void processFilesetModule( String rootArtifactId, String artifactId, final List<String> archetypeResources,
 516  
                                        File pom, final ZipFile archetypeZipFile, String moduleOffset, File basedirPom,
 517  
                                        File outputDirectoryFile, final String packageName,
 518  
                                        final AbstractArchetypeDescriptor archetypeDescriptor, final Context context )
 519  
         throws DocumentException, XmlPullParserException, ArchetypeGenerationFailure, InvalidPackaging, IOException,
 520  
         OutputFileExists
 521  
     {
 522  19
         outputDirectoryFile.mkdirs();
 523  19
         getLogger().debug( "Processing module " + artifactId );
 524  19
         getLogger().debug( "Processing module rootArtifactId " + rootArtifactId );
 525  19
         getLogger().debug( "Processing module pom " + pom );
 526  19
         getLogger().debug( "Processing module moduleOffset " + moduleOffset );
 527  19
         getLogger().debug( "Processing module outputDirectoryFile " + outputDirectoryFile );
 528  
 
 529  19
         processFilesetProject( archetypeDescriptor,
 530  
                                StringUtils.replace( artifactId, "${rootArtifactId}", rootArtifactId ),
 531  
                                archetypeResources, pom, archetypeZipFile, moduleOffset, context, packageName,
 532  
                                outputDirectoryFile, basedirPom );
 533  
 
 534  19
         String parentArtifactId = (String) context.get( Constants.PARENT_ARTIFACT_ID );
 535  
 
 536  19
         Iterator<ModuleDescriptor> subprojects = archetypeDescriptor.getModules().iterator();
 537  
 
 538  19
         if ( subprojects.hasNext() )
 539  
         {
 540  6
             getLogger().debug( artifactId + " has modules (" + archetypeDescriptor.getModules() + ")" );
 541  
 
 542  6
             setParentArtifactId( context, StringUtils.replace( artifactId, "${rootArtifactId}", rootArtifactId ) );
 543  
         }
 544  
 
 545  30
         while ( subprojects.hasNext() )
 546  
         {
 547  11
             ModuleDescriptor project = subprojects.next();
 548  
 
 549  11
             File moduleOutputDirectoryFile =
 550  
                 new File( outputDirectoryFile,
 551  
                           StringUtils.replace( project.getDir(), "__rootArtifactId__", rootArtifactId ) );
 552  
 
 553  11
             context.put( Constants.ARTIFACT_ID,
 554  
                          StringUtils.replace( project.getId(), "${rootArtifactId}", rootArtifactId ) );
 555  
 
 556  11
             processFilesetModule( rootArtifactId,
 557  
                                   StringUtils.replace( project.getDir(), "__rootArtifactId__", rootArtifactId ),
 558  
                                   archetypeResources,
 559  
                                   new File( moduleOutputDirectoryFile, Constants.ARCHETYPE_POM ), archetypeZipFile,
 560  
                                   ( StringUtils.isEmpty( moduleOffset ) ? "" : ( moduleOffset + "/" ) )
 561  
                                       + StringUtils.replace( project.getDir(), "${rootArtifactId}", rootArtifactId ),
 562  
                                   pom, moduleOutputDirectoryFile, packageName, project, context );
 563  11
         }
 564  
 
 565  19
         restoreParentArtifactId( context, parentArtifactId );
 566  
 
 567  19
         getLogger().debug( "Processed " + artifactId );
 568  19
     }
 569  
 
 570  
     private void processFilesetProject( final AbstractArchetypeDescriptor archetypeDescriptor, final String moduleId,
 571  
                                         final List<String> archetypeResources, final File pom,
 572  
                                         final ZipFile archetypeZipFile, String moduleOffset, final Context context,
 573  
                                         final String packageName, final File outputDirectoryFile, final File basedirPom )
 574  
         throws DocumentException, XmlPullParserException, ArchetypeGenerationFailure, InvalidPackaging, IOException,
 575  
         FileNotFoundException, OutputFileExists
 576  
     {
 577  19
         getLogger().debug( "Processing fileset project moduleId " + moduleId );
 578  19
         getLogger().debug( "Processing fileset project pom " + pom );
 579  19
         getLogger().debug( "Processing fileset project moduleOffset " + moduleOffset );
 580  19
         getLogger().debug( "Processing fileset project outputDirectoryFile " + outputDirectoryFile );
 581  19
         getLogger().debug( "Processing fileset project basedirPom " + basedirPom );
 582  
 
 583  19
         if ( basedirPom.exists() )
 584  
         {
 585  12
             processPomWithParent( context, pom, moduleOffset, basedirPom, moduleId );
 586  
         }
 587  
         else
 588  
         {
 589  7
             processPom( context, pom, moduleOffset );
 590  
         }
 591  
 
 592  19
         processArchetypeTemplates( archetypeDescriptor, archetypeResources, archetypeZipFile, moduleOffset, context,
 593  
                                    packageName, outputDirectoryFile );
 594  19
     }
 595  
 
 596  
     private void processPom( Context context, File pom, String moduleOffset )
 597  
         throws OutputFileExists, ArchetypeGenerationFailure
 598  
     {
 599  8
         getLogger().debug( "Processing pom " + pom );
 600  
 
 601  8
         processTemplate( pom, context, Constants.ARCHETYPE_RESOURCES + getOffsetSeparator( moduleOffset )
 602  
             + Constants.ARCHETYPE_POM, getEncoding( null ), true );
 603  8
     }
 604  
 
 605  
     private void processPomWithMerge( Context context, File pom, String moduleOffset )
 606  
         throws OutputFileExists, IOException, XmlPullParserException, ArchetypeGenerationFailure
 607  
     {
 608  3
         getLogger().debug( "Processing pom " + pom + " with merge" );
 609  
 
 610  3
         File temporaryPom = getTemporaryFile( pom );
 611  
 
 612  3
         processTemplate( temporaryPom, context, Constants.ARCHETYPE_RESOURCES + getOffsetSeparator( moduleOffset )
 613  
             + Constants.ARCHETYPE_POM, getEncoding( null ), true );
 614  
 
 615  3
         pomManager.mergePoms( pom, temporaryPom );
 616  
 
 617  
         // getTemporaryFile sets deleteOnExit. Lets try to delete and then make sure deleteOnExit is
 618  
         // still set. Windows has issues deleting files with certain JDKs.
 619  
         try
 620  
         {
 621  3
             FileUtils.forceDelete( temporaryPom );
 622  
         }
 623  0
         catch ( IOException e )
 624  
         {
 625  0
             temporaryPom.deleteOnExit();
 626  3
         }
 627  3
     }
 628  
 
 629  
     private void processPomWithParent( Context context, File pom, String moduleOffset, File basedirPom, String moduleId )
 630  
         throws OutputFileExists, XmlPullParserException, DocumentException, IOException, InvalidPackaging,
 631  
         ArchetypeGenerationFailure
 632  
     {
 633  12
         getLogger().debug( "Processing pom " + pom + " with parent " + basedirPom );
 634  
 
 635  12
         processTemplate( pom, context, Constants.ARCHETYPE_RESOURCES + getOffsetSeparator( moduleOffset )
 636  
             + Constants.ARCHETYPE_POM, getEncoding( null ), true );
 637  
 
 638  12
         getLogger().debug( "Adding module " + moduleId );
 639  
 
 640  12
         pomManager.addModule( basedirPom, moduleId );
 641  
 
 642  12
         pomManager.addParent( pom, basedirPom );
 643  12
     }
 644  
 
 645  
     @SuppressWarnings( "deprecation" )
 646  
     private boolean processTemplate( File outFile, Context context, String templateFileName, String encoding,
 647  
                                      boolean failIfExists )
 648  
         throws OutputFileExists, ArchetypeGenerationFailure
 649  
     {
 650  101
         templateFileName = templateFileName.replace( File.separatorChar, '/' );
 651  
 
 652  101
         String localTemplateFileName = templateFileName.replace( '/', File.separatorChar );
 653  101
         if ( !templateFileName.equals( localTemplateFileName )
 654  
             && !velocity.getEngine().templateExists( templateFileName )
 655  
             && velocity.getEngine().templateExists( localTemplateFileName ) )
 656  
         {
 657  0
             templateFileName = localTemplateFileName;
 658  
         }
 659  
 
 660  101
         getLogger().debug( "Processing template " + templateFileName );
 661  
 
 662  101
         if ( outFile.exists() )
 663  
         {
 664  2
             if ( failIfExists )
 665  
             {
 666  0
                 throw new OutputFileExists( "Don't override file " + outFile.getAbsolutePath() );
 667  
             }
 668  
 
 669  2
             getLogger().warn( "Don't override file " + outFile );
 670  
 
 671  2
             return false;
 672  
         }
 673  
 
 674  99
         if ( templateFileName.endsWith( "/" ) )
 675  
         {
 676  0
             getLogger().debug( "Creating directory " + outFile );
 677  
 
 678  0
             outFile.mkdirs();
 679  
 
 680  0
             return true;
 681  
         }
 682  
 
 683  99
         if ( !outFile.getParentFile().exists() )
 684  
         {
 685  20
             outFile.getParentFile().mkdirs();
 686  
         }
 687  
 
 688  99
         getLogger().debug( "Merging into " + outFile );
 689  
 
 690  99
         Writer writer = null;
 691  
 
 692  
         try
 693  
         {
 694  99
             StringWriter stringWriter = new StringWriter();
 695  
 
 696  99
             velocity.getEngine().mergeTemplate( templateFileName, encoding, context, stringWriter );
 697  
 
 698  99
             writer = new OutputStreamWriter( new FileOutputStream( outFile ), encoding );
 699  
 
 700  99
             writer.write( StringUtils.unifyLineSeparators( stringWriter.toString() ) );
 701  
 
 702  99
             writer.flush();
 703  
         }
 704  0
         catch ( Exception e )
 705  
         {
 706  0
             throw new ArchetypeGenerationFailure( "Error merging velocity templates: " + e.getMessage(), e );
 707  
         }
 708  
         finally
 709  
         {
 710  99
             IOUtil.close( writer );
 711  99
         }
 712  
 
 713  99
         return true;
 714  
     }
 715  
 
 716  
     private void processTemplates( String packageName, File outputDirectoryFile, Context context,
 717  
                                    AbstractArchetypeDescriptor archetypeDescriptor, List<String> archetypeResources,
 718  
                                    ZipFile archetypeZipFile, String moduleOffset, boolean failIfExists )
 719  
         throws OutputFileExists, ArchetypeGenerationFailure, FileNotFoundException, IOException
 720  
     {
 721  23
         Iterator<FileSet> iterator = archetypeDescriptor.getFileSets().iterator();
 722  23
         if ( iterator.hasNext() )
 723  
         {
 724  22
             getLogger().debug( "Processing filesets" + "\n  " + archetypeResources );
 725  
         }
 726  
 
 727  23
         int count = 0;
 728  93
         while ( iterator.hasNext() )
 729  
         {
 730  70
             FileSet fileSet = iterator.next();
 731  70
             count++;
 732  
 
 733  70
             List<String> fileSetResources =
 734  
                 archetypeFilesResolver.filterFiles( moduleOffset, fileSet, archetypeResources );
 735  
 
 736  
             // This creates an empty directory, even if there is no file to process
 737  
             // Fix for ARCHETYPE-57
 738  70
             getOutputFile( moduleOffset, fileSet.getDirectory(), outputDirectoryFile, fileSet.isPackaged(),
 739  
                            packageName, moduleOffset, context ).mkdirs();
 740  
 
 741  70
             if ( fileSet.isFiltered() )
 742  
             {
 743  57
                 getLogger().debug( "    Processing fileset " + fileSet + " -> " + fileSetResources.size() + ":\n      "
 744  
                                        + fileSetResources );
 745  
 
 746  57
                 int processed =
 747  
                     processFileSet( fileSet.getDirectory(), fileSetResources, fileSet.isPackaged(), packageName,
 748  
                                     context, outputDirectoryFile, moduleOffset, getEncoding( fileSet.getEncoding() ),
 749  
                                     failIfExists );
 750  
 
 751  57
                 getLogger().debug( "    Processed " + processed + " files." );
 752  57
             }
 753  
             else
 754  
             {
 755  13
                 getLogger().debug( "    Copying fileset " + fileSet + " -> " + fileSetResources.size() + ":\n      "
 756  
                                        + fileSetResources );
 757  
 
 758  13
                 int copied =
 759  
                     copyFiles( fileSet.getDirectory(), fileSetResources, fileSet.isPackaged(), packageName,
 760  
                                outputDirectoryFile, archetypeZipFile, moduleOffset, failIfExists, context );
 761  
 
 762  13
                 getLogger().debug( "    Copied " + copied + " files." );
 763  
             }
 764  70
         }
 765  
 
 766  23
         getLogger().debug( "Processed " + count + " filesets" );
 767  23
     }
 768  
 
 769  
     private void restoreParentArtifactId( Context context, String parentArtifactId )
 770  
     {
 771  19
         if ( StringUtils.isEmpty( parentArtifactId ) )
 772  
         {
 773  8
             context.remove( Constants.PARENT_ARTIFACT_ID );
 774  
         }
 775  
         else
 776  
         {
 777  11
             context.put( Constants.PARENT_ARTIFACT_ID, parentArtifactId );
 778  
         }
 779  19
     }
 780  
 
 781  
     private File getTemporaryFile( File file )
 782  
     {
 783  3
         File tmp = FileUtils.createTempFile( file.getName(), Constants.TMP, file.getParentFile() );
 784  
 
 785  3
         tmp.deleteOnExit();
 786  
 
 787  3
         return tmp;
 788  
     }
 789  
 }