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

Ste*_*ven 17 java continuous-integration spring maven-2

我通过我在〜/ .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>
Run Code Online (Sandbox Code Playgroud)

我希望能够使用以下方法在我的单元/集成测试(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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

Sea*_*oyd 31

当然.Maven资源过滤可行的方法.

这是一个示例配置(文件匹配*-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>
Run Code Online (Sandbox Code Playgroud)

一种不同的方法是使用属性Maven插件写所有的项目属性文件和参考,从春季使用文件PropertyPlaceholderConfigurer机制.

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>
Run Code Online (Sandbox Code Playgroud)

弹簧配置:

<context:property-placeholder location="classpath:mavenproject.properties"/>
Run Code Online (Sandbox Code Playgroud)


Rob*_*bin 5

好吧,@ 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>
Run Code Online (Sandbox Code Playgroud)

由于您感兴趣的属性现在由maven设置为系统属性,因此不需要该位置.PropertyPlaceholderConfigurer将使用这些以及文件中定义的任何内容,在此特定情况下不需要.请注意,您必须包含上下文的架构.

我会将它们从你当前的位置移开,因为这是一个全局设置,你的pom是项目特定的所以我认为这是它所属的地方.


mR_*_*r0g 1

当 maven 使用特定配置文件构建项目时,您可以让 Maven 将特定值替换到您的 xml 文件中。例如,您可以在 maven pom 中设置一个测试 prfile,当您使用该配置文件构建时,jar 中的 xml 文件将具有所需的属性。看一下这个例子。