我可以将来自 Maven 的属性(在 settings.xml 中定义的密码)注入到我的 Spring 容器中吗?

我通过我在 ~/.m2/settings.xml 中为我的部署插件定义的属性定义服务器密码(不过,可以在任何地方,包括 pom.xml).我想为我的集成测试使用相同的属性.有办法吗?

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?

如果没有,有没有一种方便的方法可以在 Maven 和 TestNG 之间共享属性?

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.

我在 settings.xml 中定义远程服务的凭据:

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>

我希望能够使用以下方法在我的单元/集成测试 (src/test/resources) 中引用属性:

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>

有什么选择吗?有没有其他人尝试过这个?我正在编写很多需要在我的测试中授权的 REST 测试.

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.

谢谢!

推荐答案

当然.Maven 资源过滤 是要走的路.

这是一个示例配置(匹配 *-context.xml 的文件将被过滤,其他文件不会):

Here's a sample configuration (files matching *-context.xml will be filtered, others won't):

<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>

<小时>

另一种方法是使用 Properties Maven 插件 到 将所有项目属性写入文件并从Spring 使用 PropertyPlaceholderConfigurer机制.


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 配置:

<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>

弹簧配置:

<context:property-placeholder location="classpath:mavenproject.properties"/>

相关文章