繁体   English   中英

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

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

我通过在 ~/.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 测试。

谢谢!

当然。 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>

另一种方法是使用Properties Maven Plugin 将所有项目属性写入文件,并使用PropertyPlaceholderConfigurer机制从 Spring 引用该文件。

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

好吧,@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>

该位置不是必需的,因为您感兴趣的属性现在已被 maven 设置为系统属性。 PropertyPlaceholderConfigurer 将处理这些文件以及文件中定义的任何文件,在这种特定情况下不需要。 请注意,您必须包含context的架构。

我会将它们从您当前的位置移走,因为这是一个全局设置,您的 pom 是特定于项目的,所以我认为这就是它所属的位置。

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

您可以在属性文件中定义您的值并在 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>

属性文件( env.properties

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

然后运行 ​​maven mvn initialize package (初始化以从属性文件加载值)

暂无
暂无

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

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