如何从pom.xml更新属性文件的值

问题描述 投票:0回答:1

我想在运行mvn test时从pom.xml更新application.properties文件的值。值必须在运行时从pom.xml传递到application.propertiespom.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文件gapp = $ {project.build.sourceEncoding}

java代码

        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"));  

文件夹结构

enter image description here

java maven pom.xml
1个回答
0
投票

有一个名为properties-maven-plugin的插件。这将使用您指定的名称创建一个属性文件。然后,您可以像访问任何属性文件一样直接访问所有变量。这可能不是确切的解决方案。但是也许会给你一个方向。这是一个代码示例,供您查看。这将是您的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>
© www.soinside.com 2019 - 2024. All rights reserved.