Stefano
Stefano

Reputation: 1722

how to replace placeholder from a source file.properties to another file.properties with Maven

I'm learning how to replace placeholder of a file properties to another file properties with Maven. My project has a main folder named testMaven and I have created two files properties in the project, where the first file is situated in testMaven->local->value.properties and into the file there are:

user=testUser password=testPassword

instead the second file properties situated into src/main/java->test.properties there are:

user=@user@ password=@password@

I would like to have into the file Jar a file properties with the fields replaced with: user=testUser password=testpassword

How do I write in the file pom.xml to do the description above? Thanks

Upvotes: 1

Views: 13272

Answers (1)

atao
atao

Reputation: 845

You will need to work with two plugins:

The documentation available for both of them is straightforward.

Update

Given a custom.properties file:

custom.user=testUser
custom.password=testPassword

Read it with:

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>${properties-maven-version}</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                      <files><file>${jbake.inputDirectory}/custom.properties</file>
                      </files> 
                    </configuration>
                </execution>
            </executions>
        </plugin>

Now, you need to run the replacer for each property:

            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>${replacer-maven-version</version>
                <executions>
                    <execution>
                        <id>replace-for-documentation</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                        <configuration>
                            <preserveDir>true</preserveDir>
                            <basedir>${basedir}/src/main/resources</basedir>
                            <outputBasedir>${basedir}</outputBasedir>
                            <outputDir>src/site</outputDir>
                            <ignoreErrors>true</ignoreErrors>
                            <regex>false</regex>
                            <delimiters>
                                <delimiter>@</delimiter>
                            </delimiters>
                            <filesToInclude>
                                *.properties
                            </filesToInclude>
                            <replacements>
                                <replacement>
                                    <token>user</token>
                                    <value>${custom.user</value>
                                </replacement>
                                <replacement>
                                    <token>password</token>
                                    <value>${custom.password}</value>
                                </replacement>
                            </replacements>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Any occurrence of @user@ or @password@ inside properties files under src/main/resources will be replaced. The generated files will be under src/site

Upvotes: 4

Related Questions