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