String replace in Maven with regex


October 29th 2019


I had an issue where I needed to replace all backslashes in a Maven property with forward slashes. The build helper plugin did the trick.

<code> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>${maven.build-helper.version}</version> <executions> <execution> <id>regex-property</id> <goals> <goal>regex-property</goal> </goals> <configuration> <name>some.property.replaced</name> <value>${some.property}</value> <regex>\\</regex> <replacement>/</replacement> <failIfNoMatch>false</failIfNoMatch> </configuration> </execution> </plugin> </code>

To validate that this all worked, the antrun plugin can echo out the new property:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>${maven.antrun.version}</version> <executions> <execution> <id>echo-property</id> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>some.property.replaced=${some.property.replaced}</echo> </tasks> </configuration> </execution> </plugin>
I believe that software development is fundamentally about making decisions, and so this is what I write about (mostly). In 2018 I started Highline Solutions, a consulting practice that helps companies with architecture, devops, and full-stack development. I have two degrees from Carnegie Mellon University, one in Information and Decision Systems and one in Philosophy (thesis). I live in Pittsburgh, PA with my wife and 3 energetic boys.

Got a Comment?


Comments (0)

 None so far!