View Javadoc

1   package org.apache.maven.plugin.archetype;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * The archetype creation goal looks for an archetype with a given groupId, 
39   * artifactId, and version and retrieves it from the remote repository. Once the
40   * archetype is retrieved, it is then processed against a set of user parameters
41   * to create a working Maven project.
42   *
43   * @description Creates archetype containers.
44   * @requiresProject false
45   * @goal create
46   */
47  public class MavenArchetypeMojo
48      extends AbstractMojo
49  {
50      /**
51       * Used to create the Archetype specified by the groupId, artifactId, and 
52       * version from the remote repository.
53       * 
54       * @component
55       */
56      private Archetype archetype;
57  
58      /**
59       * Used to create ArtifactRepository objects given the urls of the remote
60       * repositories.
61       * 
62       * @component
63       */
64      private ArtifactRepositoryFactory artifactRepositoryFactory;
65  
66      /**
67       * Determines whether the layout is legacy or not.
68       * 
69       * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout" roleHint="default"
70       */
71      private ArtifactRepositoryLayout defaultArtifactRepositoryLayout;
72  
73  
74      /**
75       * Maven's local repository.
76       * 
77       * @parameter expression="${localRepository}"
78       * @required
79       */
80      private ArtifactRepository localRepository;
81  
82      /**
83       * The Archetype Group Id to be used.
84       * 
85       * @parameter expression="${archetypeGroupId}" default-value="org.apache.maven.archetypes"
86       * @required
87       */
88      private String archetypeGroupId;
89  
90      /**
91       * The Archetype Artifact Id to be used.
92       * 
93       * @parameter expression="${archetypeArtifactId}" default-value="maven-archetype-quickstart"
94       * @required
95       */
96      private String archetypeArtifactId;
97  
98      /**
99       * The Archetype Version to be used.
100      * 
101      * @parameter expression="${archetypeVersion}" default-value="RELEASE"
102      * @required
103      */
104     private String archetypeVersion;
105 
106     /**
107      * The Group Id of the project to be build.
108      * 
109      * @parameter expression="${groupId}"
110      */
111     private String groupId;
112 
113     /**
114      * The Artifact Id of the project to be build.
115      * 
116      * @parameter expression="${artifactId}"
117      */
118     private String artifactId;
119 
120     /**
121      * The Version of the project to be build.
122      * 
123      * @parameter expression="${version}" default-value="1.0-SNAPSHOT"
124      * @required
125      */
126     private String version;
127 
128     /**
129      * The Package Name of the project to be build.
130      * 
131      * @parameter expression="${packageName}" alias="package"
132      */
133     private String packageName;
134 
135     /**
136      * The remote repositories available for discovering dependencies and extensions as indicated
137      * by the POM.
138      * 
139      * @parameter expression="${project.remoteArtifactRepositories}"
140      * @required
141      */
142     private List pomRemoteRepositories;
143 
144     /**
145      * Other remote repositories available for discovering dependencies and extensions.
146      * 
147      * @parameter expression="${remoteRepositories}"
148      */
149     private String remoteRepositories;
150 
151     /**
152      * The project to be created an archetype of.
153      * 
154      * @parameter expression="${project}"
155      */
156     private MavenProject project;
157 
158     /**
159      * @parameter expression="${basedir}" default-value="${user.dir}"
160      */
161     private String basedir;
162 
163     public void execute()
164         throws MojoExecutionException
165     {
166         // TODO: prompt for missing values
167         // TODO: configurable license
168 
169         // ----------------------------------------------------------------------
170         // archetypeGroupId
171         // archetypeArtifactId
172         // archetypeVersion
173         //
174         // localRepository
175         // remoteRepository
176         // parameters
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         // TODO: context mojo more appropriate?
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     //TODO: this should be put in John's artifact utils and used from there instead of being repeated here. Creating
242     // artifact repositories is someowhat cumbersome atm.
243     public ArtifactRepository createRepository( String url, String repositoryId )
244     {
245         // snapshots vs releases
246         // offline = to turning the update policy off
247 
248         //TODO: we'll need to allow finer grained creation of repositories but this will do for now
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