1
2
3
4
5
6
7
8
9 package com.kohlschutter.mavenplugins.copyrename;
10
11 import java.io.File;
12 import java.io.IOException;
13 import java.util.List;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 import org.apache.maven.plugin.AbstractMojo;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.plugins.annotations.Component;
41 import org.apache.maven.plugins.annotations.LifecyclePhase;
42 import org.apache.maven.plugins.annotations.Mojo;
43 import org.apache.maven.plugins.annotations.Parameter;
44 import org.apache.maven.project.MavenProject;
45 import org.codehaus.plexus.util.FileUtils;
46 import org.sonatype.plexus.build.incremental.BuildContext;
47
48
49
50
51
52
53
54
55
56 @Mojo(name = "rename", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
57 public class RenameMojo extends AbstractMojo {
58
59
60
61
62
63 @Parameter(property = "copy.skip", defaultValue = "false")
64 boolean skip;
65
66
67
68
69
70
71 @Parameter(required = false)
72 private File sourceFile;
73
74
75
76
77
78 @Parameter(required = false)
79 private File destinationFile;
80
81
82
83
84
85
86
87 @Parameter(required = false)
88 private List<FileSet> fileSets;
89
90
91
92
93
94
95 @Parameter(property = "copy.overWrite", defaultValue = "true")
96 boolean overWrite;
97
98
99
100
101
102
103 @Parameter(property = "copy.ignoreExisting", defaultValue = "false")
104 boolean ignoreExisting;
105
106
107
108
109
110
111 @Parameter(property = "copy.ignoreMissing", defaultValue = "false")
112 boolean ignoreMissing;
113
114
115
116
117
118
119 @Parameter(property = "copy.ignoreFileNotFoundOnIncremental", defaultValue = "true")
120 boolean ignoreFileNotFoundOnIncremental;
121
122
123
124
125
126
127 @Parameter(defaultValue = "${project}", readonly = true)
128 private MavenProject project;
129
130 @Component
131 private BuildContext buildContext;
132
133
134
135
136 public RenameMojo() {
137 super();
138 }
139
140 @Override
141 public void execute() throws MojoExecutionException {
142 new RenameMojoExecutionContext(this).execute();
143 }
144
145 private final class RenameMojoExecutionContext extends MojoExecutionContext {
146 protected RenameMojoExecutionContext(RenameMojo mojo) {
147 super(mojo);
148 }
149
150 @Override
151 public void execute() throws MojoExecutionException {
152 if (skip) {
153 logDebug("Skipping the copy-rename-maven-plugin");
154 return;
155 }
156
157 logDebug("Executing the copy-rename-maven-plugin");
158 if (fileSets != null && !fileSets.isEmpty()) {
159 for (FileSet fileSet : fileSets) {
160 File srcFile = fileSet.getSourceFile();
161 File destFile = fileSet.getDestinationFile();
162 if (srcFile != null) {
163 rename(srcFile, destFile);
164 }
165 }
166 } else if (sourceFile != null) {
167 rename(sourceFile, destinationFile);
168 } else {
169 logInfo("No Files to process");
170 }
171 }
172
173 private void rename(File srcFile, File destFile) throws MojoExecutionException {
174 if (!srcFile.exists()) {
175 if (ignoreMissing) {
176 logInfo("Skipping rename of ", srcFile.getAbsolutePath(), " (missing)");
177 } else if (ignoreFileNotFoundOnIncremental && buildContext.isIncremental()) {
178 logWarn("sourceFile ", srcFile.getAbsolutePath(), " not found during incremental build");
179 } else {
180 logError("sourceFile ", srcFile.getAbsolutePath(), " does not exist");
181 }
182 } else if (destFile == null) {
183 logError("destinationFile not specified");
184 } else if (destFile.exists() && (destFile.isFile() == srcFile.isFile()) && !overWrite) {
185 if (ignoreExisting) {
186 logInfo("Skipping ", destFile.getAbsolutePath(), " (already exists)");
187 } else {
188 logError(destFile.getAbsolutePath(), " already exists and overWrite not set");
189 }
190 } else {
191 try {
192 FileUtils.rename(srcFile, destFile);
193 logInfo("Renamed ", srcFile.getAbsolutePath(), " to ", destFile.getAbsolutePath());
194 buildContext.refresh(destFile);
195 } catch (IOException e) {
196 throw new MojoExecutionException("could not rename " + srcFile.getAbsolutePath() + " to "
197 + destFile.getAbsolutePath(), e);
198 }
199 }
200 }
201 }
202 }