简体   繁体   English

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

[英]Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container?

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.我通过在 ~/.m2/settings.xml 中为我的部署插件定义的属性(可以在任何地方,包括 pom.xml)来定义服务器的密码。 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?如果没有,是否有一种方便的方法可以在 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!谢谢!

Sure.当然。 Maven resource filtering is 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 Plugin to write all project properties to a file and reference that file from Spring using the PropertyPlaceholderConfigurer mechanism .另一种方法是使用Properties Maven Plugin 将所有项目属性写入文件,并使用PropertyPlaceholderConfigurer机制从 Spring 引用该文件。

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

Well, @seanizer is on the right track, but this can be simplified since you can already set your properties in maven.好吧,@seanizer 走在正确的轨道上,但这可以简化,因为您已经可以在 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.在您的 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.该位置不是必需的,因为您感兴趣的属性现在已被 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. PropertyPlaceholderConfigurer 将处理这些文件以及文件中定义的任何文件,在这种特定情况下不需要。 Please note, you will have to include the schema for context .请注意,您必须包含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 是特定于项目的,所以我认为这就是它所属的位置。

You can get Maven to substitute a particular value into your xml file when maven builds your project with a certain profile.当 Maven 使用特定配置文件构建您的项目时,您可以让 Maven 将特定值替换到您的 xml 文件中。 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.因此,例如,您将在 maven pom 中设置一个测试 prfile,当您使用该配置文件构建时,jar 中的 xml 文件将包含所需的属性。 Look at this for an example.看看这个例子。

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 plugin properties-maven-plugin您可以在属性文件中定义您的值并在 pom.xml 中使用${my-pw-key}引用它们并使用插件properties-maven-plugin创建一个属性文件

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>${basedir}/src/main/resources/env.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Properties file ( env.properties )属性文件( env.properties

my-pw-key=abc&123 #your password here

And then run maven mvn initialize package (initialize to load values from property file)然后运行 ​​maven mvn initialize package (初始化以从属性文件加载值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM