View Javadoc
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.old.OldArchetype;
23  import org.apache.maven.archetype.ArchetypeGenerationRequest;
24  import org.apache.maven.archetype.ArchetypeGenerationResult;
25  import org.apache.maven.archetype.common.ArchetypeArtifactManager;
26  import org.apache.maven.archetype.common.ArchetypeRegistryManager;
27  import org.apache.maven.archetype.exception.ArchetypeException;
28  import org.apache.maven.archetype.exception.ArchetypeGenerationFailure;
29  import org.apache.maven.archetype.exception.ArchetypeNotDefined;
30  import org.apache.maven.archetype.exception.UnknownArchetype;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.codehaus.plexus.component.annotations.Component;
33  import org.codehaus.plexus.component.annotations.Requirement;
34  import org.codehaus.plexus.logging.AbstractLogEnabled;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  import org.dom4j.DocumentException;
38  
39  import java.io.File;
40  import java.io.IOException;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  @Component( role = ArchetypeGenerator.class )
45  public class DefaultArchetypeGenerator
46      extends AbstractLogEnabled
47      implements ArchetypeGenerator
48  {
49      @Requirement
50      private ArchetypeRegistryManager archetypeRegistryManager;
51  
52      @Requirement
53      private ArchetypeArtifactManager archetypeArtifactManager;
54  
55      @Requirement
56      private FilesetArchetypeGenerator filesetGenerator;
57  
58      @Requirement
59      private OldArchetype oldArchetype;
60  
61      private File getArchetypeFile( ArchetypeGenerationRequest request, ArtifactRepository localRepository )
62          throws IOException, ArchetypeException, XmlPullParserException, DocumentException
63      {
64          if ( !isArchetypeDefined( request ) )
65          {
66              throw new ArchetypeNotDefined( "The archetype is not defined" );
67          }
68  
69          List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>();
70  
71          ArtifactRepository remoteRepo = null;
72          if ( request != null && request.getArchetypeRepository() != null )
73          {
74              remoteRepo =
75                  archetypeRegistryManager.createRepository( request.getArchetypeRepository(),
76                                                             request.getArchetypeArtifactId() + "-repo" );
77  
78              repos.add( remoteRepo );
79          }
80  
81          if ( !archetypeArtifactManager.exists( request.getArchetypeGroupId(), request.getArchetypeArtifactId(),
82                                                 request.getArchetypeVersion(), remoteRepo, localRepository, repos ) )
83          {
84              throw new UnknownArchetype( "The desired archetype does not exist (" + request.getArchetypeGroupId() + ":"
85                  + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() + ")" );
86          }
87  
88          File archetypeFile =
89              archetypeArtifactManager.getArchetypeFile( request.getArchetypeGroupId(), request.getArchetypeArtifactId(),
90                                                         request.getArchetypeVersion(), remoteRepo, localRepository,
91                                                         repos );
92          return archetypeFile;
93      }
94  
95      private void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile )
96          throws IOException, ArchetypeException, XmlPullParserException, DocumentException
97      {
98          if ( archetypeArtifactManager.isFileSetArchetype( archetypeFile ) )
99          {
100             processFileSetArchetype( request, archetypeFile );
101         }
102         else if ( archetypeArtifactManager.isOldArchetype( archetypeFile ) )
103         {
104             processOldArchetype( request, archetypeFile );
105         }
106         else
107         {
108             throw new ArchetypeGenerationFailure( "The defined artifact is not an archetype" );
109         }
110     }
111 
112     /** Common */
113     public String getPackageAsDirectory( String packageName )
114     {
115         return StringUtils.replace( packageName, ".", "/" );
116     }
117 
118     private boolean isArchetypeDefined( ArchetypeGenerationRequest request )
119     {
120         return StringUtils.isNotEmpty( request.getArchetypeGroupId() )
121             && StringUtils.isNotEmpty( request.getArchetypeArtifactId() )
122             && StringUtils.isNotEmpty( request.getArchetypeVersion() );
123     }
124 
125     /** FileSetArchetype */
126     private void processFileSetArchetype( ArchetypeGenerationRequest request, File archetypeFile )
127         throws ArchetypeException
128     {
129         filesetGenerator.generateArchetype( request, archetypeFile );
130     }
131 
132     private void processOldArchetype( ArchetypeGenerationRequest request, File archetypeFile )
133         throws UnknownArchetype, ArchetypeGenerationFailure
134     {
135         oldArchetype.createArchetype( request, archetypeFile );
136     }
137 
138     public void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile,
139                                    ArchetypeGenerationResult result )
140     {
141         try
142         {
143             generateArchetype( request, archetypeFile );
144         }
145         catch ( IOException ex )
146         {
147             result.setCause( ex );
148         }
149         catch ( ArchetypeException ex )
150         {
151             result.setCause( ex );
152         }
153         catch ( XmlPullParserException ex )
154         {
155             result.setCause( ex );
156         }
157         catch ( DocumentException ex )
158         {
159             result.setCause( ex );
160         }
161     }
162 
163     public void generateArchetype( ArchetypeGenerationRequest request, ArchetypeGenerationResult result )
164     {
165         try
166         {
167             File archetypeFile = getArchetypeFile( request, request.getLocalRepository() );
168 
169             generateArchetype( request, archetypeFile, result );
170         }
171         catch ( IOException ex )
172         {
173             result.setCause( ex );
174         }
175         catch ( ArchetypeException ex )
176         {
177             result.setCause( ex );
178         }
179         catch ( XmlPullParserException ex )
180         {
181             result.setCause( ex );
182         }
183         catch ( DocumentException ex )
184         {
185             result.setCause( ex );
186         }
187     }
188 }