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 expression="${localRepository}"
82       * @required
83       */
84      private ArtifactRepository localRepository;
85  
86      /**
87       * The Archetype Group Id to be used.
88       *
89       * @parameter expression="${archetypeGroupId}" default-value="org.apache.maven.archetypes"
90       * @required
91       */
92      private String archetypeGroupId;
93  
94      /**
95       * The Archetype Artifact Id to be used.
96       *
97       * @parameter expression="${archetypeArtifactId}" default-value="maven-archetype-quickstart"
98       * @required
99       */
100     private String archetypeArtifactId;
101 
102     /**
103      * The Archetype Version to be used.
104      *
105      * @parameter expression="${archetypeVersion}" default-value="RELEASE"
106      * @required
107      */
108     private String archetypeVersion;
109 
110     /**
111      * The Group Id of the project to be build.
112      *
113      * @parameter expression="${groupId}"
114      */
115     private String groupId;
116 
117     /**
118      * The Artifact Id of the project to be build.
119      *
120      * @parameter expression="${artifactId}"
121      */
122     private String artifactId;
123 
124     /**
125      * The Version of the project to be build.
126      *
127      * @parameter expression="${version}" default-value="1.0-SNAPSHOT"
128      * @required
129      */
130     private String version;
131 
132     /**
133      * The Package Name of the project to be build.
134      *
135      * @parameter expression="${packageName}" alias="package"
136      */
137     private String packageName;
138 
139     /**
140      * The remote repositories available for discovering dependencies and extensions as indicated
141      * by the POM.
142      *
143      * @parameter expression="${project.remoteArtifactRepositories}"
144      * @required
145      */
146     private List<ArtifactRepository> pomRemoteRepositories;
147 
148     /**
149      * Other remote repositories available for discovering dependencies and extensions.
150      *
151      * @parameter expression="${remoteRepositories}"
152      */
153     private String remoteRepositories;
154 
155     /**
156      * The project to be created an archetype of.
157      *
158      * @parameter expression="${project}"
159      */
160     private MavenProject project;
161 
162     /**
163      * @parameter expression="${basedir}" default-value="${user.dir}"
164      */
165     private String basedir;
166 
167     public void execute()
168         throws MojoExecutionException
169     {
170         getLog().warn( "This goal is deprecated. Please use mvn archetype:generate instead" );
171         // TODO: prompt for missing values
172         // TODO: configurable license
173 
174         // ----------------------------------------------------------------------
175         // archetypeGroupId
176         // archetypeArtifactId
177         // archetypeVersion
178         //
179         // localRepository
180         // remoteRepository
181         // parameters
182         // ----------------------------------------------------------------------
183 
184         if ( project.getFile() != null && groupId == null )
185         {
186             groupId = project.getGroupId();
187         }
188 
189         if ( packageName == null )
190         {
191             getLog().info( "Defaulting package to group ID: " + groupId );
192 
193             packageName = groupId;
194         }
195 
196 
197         List<ArtifactRepository> archetypeRemoteRepositories = new ArrayList<ArtifactRepository>( pomRemoteRepositories );
198 
199         if ( remoteRepositories != null )
200         {
201             getLog().info( "We are using command line specified remote repositories: " + remoteRepositories );
202 
203             archetypeRemoteRepositories = new ArrayList<ArtifactRepository>();
204 
205             String[] s = StringUtils.split( remoteRepositories, "," );
206 
207             for ( int i = 0; i < s.length; i++ )
208             {
209                 archetypeRemoteRepositories.add( createRepository( s[i], "id" + i ) );
210             }
211         }
212 
213         try
214         {
215             ArchetypeGenerationRequest request = new ArchetypeGenerationRequest()
216                 .setPackage( packageName )
217                 .setGroupId( groupId )
218                 .setArtifactId( artifactId )
219                 .setVersion( version )
220                 .setArchetypeGroupId( archetypeGroupId )
221                 .setArchetypeArtifactId( archetypeArtifactId )
222                 .setArchetypeVersion( archetypeVersion )
223                 .setLocalRepository( localRepository )
224                 .setRemoteArtifactRepositories( archetypeRemoteRepositories )
225                 .setOutputDirectory( basedir );
226 
227             archetype.createArchetype( request, createRepository( "http://repo1.maven.org/maven2", "central" ) );
228         }
229         catch ( UnknownArchetype e )
230         {
231             throw new MojoExecutionException( "Error creating from archetype", e );
232         }
233         catch ( ArchetypeNotFoundException e )
234         {
235             throw new MojoExecutionException( "Error creating from archetype", e );
236         }
237         catch ( ArchetypeDescriptorException e )
238         {
239             throw new MojoExecutionException( "Error creating from archetype", e );
240         }
241         catch ( ArchetypeTemplateProcessingException e )
242         {
243             throw new MojoExecutionException( "Error creating from archetype", e );
244         }
245     }
246 
247     //TODO: this should be put in John's artifact utils and used from there instead of being repeated here. Creating
248     // artifact repositories is somewhat cumbersome atm.
249     private ArtifactRepository createRepository( String url, String repositoryId )
250     {
251         // snapshots vs releases
252         // offline = to turning the update policy off
253 
254         //TODO: we'll need to allow finer grained creation of repositories but this will do for now
255 
256         String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;
257 
258         String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;
259 
260         ArtifactRepositoryPolicy snapshotsPolicy =
261             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
262 
263         ArtifactRepositoryPolicy releasesPolicy =
264             new ArtifactRepositoryPolicy( true, updatePolicyFlag, checksumPolicyFlag );
265 
266         return artifactRepositoryFactory.createArtifactRepository( repositoryId, url, defaultArtifactRepositoryLayout,
267                                                                    snapshotsPolicy, releasesPolicy );
268     }
269 }
270