Loading src/it/replace-file-content/pom.xml +30 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,31 @@ </regexes> </configuration> </execution> <execution> <id>inplace</id> <phase>validate</phase> <goals> <goal>replace-file</goal> </goals> <configuration> <filesets> <fileset> <includes> <include>**/file12.txt</include> </includes> <excludes> <exclude>target/**/*</exclude> </excludes> </fileset> </filesets> <regexes> <regex> <pattern>is supposed</pattern> <replacement>is likely</replacement> </regex> </regexes> </configuration> </execution> </executions> </plugin> <plugin> Loading Loading @@ -143,6 +168,11 @@ <checksum>fb1f2b040970526fc99b1339f22465f1</checksum> <type>md5</type> </requireFileChecksum> <requireFileChecksum> <file>${basedir}/folder1/file12.txt</file> <checksum>a7f509275e16ff57cd7756cdca70822a</checksum> <type>md5</type> </requireFileChecksum> </rules> </configuration> </execution> Loading src/main/java/com/inteligr8/maven/model/Regex.java +2 −4 Original line number Diff line number Diff line Loading @@ -14,8 +14,6 @@ */ package com.inteligr8.maven.model; import org.apache.commons.lang3.StringUtils; public class Regex implements Normalizable { private String pattern; Loading @@ -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 = ""; } } src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java +13 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -177,6 +186,10 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo { targetChannel.close(); } if (overwrite) { Files.move(tofile, file, StandardCopyOption.REPLACE_EXISTING); } return didReplace; } Loading src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java +13 −2 Original line number Diff line number Diff line Loading @@ -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); } Loading @@ -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"); } Loading src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java +15 −8 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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()); } } } Loading
src/it/replace-file-content/pom.xml +30 −0 Original line number Diff line number Diff line Loading @@ -99,6 +99,31 @@ </regexes> </configuration> </execution> <execution> <id>inplace</id> <phase>validate</phase> <goals> <goal>replace-file</goal> </goals> <configuration> <filesets> <fileset> <includes> <include>**/file12.txt</include> </includes> <excludes> <exclude>target/**/*</exclude> </excludes> </fileset> </filesets> <regexes> <regex> <pattern>is supposed</pattern> <replacement>is likely</replacement> </regex> </regexes> </configuration> </execution> </executions> </plugin> <plugin> Loading Loading @@ -143,6 +168,11 @@ <checksum>fb1f2b040970526fc99b1339f22465f1</checksum> <type>md5</type> </requireFileChecksum> <requireFileChecksum> <file>${basedir}/folder1/file12.txt</file> <checksum>a7f509275e16ff57cd7756cdca70822a</checksum> <type>md5</type> </requireFileChecksum> </rules> </configuration> </execution> Loading
src/main/java/com/inteligr8/maven/model/Regex.java +2 −4 Original line number Diff line number Diff line Loading @@ -14,8 +14,6 @@ */ package com.inteligr8.maven.model; import org.apache.commons.lang3.StringUtils; public class Regex implements Normalizable { private String pattern; Loading @@ -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 = ""; } }
src/main/java/com/inteligr8/maven/regex/AbstractReplaceMojo.java +13 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -177,6 +186,10 @@ public abstract class AbstractReplaceMojo extends AbstractRegexMojo { targetChannel.close(); } if (overwrite) { Files.move(tofile, file, StandardCopyOption.REPLACE_EXISTING); } return didReplace; } Loading
src/main/java/com/inteligr8/maven/regex/MatchFileContentMojo.java +13 −2 Original line number Diff line number Diff line Loading @@ -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); } Loading @@ -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"); } Loading
src/main/java/com/inteligr8/maven/regex/ReplaceFileContentMojo.java +15 −8 Original line number Diff line number Diff line Loading @@ -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); Loading @@ -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()); } } }