View Javadoc
1   package org.apache.maven.archetype.test;
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.ArchetypeGenerationRequest;
23  import org.apache.maven.archetype.ArchetypeGenerationResult;
24  import org.apache.maven.archetype.ArchetypeManager;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
27  import org.apache.maven.artifact.repository.MavenArtifactRepository;
28  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
29  import org.apache.maven.project.DefaultProjectBuildingRequest;
30  import org.apache.maven.project.ProjectBuildingRequest;
31  import org.apache.maven.repository.internal.MavenRepositorySystemSession;
32  import org.codehaus.plexus.PlexusTestCase;
33  
34  import java.io.File;
35  import java.util.Properties;
36  
37  import org.apache.maven.archetype.catalog.Archetype;
38  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
39  import org.codehaus.plexus.util.FileUtils;
40  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
41  
42  /** @author Jason van Zyl */
43  public class ArchetypeGenerationTest
44      extends PlexusTestCase
45  {
46      public void testProjectGenerationFromAnArchetype()
47          throws Exception
48      {
49          ArchetypeManager archetype = (ArchetypeManager) lookup( ArchetypeManager.ROLE );
50  
51          // In the embedder the localRepository will be retrieved from the embedder itself and users won't
52          // have to go through this muck.
53  
54          ArtifactRepository localRepository = createRepository(
55              new File( getBasedir(), "target/test-classes/repositories/local" )
56                  .toURI().toURL().toExternalForm(), "local-repo" );
57          
58          ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
59          MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
60          repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( "target/test-classes/repositories/central" ) );
61          buildingRequest.setRepositorySession( repositorySession );
62  
63          ArchetypeCatalog catalog = archetype.getLocalCatalog( buildingRequest );
64  
65          System.err.println( "archetypes => " + catalog.getArchetypes() );
66          // Here I am just grabbing a OldArchetype but in a UI you would take the OldArchetype objects and present
67          // them to the user.
68  
69          Archetype selection = catalog.getArchetypes().get( catalog.getArchetypes().size() - 1 );
70  
71          System.err.println( "Selected OldArchetype = " + selection );
72          // Now you will present a dialog, or whatever, and grab the following values.
73  
74          String groupId = "com.mycompany";
75  
76          String artifactId = "app";
77  
78          String version = "1.0.0";
79  
80          String packageName = "org.mycompany.app";
81  
82          // With the selected OldArchetype and the parameters you can create a generation request as follows:
83          File outputDirectory = new File( getBasedir(), "target/test-classes/projects/archetyper-generate-1" );
84          FileUtils.forceDelete(outputDirectory);
85  
86          ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest( selection )
87              .setOutputDirectory( outputDirectory.getAbsolutePath() )
88              .setLocalRepository( localRepository )
89              .setGroupId( groupId )
90              .setArtifactId( artifactId )
91              .setVersion( version )
92              .setPackage( packageName );
93  
94          Properties archetypeRequiredProperties = new Properties();
95          archetypeRequiredProperties.setProperty( "property-with-default-1", "value-1" );
96          archetypeRequiredProperties.setProperty( "property-with-default-2", "value-2" );
97          archetypeRequiredProperties.setProperty( "property-with-default-3", "value-3" );
98          archetypeRequiredProperties.setProperty( "property-with-default-4", "value-4" );
99          archetypeRequiredProperties.setProperty( "property-without-default-1", "some-value-1" );
100         archetypeRequiredProperties.setProperty( "property-without-default-2", "some-value-2" );
101         archetypeRequiredProperties.setProperty( "property-without-default-3", "some-value-3" );
102         archetypeRequiredProperties.setProperty( "property-without-default-4", "some-value-4" );
103         archetypeRequiredProperties.setProperty( "property_underscored_1", "prop1" );
104         archetypeRequiredProperties.setProperty( "property_underscored-2", "prop2" );
105         agr.setProperties( archetypeRequiredProperties );
106         agr.setProjectBuildingRequest( buildingRequest );
107         
108         // Then generate away!
109 
110         ArchetypeGenerationResult result = archetype.generateProjectFromArchetype( agr );
111 
112         if ( result.getCause() != null )
113         {
114             result.getCause().printStackTrace( System.err );
115             fail( result.getCause().getMessage() );
116         }
117     }
118     
119     private ArtifactRepository createRepository( String url, String repositoryId )
120     {
121         String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
122 
123         String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
124 
125         ArtifactRepositoryPolicy snapshotsPolicy =
126             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
127 
128         ArtifactRepositoryPolicy releasesPolicy =
129             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
130         
131         return new MavenArtifactRepository( repositoryId, url, new DefaultRepositoryLayout() , snapshotsPolicy,
132                                             releasesPolicy );
133     }
134 
135 
136 }