View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.mule.tools.maven.archetype;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.maven.archetype.Archetype;
24  import org.apache.maven.archetype.ArchetypeDescriptorException;
25  import org.apache.maven.archetype.ArchetypeNotFoundException;
26  import org.apache.maven.archetype.ArchetypeTemplateProcessingException;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  
31  /**
32   * Builds archetype containers.
33   *
34   * @goal create
35   * @description The archetype creation goal looks for an archetype with a given newGroupId, newArtifactId, and
36   * newVersion and retrieves it from the remote repository. Once the archetype is retrieve it is process against
37   * a set of user parameters to create a working Maven project. This is a modified newVersion for bobber to support additional functionality.
38   * @requiresProject false
39   */
40  public class ProjectArchetypeMojo extends AbstractMojo
41  {
42      /**
43       * @parameter expression="${component.org.apache.maven.archetype.Archetype}"
44       * @required
45       */
46      private Archetype archetype;
47  
48      /**
49       * @parameter expression="${localRepository}"
50       * @required
51       */
52      private ArtifactRepository localRepository;
53  
54      /**
55       * @parameter expression="${archetypeGroupId}" default-value="org.mule.tools"
56       * @required
57       */
58      private String archetypeGroupId;
59  
60      /**
61       * @parameter expression="${archetypeArtifactId}" default-value="mule-project-archetype"
62       * @required
63       */
64      private String archetypeArtifactId;
65  
66      /**
67       * @parameter expression="${archetypeVersion}" default-value="${muleVersion}"
68       * @required
69       */
70      private String archetypeVersion;
71  
72      /**
73       * @parameter expression="${muleVersion}"
74       * @required
75       */
76      private String muleVersion;
77  
78      /**
79       * @parameter expression="${groupId}" alias="newGroupId" default-value="com.mycompany.mule"
80       * @require
81       */
82      private String groupId;
83  
84      /**
85       * @parameter expression="${artifactId}" alias="newArtifactId" default-value="my-mule-project"
86       * @require
87       */
88      private String artifactId;
89  
90      /**
91       * @parameter expression="${version}" alias="newVersion" default-value="1.0-SNAPSHOT"
92       * @require
93       */
94      private String version;
95  
96      /** @parameter expression="${packageName}" alias="package" */
97      private String packageName;
98  
99      /**
100      * @parameter expression="${project.remoteArtifactRepositories}"
101      * @required
102      */
103     private List remoteRepositories;
104 
105     public void execute()
106             throws MojoExecutionException
107     {
108 
109         // ----------------------------------------------------------------------
110         // archetypeGroupId
111         // archetypeArtifactId
112         // archetypeVersion
113         //
114         // localRepository
115         // remoteRepository
116         // parameters
117         // ----------------------------------------------------------------------
118 
119         String basedir = System.getProperty("user.dir");
120 
121         if (packageName == null)
122         {
123             getLog().info("Defaulting package to group ID: " + groupId);
124 
125             packageName = groupId;
126         }
127 
128         // TODO: context mojo more appropriate?
129         Map map = new HashMap();
130 
131         map.put("basedir", basedir);
132 
133         map.put("package", packageName);
134 
135         map.put("packageName", packageName);
136 
137         map.put("groupId", groupId);
138 
139         map.put("artifactId", artifactId);
140 
141         map.put("version", version);
142         map.put("muleVersion", muleVersion);
143 
144         try
145         {
146             archetype.createArchetype(archetypeGroupId, archetypeArtifactId, archetypeVersion, localRepository, remoteRepositories, map);
147         }
148         catch (ArchetypeNotFoundException e)
149         {
150             throw new MojoExecutionException("Error creating from archetype", e);
151         }
152         catch (ArchetypeDescriptorException e)
153         {
154             throw new MojoExecutionException("Error creating from archetype", e);
155         }
156         catch (ArchetypeTemplateProcessingException e)
157         {
158             throw new MojoExecutionException("Error creating from archetype", e);
159         }
160     }
161 
162 
163 }