我想使用属性文件来进行某些测试(Junit),具体取决于我在哪里启动测试。为此,我生成了以下POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Project</groupId>
<artifactId>Project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Project</name>
<properties>
<main-filters-directory>src/main/filters</main-filters-directory>
<main-filtered-directory>src/main/config</main-filtered-directory>
</properties>
<profiles>
<profile>
<id>env-local</id>
<activation>
<property>
<name>env</name>
<value>local</value>
</property>
</activation>
<properties>
<env>local</env>
</properties>
<build>
<filters>
<filter>${main-filters-directory}/local.properties</filter>
</filters>
<resources>
<resource>
<directory>${main-filtered-directory}</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>env-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<env>dev</env>
</properties>
<build>
<filters>
<filter>${main-filters-directory}/dev.properties</filter>
</filters>
<resources>
<resource>
<directory>${main-filtered-directory}</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>filters</id>
<activation>
<property>
<name>env</name>
</property>
</activation>
<build>
<filters>
<filter>${main-filters-directory}/${env}.properties</filter>
</filters>
</build>
</profile>
</profiles>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
然后在我的代码中,我有类似的东西:
File dir = new File("");
String rootPath = dir.getAbsolutePath();
String appConfigPath = rootPath + "/src/main/config/configuration.properties";
Properties configProps = new Properties();
configProps.load(new FileInputStream(appConfigPath));
System.out.println(configProps.getProperty("${archivePort}"));
然后我使用“ mvn -Denv =本地测试”运行项目
但是System.out.println输出为“ null”
我想念什么吗? 我知道我可以使用Spring来获取带有@value注释的值,但我认为这不是问题所在
local.properties:
hostAdress=localhost
archivePort=11114
configuration.properties:
hostAdress=${hostAdress}
archivePort=${archivePort}
谢谢您的回答!
瓦茨拉夫