java 使用 Maven 属性连接到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14283066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Maven properties to connect to a database
提问by The Cat
I have a Maven project that runs some tests against a database. I can run these tests using mvn clean verify
but I want to specify some database properties eg. database name, port name etc... in my pom.xml file that I can use within Java to create a database connection.
我有一个针对数据库运行一些测试的 Maven 项目。我可以使用这些测试来运行,mvn clean verify
但我想指定一些数据库属性,例如。数据库名称、端口名称等...在我的 pom.xml 文件中,我可以在 Java 中使用它来创建数据库连接。
I have added this to my POM,
我已将此添加到我的 POM 中,
<properties>
<server>localhost</server>
<database>mydatabase</database>
<port>1433</port>
</properties>
Can I access these properties from my Java code, or will I need to create a Maven plugin and pass these properties as parameters to my plugin and call clean verify
from my custom plugin?
我可以从我的 Java 代码访问这些属性,还是我需要创建一个 Maven 插件并将这些属性作为参数传递给我的插件并clean verify
从我的自定义插件调用?
回答by aldrinleal
Enable Resource Filtering (e.g., for test resources):
启用资源过滤(例如,对于测试资源):
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
Create a db.properties under src/test/resources
:
创建一个 db.properties 下src/test/resources
:
server=${server}
port=${port}
In your test, read db.properties
:
在你的测试中,阅读db.properties
:
InputStream inputStream = getClass().getResourceAsStream("/db.properties")
Properties props = new Properties();
props.load(inputStream);
Maven: The Complete Referencehas an overview on how it works:
Maven: The Complete Reference概述了它的工作原理:
回答by Lee Meador
When you run your tests you can pass Maven properties into the java application with Surefire plugin equivalent of -D on the command line that runs the tests.\
当您运行测试时,您可以在运行测试的命令行上使用与 -D 等效的 Surefire 插件将 Maven 属性传递到 Java 应用程序中。\
This shows how to do that:
这显示了如何做到这一点:
http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html
http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html
回答by Kent
No you cannot from your project use those properties (maven properties) by System.getProperty()
.
不,您不能从您的项目中使用这些属性(Maven 属性)System.getProperty()
。
because maven works at building time, and your codes work at runtime. But you could let maven set some properties (properties file for example) during building, then your class could get those values.
因为 maven 在构建时工作,而您的代码在运行时工作。但是您可以让 maven 在构建期间设置一些属性(例如属性文件),然后您的类就可以获得这些值。
Usually system properties were set when jvm starts. If the VM, on which your project is running, is started from maven, by some plugin for example mvn jetty:run
then you have chance to get the properties defined in maven. However if your project is running on a VM, which has nothing to do with maven, you cannot get those properties.
通常在 jvm 启动时设置系统属性。如果运行项目的 VM 是从 Maven 启动的,例如通过某些插件,mvn jetty:run
那么您就有机会获得在 Maven 中定义的属性。但是,如果您的项目在与 maven 无关的 VM 上运行,则无法获取这些属性。