我可以从Maven(在settings.xml中定义的密码)注入属性到我的Spring容器吗? [英] Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container?

查看:114
本文介绍了我可以从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>



我希望能够引用我的单元/集成测试中的属性/ resources)using:

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>






另一种方法是使用属性Maven插件将所有项目属性写入文件,并使用 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>

Spring配置:

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

这篇关于我可以从Maven(在settings.xml中定义的密码)注入属性到我的Spring容器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆