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 * <plugin> 44 * <groupId>org.codehaus.modello</groupId> 45 * <artifactId>modello-maven-plugin</artifactId> 46 * <version>1.3</version> 47 * <dependencies> 48 * <dependency> 49 * <groupId>org.codehaus.modello</groupId> 50 * <artifactId>modello-plugin-jpa</artifactId> 51 * <version>1.0.0-SNAPSHOT</version> 52 * </dependency> 53 * </dependencies> 54 * <configuration> 55 * <version>1.0.0</version> 56 * <packageWithVersion>false</packageWithVersion> 57 * <models> 58 * <model>src/main/mdo/project-model.xml</model> 59 * </models> 60 * </configuration> 61 * <executions> 62 * <execution> 63 * <id>java</id> 64 * <goals> 65 * <goal>generate</goal> 66 * </goals> 67 * <configuration> 68 * <generatorId>java</generatorId> 69 * </configuration> 70 * </execution> 71 * <execution> 72 * <id>jpa</id> 73 * <goals> 74 * <goal>generate</goal> 75 * </goals> 76 * <configuration> 77 * <generatorId>jpa-mapping</generatorId> 78 * </configuration> 79 * </execution> 80 * </executions> 81 * </plugin> 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 }