View Javadoc
1   package org.codehaus.modello.maven;
2   
3   /*
4    * Copyright (c) 2007, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.util.Map;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.codehaus.modello.plugin.ModelloGenerator;
33  
34  /**
35   * <p>
36   * A dynamic way to use generators and Modello plugins.
37   * </p>
38   *
39   * <p>
40   * Example Usage:
41   * </p>
42   * <pre>
43   *   &lt;plugin&gt;
44   *     &lt;groupId&gt;org.codehaus.modello&lt;/groupId&gt;
45   *     &lt;artifactId&gt;modello-maven-plugin&lt;/artifactId&gt;
46   *     &lt;version&gt;1.3&lt;/version&gt;
47   *     &lt;dependencies&gt;
48   *       &lt;dependency&gt;
49   *         &lt;groupId&gt;org.codehaus.modello&lt;/groupId&gt;
50   *         &lt;artifactId&gt;modello-plugin-jpa&lt;/artifactId&gt;
51   *         &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
52   *       &lt;/dependency&gt;
53   *     &lt;/dependencies&gt;
54   *     &lt;configuration&gt;
55   *       &lt;version&gt;1.0.0&lt;/version&gt;
56   *       &lt;packageWithVersion&gt;false&lt;/packageWithVersion&gt;
57   *       &lt;models&gt;
58   *         &lt;model&gt;src/main/mdo/project-model.xml&lt;/model&gt;
59   *       &lt;/models&gt;
60   *     &lt;/configuration&gt;
61   *     &lt;executions&gt;
62   *       &lt;execution&gt;
63   *         &lt;id&gt;java&lt;/id&gt;
64   *         &lt;goals&gt;
65   *           &lt;goal&gt;generate&lt;/goal&gt;
66   *         &lt;/goals&gt;
67   *         &lt;configuration&gt;
68   *           &lt;generatorId&gt;java&lt;/generatorId&gt;
69   *         &lt;/configuration&gt;
70   *       &lt;/execution&gt;
71   *       &lt;execution&gt;
72   *         &lt;id&gt;jpa&lt;/id&gt;
73   *         &lt;goals&gt;
74   *           &lt;goal&gt;generate&lt;/goal&gt;
75   *         &lt;/goals&gt;
76   *         &lt;configuration&gt;
77   *           &lt;generatorId&gt;jpa-mapping&lt;/generatorId&gt;
78   *         &lt;/configuration&gt;
79   *       &lt;/execution&gt;
80   *     &lt;/executions&gt;
81   *   &lt;/plugin&gt;
82   * </pre>
83   *
84   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
85   */
86  @Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
87  public class ModelloGenerateMojo extends AbstractModelloSourceGeneratorMojo {
88      @Component(role = ModelloGenerator.class)
89      private Map<String, ModelloGenerator> generatorMap;
90  
91      @Parameter(property = "modello.generator.id", defaultValue = "java")
92      private String generatorId;
93  
94      protected String getGeneratorType() {
95          return generatorId;
96      }
97  
98      public void execute() throws MojoExecutionException {
99          if (!generatorMap.containsKey(generatorId)) {
100             throw new MojoExecutionException("Unable to execute modello, generator id [" + generatorId
101                     + "] not found.  (Available generator ids : " + generatorMap.keySet() + ")");
102         }
103 
104         getLog().info("[modello:generate {generator: " + generatorId + "}]");
105 
106         super.execute();
107     }
108 }