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.


    <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>

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'm an "old" programmer who has been blogging for almost 20 years now. In 2017, I started Highline Solutions, a consulting company that helps with software architecture and full-stack development. I have two degrees from Carnegie Mellon University, one practical (Information and Decision Systems) and one not so much (Philosophy - thesis here). Pittsburgh, PA is my home where I live with my wife and 3 energetic boys.
We're hiring! Looking for a full-stack developer (React, NodeJS or Java, AWS) open to contract work. Fully remote. The model at Highline is a little different - we're more of a co-op than a traditional consultancy. Our goal is to reward the person doing the work, and keep everything else streamlined. If you're interested, hit me up at ben@highlinesolutions.io to find out more. Send along a resume, or better yet a TechRez!
I recently released a web app called TechRez, a "better resume for tech". The idea is that instead of sending out the same-old static PDF resume that's jam packed with buzz words and spans multiple pages, you can create a TechRez, which is modern, visual, and interactive. Try it out for free!
Got a Comment?
Comments (0)

 None so far!