View Javadoc

1   package org.appfuse.mojo.installer;
2   
3   import org.apache.maven.plugin.AbstractMojo;
4   import org.apache.maven.plugin.MojoExecutionException;
5   import org.apache.maven.plugin.MojoFailureException;
6   import org.apache.maven.project.MavenProject;
7   import org.codehaus.plexus.components.interactivity.Prompter;
8   import org.codehaus.plexus.components.interactivity.PrompterException;
9   import org.appfuse.tool.ArtifactInstaller;
10  
11  /**
12   * This mojo is used to "install" generated artifacts (Java files, XML files) into an AppFuse project.
13   *
14   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
15   * @goal install
16   * @phase generate-sources
17   * @execute phase="compile"
18   */
19  public class InstallArtifactsMojo extends AbstractMojo {
20  
21      /**
22       * This is a prompter that can be user within the maven framework.
23       *
24       * @component
25       */
26      Prompter prompter;
27  
28      /**
29       * <i>Maven Internal</i>: Project to interact with.
30       *
31       * @parameter expression="${project}"
32       * @required
33       * @readonly
34       * @noinspection UnusedDeclaration
35       */
36      private MavenProject project;
37  
38      /**
39       * The path where the generated artifacts will be placed. This is intentionally not set to the
40       * default location for maven generated sources. This is to keep these files out of the
41       * eclipse/idea generated sources directory as the intention is that these files will be copied
42       * to a source directory to be edited and modified and not re generated each time the plugin is
43       * run. If you want to regenerate the files each time you build the project just set this value
44       * to ${basedir}/target/generated-sources or set the flag on eclipse/idea plugin to include this
45       * file in your project file as a source directory.
46       *
47       * @parameter expression="${appfuse.destinationDirectory}" default-value="${basedir}"
48       */
49      private String destinationDirectory;
50  
51      /**
52       * The directory containing the source code.
53       *
54       * @parameter expression="${appfuse.sourceDirectory}" default-value="${basedir}/target/appfuse/generated-sources"
55       */
56      private String sourceDirectory;
57  
58      /**
59       * @parameter expression="${appfuse.genericCore}" default-value="true"
60       */
61      private boolean genericCore;
62  
63      public void execute() throws MojoExecutionException, MojoFailureException {
64          // if project is of type "pom", throw an error
65          if (project.getPackaging().equalsIgnoreCase("pom")) {
66              String errorMsg = "Doh! This plugin cannot be run from a pom project, please run it from a jar or war project (i.e. core or web).";
67              //getLog().error(errorMsg);
68              throw new MojoFailureException(errorMsg);
69          }
70  
71          String pojoName = System.getProperty("entity");
72  
73          if (pojoName == null) {
74              try {
75                  pojoName = prompter.prompt("What is the name of your pojo (i.e. Person)?");
76              } catch (PrompterException pe) {
77                  pe.printStackTrace();
78              }
79          }
80  
81          if (pojoName == null) {
82              throw new MojoExecutionException("You must specify an entity name to continue.");
83          }
84  
85          ArtifactInstaller installer = new ArtifactInstaller(project, pojoName, sourceDirectory, destinationDirectory, genericCore);
86          installer.execute();
87      }
88  }