Abhay Agarwal
Abhay Agarwal

Reputation: 348

Override property key's value in maven

I have global property file (global.properties) with below mentioned content

app.server.username=globalUser

I have another property file called sample.properties with below mentioned content

app.server.username=sampleUser
app.server.port=443

Now, I need to replace app.server.username key value from "sampleUser" to "globalUser" using Maven , with packaging of jar file happens.

These 2 properties file is placed in same folder within Java project.

So, during Maven build phase (or in package phase) , Maven should refer to global.properties, search for all key value pairs (defined in it) in sample.properties. And replace value of all matched keys.

So , after Maven build, sample.properties files should have below mentioned content

app.server.username=globalUser
app.server.port=443

Please suggest how to do it in Maven ?

Upvotes: 1

Views: 369

Answers (1)

ernest_k
ernest_k

Reputation: 45309

You can use the properties plugin for that, specifying multiple files to be read:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>global.properties</file>
                    <file>sample.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>

From my test results, the latter files will cause duplicate property keys to override the former ones. So just ensure that your files are listed in the correct order.

Upvotes: 1

Related Questions