简体   繁体   中英

How to updates values of properties files from pom.xml

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

在此处输入图片说明

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

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