简体   繁体   中英

How to replace value of a key in maven

I have a simple property file, containing the following lines:

abc=123
location=C:\temp
version=1.0.0

Now, using some Maven plugin, I need to change the value of version from 1.0.0 to some other value.

I know I can use the maven-replacer-plugin by replacing 1.0.0 with some token (say $my_version ). Then, I could use this token to replace the value on the fly.

However, I don't want to use the token value; what I need is to replace all the text after the = (equal sign). How can I do that?

You can use maven-antrun-plugin to replace a string in the source code. Declare resources and plugin as below:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <phase>prepare-package</phase>
                <configuration>
                    <tasks>
                        <replace token="<strToBeReplaced>" value="<yourValue>" dir="target/classes">
                            <include name="**/*.properties" />
                        </replace>
                    </tasks>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</build>

Run mvn clean install the string/token in the properties file will be replaced by the value given in pom.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM