diff --git a/src/it/replace-file-content/pom.xml b/src/it/replace-file-content/pom.xml
index a8b1fad..ec9d186 100644
--- a/src/it/replace-file-content/pom.xml
+++ b/src/it/replace-file-content/pom.xml
@@ -99,6 +99,31 @@
+
+ inplace
+ validate
+
+ replace-file
+
+
+
+
+
+ **/file12.txt
+
+
+ target/**/*
+
+
+
+
+
+ is supposed
+ is likely
+
+
+
+
@@ -143,6 +168,11 @@
fb1f2b040970526fc99b1339f22465f1
md5
+
+ ${basedir}/folder1/file12.txt
+ a7f509275e16ff57cd7756cdca70822a
+ md5
+
diff --git a/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java b/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java
index 65ffcda..946dd6f 100644
--- a/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java
+++ b/src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java
@@ -14,10 +14,13 @@
*/
package com.inteligr8.maven.regex;
+import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.LinkedList;
import java.util.List;
@@ -130,10 +133,16 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo {
if (this.getLog().isDebugEnabled())
this.getLog().debug("replace all: " + file + " => " + tofile);
+ boolean overwrite = file.equals(tofile);
boolean didReplace = false;
Charset charset = Charset.forName(this.charsetName);
StringBuilder strbuilder = new StringBuilder();
+ if (overwrite) {
+ // if overwriting the existing file, use a temporary file
+ tofile = File.createTempFile("regexed-", ".tmp").toPath();
+ }
+
FileChannel targetChannel = FileChannel.open(tofile, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
try {
FileChannel sourceChannel = FileChannel.open(file, StandardOpenOption.READ);
@@ -177,6 +186,10 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo {
targetChannel.close();
}
+ if (overwrite) {
+ Files.move(tofile, file, StandardCopyOption.REPLACE_EXISTING);
+ }
+
return didReplace;
}
diff --git a/src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java b/src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java
index 5ff1f2b..8ba9700 100644
--- a/src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java
+++ b/src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java
@@ -74,6 +74,9 @@ public class ReplaceFileContentMojo extends AbstractReplaceMojo {
}
private Path resolveDirectory(Path basepath, String directory, boolean createIfMissing, String errorName) throws IOException, MojoExecutionException {
+ if (directory == null)
+ return this.project.getBasedir().toPath();
+
Path path = new File(directory).toPath();
if (!path.isAbsolute())
path = basepath.resolve(path);
@@ -93,19 +96,23 @@ public class ReplaceFileContentMojo extends AbstractReplaceMojo {
}
@Override
- protected void normalizeParameters() throws MojoFailureException {
- super.normalizeParameters();
+ protected void validateParamsPreNormalization() throws MojoFailureException {
+ super.validateParamsPreNormalization();
+
+ if (this.filesets == null || this.filesets.isEmpty())
+ throw new MojoFailureException("At least one 'fileset' is required");
}
@Override
- protected void validateParamsPostNormalization() throws MojoFailureException {
- super.validateParamsPostNormalization();
+ protected void normalizeParameters() throws MojoFailureException {
+ super.normalizeParameters();
- if (this.filesets == null || this.filesets.isEmpty())
- throw new MojoFailureException("At least one 'fileset' is required");
- for (FileSet fileset : this.filesets)
+ for (FileSet fileset : this.filesets) {
+ if (fileset.getDirectory() == null)
+ fileset.setDirectory(this.project.getBasedir().getAbsolutePath());
if (fileset.getOutputDirectory() == null)
- throw new MojoFailureException("All 'fileset' must have an 'outputDirectory'");
+ fileset.setOutputDirectory(this.project.getBasedir().getAbsolutePath());
+ }
}
}