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 private File newFile;
37
38
39
40
41 private File oldFile;
42
43
44
45
46 @Override
47 protected boolean skipMojo() {
48
49 if (super.skipMojo()) {
50 return true;
51 }
52
53
54 boolean morphNeeded = isMorphNeeded();
55 if (morphNeeded) {
56 return false;
57 } else {
58 getLog().info("Skipping morph. Nothing has changed");
59 return true;
60 }
61 }
62
63 protected boolean isMorphNeeded() {
64
65 if (!getOldFile().exists()) {
66 getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
67 return false;
68 }
69
70
71 if (!getNewFile().exists()) {
72 return true;
73 }
74
75
76 long oldLastModified = getOldFile().lastModified();
77 long newLastModified = getNewFile().lastModified();
78
79
80
81 return oldLastModified > newLastModified;
82 }
83
84 protected abstract Morpher getMorpher(MorphRequest request, String artifactId);
85
86 @Override
87 protected void executeMojo() throws MojoExecutionException {
88 try {
89 getLog().info("From: " + oldFile.getAbsolutePath());
90 getLog().info(" To: " + newFile.getAbsolutePath());
91 FileUtils.forceMkdir(new File(FileUtils.getPath(newFile.getAbsolutePath())));
92 MorphRequest request = new MorphRequest(oldFile.getName(), new FileInputStream(oldFile),
93 new FileOutputStream(newFile));
94 request.setEncoding(getEncoding());
95 Morpher morpher = getMorpher(request, getProject().getArtifactId());
96 morpher.executeMorph();
97 } catch (IOException e) {
98 throw new MojoExecutionException("Unexpected error while attempting to morph " + oldFile.getAbsolutePath(),
99 e);
100 }
101 }
102
103 public File getNewFile() {
104 return newFile;
105 }
106
107 public void setNewFile(final File newFile) {
108 this.newFile = newFile;
109 }
110
111 public File getOldFile() {
112 return oldFile;
113 }
114
115 public void setOldFile(final File oldFile) {
116 this.oldFile = oldFile;
117 }
118 }