0

I want to update application.properties file values from pom.xml when I run mvn test. values must be passed from pom.xml to application.properties at runtime pom.xml

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>    
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M4</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/test/resources/phomeTestNg.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>    
            </plugins>  
        </pluginManagement>
        <resources>
            <resource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build> 

application.properties file gapp=${project.build.sourceEncoding}

java code

        FileReader reader=new 
        FileReader(System.getProperty("user.dir")+"\\src\\test\\resources\\application.properties");          
        Properties p=new Properties();  
        p.load(reader);           
        System.out.println("Properties  "+p.getProperty("gapp"));  

Folder structure

enter image description here

user3890872
  • 31
  • 1
  • 9

1 Answers1

0

There is a plugin named properties-maven-plugin. That creates a property file with the name you specify. And then you can directly access all the variables just like you do in any property file. It might not be the exact solution. But will give you a direction perhaps. Here's a code sample to have a look at it. This will be your pom :

<properties>
    <name>${project.name}</name>
     <version>${project.version}</version>
     <foo>bar</foo>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>write-project-properties</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.outputDirectory}/my.properties</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Got this code from another question. https://stackoverflow.com/a/26589696/7241652

Durlabh Sharma
  • 397
  • 2
  • 14