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/model/Regex.java b/src/main/java/com/inteligr8/maven/model/Regex.java
index 4356da2..f9cb442 100644
--- a/src/main/java/com/inteligr8/maven/model/Regex.java
+++ b/src/main/java/com/inteligr8/maven/model/Regex.java
@@ -14,8 +14,6 @@
*/
package com.inteligr8.maven.model;
-import org.apache.commons.lang3.StringUtils;
-
public class Regex implements Normalizable {
private String pattern;
@@ -41,8 +39,8 @@ public class Regex implements Normalizable {
@Override
public void normalize() {
- this.pattern = StringUtils.trimToNull(this.pattern);
- this.replacement = StringUtils.trimToNull(this.replacement);
+ if (this.replacement == null)
+ this.replacement = "";
}
}
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/MatchFileContentMojo.java b/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java
index 69bffa6..1b45d10 100644
--- a/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java
+++ b/src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java
@@ -89,10 +89,23 @@ public class MatchFileContentMojo extends AbstractMatchMojo {
return path;
}
+ @Override
+ 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 normalizeParameters() throws MojoFailureException {
super.normalizeParameters();
+ for (FileSet fileset : this.filesets) {
+ if (fileset.getDirectory() == null)
+ fileset.setDirectory(this.project.getBasedir().getAbsolutePath());
+ }
+
this.newProperty = StringUtils.trimToNull(this.newProperty);
}
@@ -100,8 +113,6 @@ public class MatchFileContentMojo extends AbstractMatchMojo {
protected void validateParamsPostNormalization() throws MojoFailureException {
super.validateParamsPostNormalization();
- if (this.filesets == null || this.filesets.isEmpty())
- throw new MojoFailureException("At least one 'fileset' is required");
if (this.newProperty == null)
throw new MojoFailureException("The 'newProperty' element is required");
}
diff --git a/src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java b/src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java
index 5ff1f2b..9487c85 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(fileset.getDirectory());
+ }
}
}