View Javadoc
1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tools.maven.archetype;
12  
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  
17  import org.apache.maven.archetype.Archetype;
18  import org.apache.maven.archetype.ArchetypeDescriptorException;
19  import org.apache.maven.archetype.ArchetypeNotFoundException;
20  import org.apache.maven.archetype.ArchetypeTemplateProcessingException;
21  import org.apache.maven.artifact.repository.ArtifactRepository;
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  
25  /**
26   * Creates a new configuration pattern.
27   * 
28   * @goal new-pattern
29   * @description The archetype creation goal looks for an archetype with a given newGroupId, newArtifactId, and
30   *              newVersion and retrieves it from the remote repository. Once the archetype is retrieve it is process
31   *              against a set of user parameters to create a working Maven project. This is a modified newVersion for
32   *              bobber to support additional functionality.
33   * @requiresProject false
34   */
35  public class ConfigurationPatternArchetypeMojo extends AbstractMojo
36  {
37      /**
38       * @parameter expression="${component.org.apache.maven.archetype.Archetype}"
39       * @required
40       */
41      private Archetype archetype;
42  
43      /**
44       * @parameter expression="${localRepository}"
45       * @required
46       */
47      private ArtifactRepository localRepository;
48  
49      /**
50       * @parameter expression="${archetypeGroupId}" default-value="org.mule.tools"
51       * @required
52       */
53      private String archetypeGroupId;
54  
55      /**
56       * @parameter expression="${archetypeArtifactId}" default-value="mule-catalog-archetype"
57       * @required
58       */
59      private String archetypeArtifactId;
60  
61      /**
62       * @parameter expression="${archetypeVersion}" default-value="${muleVersion}"
63       * @required
64       */
65      private String archetypeVersion;
66  
67      /**
68       * @parameter expression="${muleVersion}"
69       * @required
70       */
71      private String muleVersion;
72  
73      /**
74       * @parameter expression="${groupId}" alias="newGroupId"
75       * @require
76       */
77      private String groupId;
78  
79      /**
80       * @parameter expression="${artifactId}" alias="newArtifactId"
81       * @require
82       */
83      private String artifactId;
84  
85      /**
86       * @parameter expression="${version}" alias="newVersion"
87       * @require
88       */
89      private String version;
90  
91      /** @parameter expression="${packageName}" alias="package" */
92      private String packageName;
93  
94      /**
95       * @parameter expression="${project.remoteArtifactRepositories}"
96       * @required
97       */
98      private List<?> remoteRepositories;
99  
100     public void execute() throws MojoExecutionException
101     {
102         // ----------------------------------------------------------------------
103         // archetypeGroupId
104         // archetypeArtifactId
105         // archetypeVersion
106         //
107         // localRepository
108         // remoteRepository
109         // parameters
110         // ----------------------------------------------------------------------
111 
112         final String basedir = System.getProperty("user.dir");
113 
114         if (packageName == null)
115         {
116             getLog().info("Defaulting package to group ID: " + groupId);
117 
118             packageName = groupId;
119         }
120 
121         final Map<Object, Object> map = new HashMap<Object, Object>();
122         map.put("basedir", basedir);
123         map.put("package", packageName);
124         map.put("packageName", packageName);
125         map.put("groupId", groupId);
126         map.put("artifactId", artifactId);
127         map.put("version", version);
128         map.put("muleVersion", muleVersion);
129 
130         try
131         {
132             archetype.createArchetype(archetypeGroupId, archetypeArtifactId, archetypeVersion,
133                 localRepository, remoteRepositories, map);
134         }
135         catch (final ArchetypeNotFoundException e)
136         {
137             throw new MojoExecutionException("Error creating from archetype", e);
138         }
139         catch (final ArchetypeDescriptorException e)
140         {
141             throw new MojoExecutionException("Error creating from archetype", e);
142         }
143         catch (final ArchetypeTemplateProcessingException e)
144         {
145             throw new MojoExecutionException("Error creating from archetype", e);
146         }
147     }
148 
149 }