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.apache.tools.ant.taskdefs.Replace;
8   import org.apache.tools.ant.Project;
9   import org.codehaus.plexus.components.interactivity.Prompter;
10  import org.codehaus.plexus.components.interactivity.PrompterException;
11  import org.appfuse.tool.ArtifactUninstaller;
12  
13  import java.io.File;
14  
15  /**
16   * This mojo is used to "remove" installed artifacts installed by AMP.
17   *
18   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
19   * @goal remove
20   */
21  public class UninstallArtifactsMojo extends AbstractMojo {
22  
23      /**
24       * Prompter for user interaction.
25       *
26       * @component
27       * @readonly
28       * @noinspection UnusedDeclaration
29       */
30      private Prompter prompter;
31  
32      /**
33       * <i>Maven Internal</i>: Project to interact with.
34       *
35       * @parameter expression="${project}"
36       * @required
37       * @readonly
38       * @noinspection UnusedDeclaration
39       */
40      private MavenProject project;
41  
42      /**
43       * The path where the artifacts are installed.
44       *
45       * @parameter expression="${appfuse.installedDirectory}" default-value="${basedir}"
46       * @noinspection UnusedDeclaration
47       */
48      private String installedDirectory;
49  
50      /**
51       * @parameter expression="${appfuse.genericCore}" default-value="true"
52       * @noinspection UnusedDeclaration
53       */
54      private boolean genericCore;
55  
56      public void execute() throws MojoExecutionException, MojoFailureException {
57          // if project is of type "pom", throw an error
58          if (project.getPackaging().equalsIgnoreCase("pom")) {
59              String errorMsg = "[ERROR] This plugin cannot be run from a pom project, please run it from a jar or war project (i.e. core or web).";
60              //getLog().error(errorMsg);
61              throw new MojoFailureException(errorMsg);
62          }
63  
64          String pojoName = System.getProperty("entity");
65  
66          if (pojoName == null) {
67              try {
68                  pojoName = prompter.prompt("What is the name of your pojo (i.e. Person)?");
69              } catch (PrompterException pe) {
70                  pe.printStackTrace();
71              }
72          }
73  
74          if (!"true".equals(System.getProperty("skip.areyousure"))) {
75              try {
76                  String text = "WARNING: ALL artifacts will be removed, including your model object!";
77                         text += "\nAre you sure you want to remove '" + pojoName + ".java' and all related artifacts? [Y/N]";
78                  String proceed = prompter.prompt(text);
79                  if (!"Y".equalsIgnoreCase(proceed)) {
80                      log("Cancelling removal at your request.");
81                      return;
82                  } else {
83                      log("Proceeding... let's hope you're using source control!");
84                  }
85              } catch (PrompterException pe) {
86                  pe.printStackTrace();
87              }
88          }
89  
90          if (pojoName == null) {
91              throw new MojoExecutionException("You must specify an entity name to continue.");
92          }
93  
94          ArtifactUninstaller uninstaller = new ArtifactUninstaller(project, pojoName, installedDirectory, genericCore);
95          uninstaller.execute();
96  
97          String hibernateCfgLocation = installedDirectory + "/src/main/resources/hibernate.cfg.xml";
98          // remove entity from hibernate.cfg.xml
99          // this is to allow using hibernate.cfg.xml from core module
100         if (project.getPackaging().equals("war") && (project.hasParent()
101             && !project.getParentArtifact().getGroupId().contains("appfuse"))) {
102             // assume first module in parent project has hibernate.cfg.xml
103             String moduleName = (String) project.getParent().getModules().get(0);
104             String pathToParent = project.getOriginalModel().getParent().getRelativePath();
105             pathToParent = pathToParent.substring(0, pathToParent.lastIndexOf('/') + 1);
106             log("Assuming '" + moduleName + "' has hibernate.cfg.xml in its src/main/resources directory");
107             hibernateCfgLocation = project.getBasedir() + "/"
108                     + pathToParent + moduleName + "/src/main/resources/hibernate.cfg.xml";
109         }
110 
111         String daoFramework = (String) project.getProperties().get("dao.framework");
112 
113         if (daoFramework == null) {
114             getLog().error("[ERROR] No <dao.framework> property found in pom.xml. Please add this property.");
115         }
116 
117         if ("hibernate".equals(daoFramework)) {
118             log("Removing mapping for '" + pojoName + "' from hibernate.cfg.xml");
119             String className = project.getGroupId() + ".model." + pojoName;
120             Project antProject = AntUtils.createProject();
121             Replace replace = (Replace) antProject.createTask("replace");
122             replace.setFile(new File(hibernateCfgLocation));
123             replace.setToken("    <mapping class=\"" + className + "\"/>");
124             replace.execute();
125         }
126 
127         log("Removal succeeded! Please run 'mvn clean' to remove any compiled classes.");
128     }
129 
130     private void log(String msg) {
131         getLog().info("[AppFuse] " + msg);
132     }
133 }