我想在运行 mvn test 时更新 pom.xml 中的 application.properties 文件值。 值必须在运行时从 pom.xml 传递到 application.properties 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 文件 间隙=${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"));
文件夹结构
最佳答案
有一个名为 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>
从另一个问题中获取此代码。 https://stackoverflow.com/a/26589696/7241652
关于java - 如何从 pom.xml 更新属性文件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118380/