View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.archetype.test;
20  
21  import java.io.File;
22  import java.util.Properties;
23  
24  import org.apache.maven.archetype.ArchetypeGenerationRequest;
25  import org.apache.maven.archetype.ArchetypeGenerationResult;
26  import org.apache.maven.archetype.ArchetypeManager;
27  import org.apache.maven.archetype.catalog.Archetype;
28  import org.apache.maven.archetype.catalog.ArchetypeCatalog;
29  import org.codehaus.plexus.ContainerConfiguration;
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.eclipse.aether.DefaultRepositorySystemSession;
33  import org.eclipse.aether.RepositorySystem;
34  import org.eclipse.aether.repository.LocalRepository;
35  import org.eclipse.aether.repository.LocalRepositoryManager;
36  
37  /** @author Jason van Zyl */
38  public class ArchetypeGenerationTest extends PlexusTestCase {
39  
40      @Override
41      protected void customizeContainerConfiguration(ContainerConfiguration configuration) {
42          configuration.setClassPathScanning("index");
43      }
44  
45      public void testProjectGenerationFromAnArchetype() throws Exception {
46          ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
47  
48          DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
49          RepositorySystem repositorySystem = lookup(RepositorySystem.class);
50          LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager(
51                  repositorySession, new LocalRepository("target/test-classes/repositories/central"));
52          repositorySession.setLocalRepositoryManager(localRepositoryManager);
53  
54          ArchetypeCatalog catalog = archetype.getLocalCatalog(repositorySession);
55  
56          // Here I am just grabbing a OldArchetype but in a UI you would take the OldArchetype objects and present
57          // them to the user.
58  
59          Archetype selection =
60                  catalog.getArchetypes().get(catalog.getArchetypes().size() - 1);
61  
62          // Now you will present a dialog, or whatever, and grab the following values.
63  
64          String groupId = "com.mycompany";
65  
66          String artifactId = "app";
67  
68          String version = "1.0.0";
69  
70          String packageName = "org.mycompany.app";
71  
72          // With the selected OldArchetype and the parameters you can create a generation request as follows:
73          File outputDirectory = new File(getBasedir(), "target/test-classes/projects/archetyper-generate-1");
74          FileUtils.forceDelete(outputDirectory);
75  
76          ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest(selection)
77                  .setOutputDirectory(outputDirectory.getAbsolutePath())
78                  .setGroupId(groupId)
79                  .setArtifactId(artifactId)
80                  .setVersion(version)
81                  .setPackage(packageName)
82                  .setRepositorySession(repositorySession);
83  
84          Properties archetypeRequiredProperties = new Properties();
85          archetypeRequiredProperties.setProperty("property-with-default-1", "value-1");
86          archetypeRequiredProperties.setProperty("property-with-default-2", "value-2");
87          archetypeRequiredProperties.setProperty("property-with-default-3", "value-3");
88          archetypeRequiredProperties.setProperty("property-with-default-4", "value-4");
89          archetypeRequiredProperties.setProperty("property-without-default-1", "some-value-1");
90          archetypeRequiredProperties.setProperty("property-without-default-2", "some-value-2");
91          archetypeRequiredProperties.setProperty("property-without-default-3", "some-value-3");
92          archetypeRequiredProperties.setProperty("property-without-default-4", "some-value-4");
93          archetypeRequiredProperties.setProperty("property_underscored_1", "prop1");
94          archetypeRequiredProperties.setProperty("property_underscored-2", "prop2");
95          agr.setProperties(archetypeRequiredProperties);
96  
97          // Then generate away!
98  
99          ArchetypeGenerationResult result = archetype.generateProjectFromArchetype(agr);
100 
101         if (result.getCause() != null) {
102             result.getCause().printStackTrace(System.err);
103             fail(result.getCause().getMessage());
104         }
105     }
106 }