montu
montu

Reputation: 23

how to update a property file using maven or pom.xml

I have created an automation framework where I am reading values from a property file say "config.properties".

My config.propertioes file contains following :

BrowserName=${browser}
Environment=${env}

I am reading browser value from the property file and passing it to my selenium script to run it.

Now I wants to replace "${browser}" && "${env}" with value "ie" && "IT" using pom.xml. Is there any way/plugin using which I can edit a property file using pom.xml.

Please suggest.

@Keshava I am putting whole example below as suggested below : 1.Have 2 property files: ◦project.properties: This is the file that we commit in the repository. It consists data as follows: ◾project.connection.username=@@DB_USERNAME@@ project.connection.password=@@DB_PASSWORD@@

◦build.properties: This is the file that we do not commit in the repository and is maintained at each of the deployment environments, be it the developers env., UAT env or Production env. The contents of this file are as follows: ◾db.username=mydbuser db.password=mydbpassword

2.In the project’s pom.xml add the following plugin and a execution:

<plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>maven-replacer-plugin</artifactId>
                <version>1.3.5</version>
                <executions>
                    <execution>
                        <id>replaceTokens</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>target/classes/project.properties</file>
                    <replacements>
                        <replacement>
                            <token>@@DB_USERNAME@@</token>
                            <value>${db.username}</value>
                        </replacement>
                        <replacement>
                            <token>@@DB_PASSWORD@@</token>
                            <value>${db.password}</value>
                        </replacement>
                    </replacements>
                </configuration>
</plugin>

from above, I understand that "@@DB_USERNAME@@" is from "project.properties". But, from which properties file this "${db.username}" value will be taken?. how my pom will understand from where to take "${db.username}" .

Do I need to pass this value in maven goal like below :

mvn clean install -Ddb.username=myuserid

Upvotes: 2

Views: 9620

Answers (3)

ozgur
ozgur

Reputation: 1

maven also has a resolvable, inheritable, and also overrideable section in its nature. You can implement any possible solution using maven profiles together. Your keywords to search for could be "managing maven properties using maven profiles"

Upvotes: 0

Florent Amaridon
Florent Amaridon

Reputation: 146

Hello you can use the maven resource plugin.

This plugin implement "Maven Filtering".

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <id>selenium-profile-chrome</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/selenium</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

Upvotes: 6

Keshava
Keshava

Reputation: 802

you could tryin using the maven replacer plugin See https://code.google.com/archive/p/maven-replacer-plugin/

See an example here

Upvotes: 2

Related Questions