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