1 package org.apache.maven.plugin.archetype;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.maven.archetype.Archetype;
20 import org.apache.maven.archetype.ArchetypeDescriptorException;
21 import org.apache.maven.archetype.ArchetypeNotFoundException;
22 import org.apache.maven.archetype.ArchetypeTemplateProcessingException;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
25 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
26 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.project.MavenProject;
30 import org.codehaus.plexus.util.StringUtils;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37
38
39
40
41
42
43
44
45
46
47 public class MavenArchetypeMojo
48 extends AbstractMojo
49 {
50
51
52
53
54
55
56 private Archetype archetype;
57
58
59
60
61
62
63
64 private ArtifactRepositoryFactory artifactRepositoryFactory;
65
66
67
68
69
70
71 private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
72
73
74
75
76
77
78
79
80 private ArtifactRepository localRepository;
81
82
83
84
85
86
87
88 private String archetypeGroupId;
89
90
91
92
93
94
95
96 private String archetypeArtifactId;
97
98
99
100
101
102
103
104 private String archetypeVersion;
105
106
107
108
109
110
111 private String groupId;
112
113
114
115
116
117
118 private String artifactId;
119
120
121
122
123
124
125
126 private String version;
127
128
129
130
131
132
133 private String packageName;
134
135
136
137
138
139
140
141
142 private List pomRemoteRepositories;
143
144
145
146
147
148
149 private String remoteRepositories;
150
151
152
153
154
155
156 private MavenProject project;
157
158
159
160
161 private String basedir = System.getProperty( "user.dir" );
162
163 public void execute()
164 throws MojoExecutionException
165 {
166
167
168
169
170
171
172
173
174
175
176
177
178
179 if ( project.getFile() != null && groupId == null )
180 {
181 groupId = project.getGroupId();
182 }
183
184 if ( packageName == null )
185 {
186 getLog().info( "Defaulting package to group ID: " + groupId );
187
188 packageName = groupId;
189 }
190
191
192 Map map = new HashMap();
193
194 map.put( "basedir", basedir );
195
196 map.put( "package", packageName );
197
198 map.put( "packageName", packageName );
199
200 map.put( "groupId", groupId );
201
202 map.put( "artifactId", artifactId );
203
204 map.put( "version", version );
205
206 List archetypeRemoteRepositories = new ArrayList( pomRemoteRepositories );
207
208 if ( remoteRepositories != null )
209 {
210 getLog().info( "We are using command line specified remote repositories: " + remoteRepositories );
211
212 archetypeRemoteRepositories = new ArrayList();
213
214 String[] s = StringUtils.split( remoteRepositories, "," );
215
216 for ( int i = 0; i < s.length; i++ )
217 {
218 archetypeRemoteRepositories.add( createRepository( s[i], "id" + i ) );
219 }
220 }
221
222 try
223 {
224 archetype.createArchetype( archetypeGroupId, archetypeArtifactId, archetypeVersion, localRepository,
225 archetypeRemoteRepositories, map );
226 }
227 catch ( ArchetypeNotFoundException e )
228 {
229 throw new MojoExecutionException( "Error creating from archetype", e );
230 }
231 catch ( ArchetypeDescriptorException e )
232 {
233 throw new MojoExecutionException( "Error creating from archetype", e );
234 }
235 catch ( ArchetypeTemplateProcessingException e )
236 {
237 throw new MojoExecutionException( "Error creating from archetype", e );
238 }
239 }
240
241
242
243 public ArtifactRepository createRepository( String url, String repositoryId )
244 {
245
246
247
248
249
250 String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
251
252 String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
253
254 ArtifactRepositoryPolicy snapshotsPolicy =
255 new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
256
257 ArtifactRepositoryPolicy releasesPolicy =
258 new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
259
260 return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout,
261 snapshotsPolicy, releasesPolicy );
262 }
263 }
264