Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MavenArchetypeMojo |
|
| 7.0;7 |
1 | package org.apache.maven.archetype.mojos; | |
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.exception.UnknownArchetype; | |
23 | import org.apache.maven.archetype.old.OldArchetype; | |
24 | import org.apache.maven.archetype.old.ArchetypeDescriptorException; | |
25 | import org.apache.maven.archetype.old.ArchetypeNotFoundException; | |
26 | import org.apache.maven.archetype.old.ArchetypeTemplateProcessingException; | |
27 | import org.apache.maven.artifact.repository.ArtifactRepository; | |
28 | import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; | |
29 | import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; | |
30 | import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; | |
31 | import org.apache.maven.plugin.AbstractMojo; | |
32 | import org.apache.maven.plugin.MojoExecutionException; | |
33 | import org.apache.maven.project.MavenProject; | |
34 | import org.codehaus.plexus.util.StringUtils; | |
35 | ||
36 | import java.util.ArrayList; | |
37 | import java.util.HashMap; | |
38 | import java.util.List; | |
39 | import java.util.Map; | |
40 | ||
41 | /** | |
42 | * The archetype creation goal looks for an archetype with a given groupId, | |
43 | * artifactId, and version and retrieves it from the remote repository. Once the | |
44 | * archetype is retrieved, it is then processed against a set of user parameters | |
45 | * to create a working Maven project. | |
46 | * | |
47 | * @description Creates archetype containers. | |
48 | * @requiresProject false | |
49 | * @goal create | |
50 | * @deprecated Please use the generate mojo instead. | |
51 | */ | |
52 | 0 | public class MavenArchetypeMojo |
53 | extends AbstractMojo | |
54 | { | |
55 | /** | |
56 | * Used to create the Archetype specified by the groupId, artifactId, and | |
57 | * version from the remote repository. | |
58 | * | |
59 | * @component | |
60 | */ | |
61 | private OldArchetype archetype; | |
62 | ||
63 | /** | |
64 | * Used to create ArtifactRepository objects given the urls of the remote | |
65 | * repositories. | |
66 | * | |
67 | * @component | |
68 | */ | |
69 | private ArtifactRepositoryFactory artifactRepositoryFactory; | |
70 | ||
71 | /** | |
72 | * Determines whether the layout is legacy or not. | |
73 | * | |
74 | * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout" roleHint="default" | |
75 | */ | |
76 | private ArtifactRepositoryLayout defaultArtifactRepositoryLayout; | |
77 | ||
78 | ||
79 | /** | |
80 | * Maven's local repository. | |
81 | * | |
82 | * @parameter expression="${localRepository}" | |
83 | * @required | |
84 | */ | |
85 | private ArtifactRepository localRepository; | |
86 | ||
87 | /** | |
88 | * The Archetype Group Id to be used. | |
89 | * | |
90 | * @parameter expression="${archetypeGroupId}" default-value="org.apache.maven.archetypes" | |
91 | * @required | |
92 | */ | |
93 | private String archetypeGroupId; | |
94 | ||
95 | /** | |
96 | * The Archetype Artifact Id to be used. | |
97 | * | |
98 | * @parameter expression="${archetypeArtifactId}" default-value="maven-archetype-quickstart" | |
99 | * @required | |
100 | */ | |
101 | private String archetypeArtifactId; | |
102 | ||
103 | /** | |
104 | * The Archetype Version to be used. | |
105 | * | |
106 | * @parameter expression="${archetypeVersion}" default-value="RELEASE" | |
107 | * @required | |
108 | */ | |
109 | private String archetypeVersion; | |
110 | ||
111 | /** | |
112 | * The Group Id of the project to be build. | |
113 | * | |
114 | * @parameter expression="${groupId}" | |
115 | */ | |
116 | private String groupId; | |
117 | ||
118 | /** | |
119 | * The Artifact Id of the project to be build. | |
120 | * | |
121 | * @parameter expression="${artifactId}" | |
122 | */ | |
123 | private String artifactId; | |
124 | ||
125 | /** | |
126 | * The Version of the project to be build. | |
127 | * | |
128 | * @parameter expression="${version}" default-value="1.0-SNAPSHOT" | |
129 | * @required | |
130 | */ | |
131 | private String version; | |
132 | ||
133 | /** | |
134 | * The Package Name of the project to be build. | |
135 | * | |
136 | * @parameter expression="${packageName}" alias="package" | |
137 | */ | |
138 | private String packageName; | |
139 | ||
140 | /** | |
141 | * The remote repositories available for discovering dependencies and extensions as indicated | |
142 | * by the POM. | |
143 | * | |
144 | * @parameter expression="${project.remoteArtifactRepositories}" | |
145 | * @required | |
146 | */ | |
147 | private List pomRemoteRepositories; | |
148 | ||
149 | /** | |
150 | * Other remote repositories available for discovering dependencies and extensions. | |
151 | * | |
152 | * @parameter expression="${remoteRepositories}" | |
153 | */ | |
154 | private String remoteRepositories; | |
155 | ||
156 | /** | |
157 | * The project to be created an archetype of. | |
158 | * | |
159 | * @parameter expression="${project}" | |
160 | */ | |
161 | private MavenProject project; | |
162 | ||
163 | /** | |
164 | * @parameter expression="${basedir}" default-value="${user.dir}" | |
165 | */ | |
166 | private String basedir; | |
167 | ||
168 | public void execute() | |
169 | throws MojoExecutionException | |
170 | { | |
171 | 0 | getLog().warn( "This goal is deprecated. Please use mvn archetype:generate instead" ); |
172 | // TODO: prompt for missing values | |
173 | // TODO: configurable license | |
174 | ||
175 | // ---------------------------------------------------------------------- | |
176 | // archetypeGroupId | |
177 | // archetypeArtifactId | |
178 | // archetypeVersion | |
179 | // | |
180 | // localRepository | |
181 | // remoteRepository | |
182 | // parameters | |
183 | // ---------------------------------------------------------------------- | |
184 | ||
185 | 0 | if ( project.getFile() != null && groupId == null ) |
186 | { | |
187 | 0 | groupId = project.getGroupId(); |
188 | } | |
189 | ||
190 | 0 | if ( packageName == null ) |
191 | { | |
192 | 0 | getLog().info( "Defaulting package to group ID: " + groupId ); |
193 | ||
194 | 0 | packageName = groupId; |
195 | } | |
196 | ||
197 | // TODO: context mojo more appropriate? | |
198 | 0 | Map map = new HashMap(); |
199 | ||
200 | 0 | map.put( "basedir", basedir ); |
201 | ||
202 | 0 | map.put( "package", packageName ); |
203 | ||
204 | 0 | map.put( "packageName", packageName ); |
205 | ||
206 | 0 | map.put( "groupId", groupId ); |
207 | ||
208 | 0 | map.put( "artifactId", artifactId ); |
209 | ||
210 | 0 | map.put( "version", version ); |
211 | ||
212 | 0 | List archetypeRemoteRepositories = new ArrayList( pomRemoteRepositories ); |
213 | ||
214 | 0 | if ( remoteRepositories != null ) |
215 | { | |
216 | 0 | getLog().info( "We are using command line specified remote repositories: " + remoteRepositories ); |
217 | ||
218 | 0 | archetypeRemoteRepositories = new ArrayList(); |
219 | ||
220 | 0 | String[] s = StringUtils.split( remoteRepositories, "," ); |
221 | ||
222 | 0 | for ( int i = 0; i < s.length; i++ ) |
223 | { | |
224 | 0 | archetypeRemoteRepositories.add( createRepository( s[i], "id" + i ) ); |
225 | } | |
226 | } | |
227 | ||
228 | try | |
229 | { | |
230 | 0 | archetype.createArchetype( |
231 | archetypeGroupId, | |
232 | archetypeArtifactId, | |
233 | archetypeVersion, | |
234 | createRepository("http://repo1.maven.org/maven2", "central"), | |
235 | localRepository, | |
236 | archetypeRemoteRepositories, | |
237 | map); | |
238 | } | |
239 | 0 | catch (UnknownArchetype e) |
240 | { | |
241 | 0 | throw new MojoExecutionException( "Error creating from archetype", e ); |
242 | } | |
243 | 0 | catch ( ArchetypeNotFoundException e ) |
244 | { | |
245 | 0 | throw new MojoExecutionException( "Error creating from archetype", e ); |
246 | } | |
247 | 0 | catch ( ArchetypeDescriptorException e ) |
248 | { | |
249 | 0 | throw new MojoExecutionException( "Error creating from archetype", e ); |
250 | } | |
251 | 0 | catch ( ArchetypeTemplateProcessingException e ) |
252 | { | |
253 | 0 | throw new MojoExecutionException( "Error creating from archetype", e ); |
254 | 0 | } |
255 | 0 | } |
256 | ||
257 | //TODO: this should be put in John's artifact utils and used from there instead of being repeated here. Creating | |
258 | // artifact repositories is someowhat cumbersome atm. | |
259 | public ArtifactRepository createRepository( String url, String repositoryId ) | |
260 | { | |
261 | // snapshots vs releases | |
262 | // offline = to turning the update policy off | |
263 | ||
264 | //TODO: we'll need to allow finer grained creation of repositories but this will do for now | |
265 | ||
266 | 0 | String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS; |
267 | ||
268 | 0 | String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN; |
269 | ||
270 | 0 | ArtifactRepositoryPolicy snapshotsPolicy = |
271 | new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag ); | |
272 | ||
273 | 0 | ArtifactRepositoryPolicy releasesPolicy = |
274 | new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag ); | |
275 | ||
276 | 0 | return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout, |
277 | snapshotsPolicy, releasesPolicy ); | |
278 | } | |
279 | } | |
280 |