View Javadoc

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  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         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         if ( project.getFile() != null && groupId == null )
186         {
187             groupId = project.getGroupId();
188         }
189 
190         if ( packageName == null )
191         {
192             getLog().info( "Defaulting package to group ID: " + groupId );
193 
194             packageName = groupId;
195         }
196 
197         // TODO: context mojo more appropriate?
198         Map parameters = new HashMap();
199 
200         parameters.put( "basedir", basedir );
201 
202         parameters.put( "package", packageName );
203 
204         parameters.put( "packageName", packageName );
205 
206         parameters.put( "groupId", groupId );
207 
208         parameters.put( "artifactId", artifactId );
209 
210         parameters.put( "version", version );
211 
212         List archetypeRemoteRepositories = new ArrayList( pomRemoteRepositories );
213 
214         if ( remoteRepositories != null )
215         {
216             getLog().info( "We are using command line specified remote repositories: " + remoteRepositories );
217 
218             archetypeRemoteRepositories = new ArrayList();
219 
220             String[] s = StringUtils.split( remoteRepositories, "," );
221 
222             for ( int i = 0; i < s.length; i++ )
223             {
224                 archetypeRemoteRepositories.add( createRepository( s[i], "id" + i ) );
225             }
226         }
227 
228         try
229         {
230             archetype.createArchetype( archetypeGroupId, archetypeArtifactId, archetypeVersion,
231                                        createRepository( "http://repo1.maven.org/maven2", "central" ), localRepository,
232                                        archetypeRemoteRepositories, parameters );
233         }
234         catch (UnknownArchetype e)
235         {
236             throw new MojoExecutionException( "Error creating from archetype", e );
237         }
238         catch ( ArchetypeNotFoundException e )
239         {
240             throw new MojoExecutionException( "Error creating from archetype", e );
241         }
242         catch ( ArchetypeDescriptorException e )
243         {
244             throw new MojoExecutionException( "Error creating from archetype", e );
245         }
246         catch ( ArchetypeTemplateProcessingException e )
247         {
248             throw new MojoExecutionException( "Error creating from archetype", e );
249         }
250     }
251 
252     //TODO: this should be put in John's artifact utils and used from there instead of being repeated here. Creating
253     // artifact repositories is someowhat cumbersome atm.
254     public ArtifactRepository createRepository( String url, String repositoryId )
255     {
256         // snapshots vs releases
257         // offline = to turning the update policy off
258 
259         //TODO: we'll need to allow finer grained creation of repositories but this will do for now
260 
261         String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
262 
263         String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
264 
265         ArtifactRepositoryPolicy snapshotsPolicy =
266             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
267 
268         ArtifactRepositoryPolicy releasesPolicy =
269             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
270 
271         return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout,
272                                                                    snapshotsPolicy, releasesPolicy );
273     }
274 }
275