1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.torque.mojo;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.torque.mojo.morph.MorphRequest;
25 import org.apache.torque.mojo.morph.Morpher;
26 import org.codehaus.plexus.util.FileUtils;
27
28
29
30
31 public abstract class AbstractMorphSingleMojo extends BaseMojo {
32
33
34
35
36
37
38
39 private String artifactId;
40
41
42
43
44 private File newFile;
45
46
47
48
49 private File oldFile;
50
51
52
53
54 @Override
55 protected boolean skipMojo() {
56
57 if (super.skipMojo()) {
58 return true;
59 }
60
61
62 boolean morphNeeded = isMorphNeeded();
63 if (morphNeeded) {
64 return false;
65 } else {
66 getLog().info("Skipping morph. Nothing has changed");
67 return true;
68 }
69 }
70
71 protected boolean isMorphNeeded() {
72
73 if (!getOldFile().exists()) {
74 getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
75 return false;
76 }
77
78
79 if (!getNewFile().exists()) {
80 return true;
81 }
82
83
84 long oldLastModified = getOldFile().lastModified();
85 long newLastModified = getNewFile().lastModified();
86
87
88
89 return oldLastModified > newLastModified;
90 }
91
92 protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
93
94 @Override
95 protected void executeMojo() throws MojoExecutionException {
96 try {
97 getLog().info("From: " + oldFile.getAbsolutePath());
98 getLog().info(" To: " + newFile.getAbsolutePath());
99 FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
100 MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile), new FileOutputStream(newFile));
101 request.setEncoding(getEncoding());
102 Morpher morpher = getMorpher(request, artifactId);
103 morpher.executeMorph();
104 } catch (IOException e) {
105 throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(), e);
106 }
107 }
108
109 public File getNewFile() {
110 return newFile;
111 }
112
113 public void setNewFile(final File newFile) {
114 this.newFile = newFile;
115 }
116
117 public File getOldFile() {
118 return oldFile;
119 }
120
121 public void setOldFile(final File oldFile) {
122 this.oldFile = oldFile;
123 }
124
125 public String getArtifactId() {
126 return artifactId;
127 }
128
129 public void setArtifactId(String artifactId) {
130 this.artifactId = artifactId;
131 }
132 }