我有一个Maven项目,它针对数据库运行一些测试.我可以使用这些测试,mvn clean verify
但我想指定一些数据库属性,例如.数据库名称,端口名称等...在我的pom.xml文件中,我可以在Java中使用它来创建数据库连接.
我已将此添加到我的POM中,
<properties>
<server>localhost</server>
<database>mydatabase</database>
<port>1433</port>
</properties>
Run Code Online (Sandbox Code Playgroud)
我可以从我的Java代码中访问这些属性,还是需要创建一个Maven插件并将这些属性作为参数传递给我的插件并clean verify
从我的自定义插件调用?
启用资源过滤(例如,用于测试资源):
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
Run Code Online (Sandbox Code Playgroud)
在以下位置创建db.properties src/test/resources
:
server=${server}
port=${port}
Run Code Online (Sandbox Code Playgroud)
在您的测试中,请阅读db.properties
:
InputStream inputStream = getClass().getResourceAsStream("/db.properties")
Properties props = new Properties();
props.load(inputStream);
Run Code Online (Sandbox Code Playgroud)
Maven:完整参考资料概述了它的工作原理: