java 我可以将来自 Maven 的属性(在 settings.xml 中定义的密码)注入到我的 Spring 容器中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4107222/
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
Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container?
提问by Steven
I define passwords to servers via properties I define in my ~/.m2/settings.xml (could be anywhere, though, including pom.xml) for my deployment plugin. I'd like to use the same properties for my integration tests. Is there a way to do so?
我通过我在 ~/.m2/settings.xml(可以在任何地方,包括 pom.xml)中为我的部署插件定义的属性来定义服务器的密码。我想为我的集成测试使用相同的属性。有没有办法这样做?
If not, is there a convenient way to share properties between Maven and TestNG?
如果没有,是否有一种方便的方法可以在 Maven 和 TestNG 之间共享属性?
I want to write a nice test suite that can run on different continuous integration servers, pointing to different remote hosts (development, testing, staging, and production), without modification of the code.
我想编写一个很好的测试套件,它可以在不同的持续集成服务器上运行,指向不同的远程主机(开发、测试、登台和生产),而无需修改代码。
I am defining credentials to a remote service in settings.xml:
我正在 settings.xml 中定义远程服务的凭据:
<properties>
<my.host>http://my.company.com</my.host>
<my.username>my-un</my.username>
<my.password>my-pw</my.password>
</properties>
I'd like to be able to reference the properties in my unit/integration tests (src/test/resources) using:
我希望能够使用以下方法引用我的单元/集成测试(src/test/resources)中的属性:
<?xml version="1.0" encoding="UTF-8"?>
<beans....
<bean class="java.lang.String" id="un">
<constructor-arg value="${my.username}"/>
</bean>
<bean class="java.lang.String" id="pw">
<constructor-arg value="${my.password}"/>
</bean>
</beans>
Are there any options to doing this? Has anyone else tried this before? I am writing a lot of REST tests which require authorization in my tests.
有没有办法做到这一点?有没有其他人试过这个?我正在编写许多 REST 测试,这些测试需要在我的测试中获得授权。
Thanks!
谢谢!
回答by Sean Patrick Floyd
Sure. Maven resource filteringis the way to go.
当然。Maven 资源过滤是要走的路。
Here's a sample configuration (files matching *-context.xml
will be filtered, others won't):
这是一个示例配置(匹配的文件*-context.xml
将被过滤,其他则不会):
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*-context.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*-context.xml</exclude>
</excludes>
</resource>
</resources>
</build>
A different approach would be to use the Properties Maven Pluginto write all project properties to a fileand reference that file from Spring using the PropertyPlaceholderConfigurer
mechanism.
一种不同的方法是使用属性Maven插件来写所有的项目属性文件,并使用从Spring参考该文件的PropertyPlaceholderConfigurer
机制。
Maven Configuration:
Maven配置:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>generate-test-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.testOutputDirectory}/mavenproject.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Spring configuration:
弹簧配置:
<context:property-placeholder location="classpath:mavenproject.properties"/>
回答by Robin
Well, @seanizer is on the right track, but this can be simplified since you can already set your properties in maven. Set them in your pom and in your Spring config, all you need to do is get access to them, so simply changing your config like this will accomplish that.
好吧,@seanizer 走在正确的轨道上,但这可以简化,因为您已经可以在 maven 中设置您的属性。在您的 pom 和 Spring 配置中设置它们,您需要做的就是访问它们,因此只需像这样更改您的配置即可实现。
<beans....
<context:property-placeholder />
<bean class="java.lang.String" id="un">
<constructor-arg value="${my.username}"/>
</bean>
<bean class="java.lang.String" id="pw">
<constructor-arg value="${my.password}"/>
</bean>
</beans>
The location is not required since the properties you are interested in are now set as system properties by maven. The PropertyPlaceholderConfigurer will work with those as well as any that are defined in a file, which is not required in this specific case. Please note, you will have to include the schema for context.
该位置不是必需的,因为您感兴趣的属性现在已被 maven 设置为系统属性。PropertyPlaceholderConfigurer 将与那些以及在文件中定义的任何一起工作,这在这种特定情况下不是必需的。请注意,您必须包含context的架构。
I would move them from your current location though as that is a global setting, your pom is project specific so I think that is where it belongs.
我会将它们从您当前的位置移开,因为这是一个全局设置,您的 pom 是特定于项目的,所以我认为这就是它所属的位置。
回答by mR_fr0g
You can get Maven to substitute a particular value into your xml file when maven builds your project with a certain profile. So for example you would set up a test prfile in your maven pom and when you build with that profile the xml file in your jar will have the desired property in it. Look at thisfor an example.
当 Maven 使用特定配置文件构建您的项目时,您可以让 Maven 将特定值替换到您的 xml 文件中。因此,例如,您将在 maven pom 中设置一个测试 prfile,当您使用该配置文件构建时,jar 中的 xml 文件将具有所需的属性。看看这个例子。