【发布时间】:2013-11-20 08:51:59
【问题描述】:
我尝试在 this thread 之后在 Maven 中使用属性
我创建了一个属性:
<properties>
<application.root.url>http://localhost:8080</application.root.url>
</properties>
添加资源和过滤:
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
将“test.properties”文件放在 src/test/resources 目录下:
application.root.url=${project.application.root.url}
在 Java 中,我有这个:
public static Properties getProperties(){
Properties props = new Properties();
try {
props.load(new FileInputStream("test.properties"));
} catch (IOException e){
props = null;
e.printStackTrace();
}
return props;
}
现在,如果我有new FileInputStream("test.properties"),我会得到“找不到文件”异常,如果我输入new FileInputStream("src/test/resources/test.properties"),那么它会返回${application.root.url} 作为属性application.root.url 的值
我做错了什么?
更新 1:
好的,现在我变了
${project.application.root.url}到
${application.root.url}
和
props.load(new FileInputStream("test.properties"));到
props.load(Util.class.getClassLoader().getResourceAsStream("test.properties"));
现在我不再收到“找不到文件”错误。但我仍然有问题,target/test-classes/test.properties 中的值没有从${application.root.url} 更改(并且在target/classes/test.properties 它是 更改...)
最终更新:
最后,当我将“资源”更改为“测试资源”时,一切都开始了:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
谢谢大家的建议!
【问题讨论】:
-
如果将properites 文件移动到主源根目录而不是 test 会发生什么?我的意思是将 test.properties 移动到 /src/main/resources
-
试过了。即使
src\test\resources文件被复制到target\classes目录,但找不到它。我在target\test-classes中也有相同的文件(但 ${application.root.url} 未更改),但仍然出现“找不到文件”异常...... -
<testResources> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources>
标签: java maven properties