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.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   * This mojo is used to copy FreeMarker templates from AMP into an AppFuse project.
18   *
19   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
20   * @goal copy-templates
21   */
22  public class CopyTemplatesMojo extends AbstractMojo {
23  
24      /**
25       * <i>Maven Internal</i>: Project to interact with.
26       *
27       * @parameter expression="${project}"
28       * @required
29       * @readonly
30       * @noinspection UnusedDeclaration
31       */
32      private MavenProject project;
33  
34      /**
35       * The directory containing the source code.
36       *
37       * @parameter expression="${appfuse.trunk}" default-value="https://svn.java.net/svn/appfuse~svn/"
38       */
39      private String trunk;
40  
41      /**
42       * The tag containing the source code - defaults to '/trunk', but you may want to set it to '/tags/TAGNAME'
43       *
44       * @parameter expression="${appfuse.tag}" default-value="trunk/"
45       */
46      private String tag;
47  
48      /**
49       * @parameter expression="${appfuse.templateDirectory}" default-value="src/test/resources/appfuse"
50       * @noinspection UnusedDeclaration
51       */
52      private String templateDirectory;
53  
54      public void execute() throws MojoExecutionException, MojoFailureException {
55          // TODO: Copy files from JAR instead of SVN
56          // If appfuse.version is specified as a property, and not a SNAPSHOT, use it for the tag
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              // export model templates
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              // export dao templates
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              // delete templates that aren't for current dao.framework
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             // export manager templates
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             // export web templates
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             // delete templates that aren't for current web.framework
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     // Allow setting project from tests (AbstractAppFuseMojoTestCase)
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              * Display all tree of error messages.
173              * Utility method SVNErrorMessage.getFullMessage() may be used instead of the loop.
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 }