我通过在部署插件的 ~/.m2/settings.xml(可以在任何地方,包括 pom.xml)中定义的属性来定义服务器的密码。我想在集成测试中使用相同的属性。有办法这样做吗?
如果没有,是否有一种方便的方法在 Maven 和 TestNG 之间共享属性?
我想编写一个很好的测试套件,可以在不同的持续集成服务器上运行,指向不同的远程主机(开发、测试、登台和生产),而无需修改代码。
我正在 settings.xml 中定义远程服务的凭据:
<properties>
<my.host>http://my.company.com</my.host>
<my.username>my-un</my.username>
<my.password>my-pw</my.password>
</properties>
我希望能够使用以下方法引用我的单元/集成测试 (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>
是否有任何选项可以执行此操作?以前有其他人尝试过吗?我正在编写很多 REST 测试,这些测试需要在我的测试中进行授权。
谢谢!
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?
If not, is there a convenient way to share properties between Maven and 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:
<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:
<?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.
Thanks!
发布评论
评论(4)
当然。 Maven 资源过滤 是可行的方法。
下面是一个示例配置(匹配
*-context.xml
的文件将被过滤,其他文件则不会):另一种方法是使用 属性 Maven 插件 到 将所有项目属性写入文件 并使用
PropertyPlaceholderConfigurer
机制。Maven配置:
Spring配置:
Sure. Maven resource filtering is the way to go.
Here's a sample configuration (files matching
*-context.xml
will be filtered, others won't):A different approach would be to use the Properties Maven Plugin to write all project properties to a file and reference that file from Spring using the
PropertyPlaceholderConfigurer
mechanism.Maven Configuration:
Spring configuration:
嗯,@seanizer 是在正确的轨道上,但是这可以简化,因为您已经可以在 Maven 中设置属性。将它们设置在您的 pom 和 Spring 配置中,您所需要做的就是访问它们,因此只需像这样更改您的配置即可完成此操作。
该位置不是必需的,因为您感兴趣的属性现在已由 Maven 设置为系统属性。 PropertyPlaceholderConfigurer 将与这些以及文件中定义的任何配置一起使用,在这种特定情况下不需要。请注意,您必须包含上下文的架构。
我会将它们从您当前的位置移走,因为这是一个全局设置,您的 pom 是特定于项目的,所以我认为这就是它所属的位置。
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.
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.
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.
当 maven 使用特定配置文件构建项目时,您可以让 Maven 将特定值替换到您的 xml 文件中。例如,您可以在 maven pom 中设置一个测试 prfile,当您使用该配置文件构建时,jar 中的 xml 文件将具有所需的属性。查看这个作为示例。
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 this for an example.
您可以在属性文件中定义您的值,并使用 pom.xml 中的
${my-pw-key}
引用它们,并使用插件properties-maven-plugin
创建属性文件>属性文件(
env.properties
),然后运行maven
mvn初始化包
(初始化以从属性文件加载值)You can define your values in property file and refer them using
${my-pw-key}
in pom.xml and create a properties file by using pluginproperties-maven-plugin
Properties file (
env.properties
)And then run maven
mvn initialize package
(initialize to load values from property file)