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),
101 new FileOutputStream(newFile));
102 request.setEncoding(getEncoding());
103 Morpher morpher = getMorpher(request, artifactId);
104 morpher.executeMorph();
105 } catch (IOException e) {
106 throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(),
107 e);
108 }
109 }
110
111 public File getNewFile() {
112 return newFile;
113 }
114
115 public void setNewFile(final File newFile) {
116 this.newFile = newFile;
117 }
118
119 public File getOldFile() {
120 return oldFile;
121 }
122
123 public void setOldFile(final File oldFile) {
124 this.oldFile = oldFile;
125 }
126
127 public String getArtifactId() {
128 return artifactId;
129 }
130
131 public void setArtifactId(String artifactId) {
132 this.artifactId = artifactId;
133 }
134 }