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.appfuse.tool.SubversionUtils;
8 import org.tmatesoft.svn.core.SVNException;
9 import org.tmatesoft.svn.core.SVNErrorMessage;
10 import org.codehaus.plexus.util.FileUtils;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.List;
15
16
17
18
19
20
21
22 public class CopyTemplatesMojo extends AbstractMojo {
23
24
25
26
27
28
29
30
31
32 private MavenProject project;
33
34
35
36
37
38
39 private String trunk;
40
41
42
43
44
45
46 private String tag;
47
48
49
50
51
52 private String templateDirectory;
53
54 public void execute() throws MojoExecutionException, MojoFailureException {
55
56
57 String appfuseVersion = project.getProperties().getProperty("appfuse.version");
58
59 if ((appfuseVersion != null) && !appfuseVersion.endsWith("SNAPSHOT") && tag.equals("trunk/")) {
60 tag = "tags/APPFUSE_" + appfuseVersion.toUpperCase().replaceAll("-", "_") + "/";
61 }
62
63 String daoFramework = project.getProperties().getProperty("dao.framework");
64
65 if (daoFramework == null) {
66 log("No dao.framework property specified, defaulting to 'hibernate'");
67 }
68
69 String webFramework = project.getProperties().getProperty("web.framework");
70
71 boolean modular = (project.getPackaging().equals("pom") && !project.hasParent());
72
73 log("Installing templates in " + templateDirectory + "...");
74
75 if (!new File(templateDirectory).exists()) {
76 FileUtils.mkdir(templateDirectory);
77 }
78
79 if (project.getPackaging().equals("jar") || (project.getPackaging().equals("war") && !project.hasParent())) {
80
81
82 log("Installing model templates...");
83 if (!new File(templateDirectory + "/model").exists()) {
84 FileUtils.mkdir(templateDirectory + "/model");
85 }
86 export("plugins/appfuse-maven-plugin/src/main/resources/appfuse/model/",
87 ((modular) ? "core/" + templateDirectory : templateDirectory) + "/model");
88
89
90 log("Installing " + daoFramework + " templates...");
91 if (!new File(templateDirectory + "/dao").exists()) {
92 FileUtils.mkdir(templateDirectory + "/dao");
93 }
94
95 export("plugins/appfuse-maven-plugin/src/main/resources/appfuse/dao",
96 ((modular) ? "core/" + templateDirectory : templateDirectory) + "/dao");
97
98
99 try {
100 File daoDir = new File(templateDirectory + "/dao");
101 String[] dirs = daoDir.list();
102
103 for (String dir : dirs) {
104 if (new File(templateDirectory + "/dao/" + dir).isDirectory()) {
105 if (!dir.equals(daoFramework)) {
106 FileUtils.deleteDirectory(templateDirectory + "/dao/" + dir);
107 }
108 }
109 }
110 } catch (IOException io) {
111 throw new MojoFailureException(io.getMessage());
112 }
113
114
115 log("Installing service templates...");
116 if (!new File(templateDirectory + "/service").exists()) {
117 FileUtils.mkdir(templateDirectory + "/service");
118 }
119 export("plugins/appfuse-maven-plugin/src/main/resources/appfuse/service/",
120 ((modular) ? "core/" + templateDirectory : templateDirectory) + "/service");
121 }
122
123 if (project.getPackaging().equalsIgnoreCase("war")) {
124 if (webFramework == null) {
125 getLog().error("The web.framework property is not specified - please modify your pom.xml to add " +
126 " this property. For example: <web.framework>struts</web.framework>.");
127 throw new MojoExecutionException("No web.framework property specified, please modify pom.xml to add it.");
128 }
129
130
131 log("Installing " + webFramework + " templates...");
132 if (!new File(templateDirectory + "/web").exists()) {
133 FileUtils.mkdir(templateDirectory + "/web");
134 }
135
136 export("plugins/appfuse-maven-plugin/src/main/resources/appfuse/web",
137 ((modular) ? "web/" + templateDirectory : templateDirectory) + "/web");
138
139
140 try {
141 File webDir = new File(templateDirectory + "/web");
142 String[] dirs = webDir.list();
143
144 for (String dir : dirs) {
145 if (new File(templateDirectory + "/web/" + dir).isDirectory()) {
146 if (!dir.equals(webFramework)) {
147 FileUtils.deleteDirectory(templateDirectory + "/web/" + dir);
148 }
149 }
150 }
151 } catch (IOException io) {
152 throw new MojoFailureException(io.getMessage());
153 }
154
155 }
156 }
157
158
159 void setProject(MavenProject project) {
160 this.project = project;
161 }
162
163 private void export(String url, String destinationDirectory) throws MojoExecutionException {
164 SubversionUtils svn = new SubversionUtils(trunk + tag + url, destinationDirectory);
165
166 try {
167 svn.export();
168 } catch (SVNException e) {
169 SVNErrorMessage err = e.getErrorMessage();
170
171
172
173
174
175 while (err != null) {
176 getLog()
177 .error(err.getErrorCode().getCode() + " : " +
178 err.getMessage());
179 err = err.getChildErrorMessage();
180 }
181
182 throw new MojoExecutionException(e.getMessage());
183 }
184 }
185
186 private void log(String msg) {
187 getLog().info("[AppFuse] " + msg);
188 }
189 }