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