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
17
18
19
20
21 public class UninstallArtifactsMojo extends AbstractMojo {
22
23
24
25
26
27
28
29
30 private Prompter prompter;
31
32
33
34
35
36
37
38
39
40 private MavenProject project;
41
42
43
44
45
46
47
48 private String installedDirectory;
49
50
51
52
53
54 private boolean genericCore;
55
56 public void execute() throws MojoExecutionException, MojoFailureException {
57
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
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
99
100 if (project.getPackaging().equals("war") && project.hasParent()) {
101
102 String moduleName = (String) project.getParent().getModules().get(0);
103 String pathToParent = project.getOriginalModel().getParent().getRelativePath();
104 pathToParent = pathToParent.substring(0, pathToParent.lastIndexOf('/') + 1);
105 log("Assuming '" + moduleName + "' has hibernate.cfg.xml in its src/main/resources directory");
106 hibernateCfgLocation = project.getBasedir() + "/"
107 + pathToParent + moduleName + "/src/main/resources/hibernate.cfg.xml";
108 }
109
110 String daoFramework = (String) project.getProperties().get("dao.framework");
111
112 if (daoFramework == null) {
113 getLog().error("[ERROR] No <dao.framework> property found in pom.xml. Please add this property.");
114 }
115
116 if ("hibernate".equals(daoFramework)) {
117 log("Removing mapping for '" + pojoName + "' from hibernate.cfg.xml");
118 String className = project.getGroupId() + ".model." + pojoName;
119 Project antProject = AntUtils.createProject();
120 Replace replace = (Replace) antProject.createTask("replace");
121 replace.setFile(new File(hibernateCfgLocation));
122 replace.setToken(" <mapping class=\"" + className + "\"/>");
123 replace.execute();
124 }
125
126 log("Removal succeeded! Please run 'mvn clean' to remove any compiled classes.");
127 }
128
129 private void log(String msg) {
130 getLog().info("[AppFuse] " + msg);
131 }
132 }