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

问题描述 投票:17回答:4

我通过在部署插件的 ~.m2settings.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>

我想在我的单元集成测试(srctestresources)中引用这些属性。

<?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测试,这些测试需要授权。

谢谢!我通过属性来定义服务器的密码。

java spring maven-2 continuous-integration
4个回答
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>

一个不同的方法是使用 属性Maven插件将所有项目属性写入文件 并从Spring中引用该文件,使用 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>

Spring配置。

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

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>

由于你感兴趣的属性现在已经被maven设置为系统属性,所以不需要设置位置。 PropertyPlaceholderConfigurer可以使用这些属性,也可以使用任何在文件中定义的属性,在这种情况下不需要。 请注意,你必须在文件中包含以下模式 语境.

我会把它们从你当前的位置移开,因为那是一个全局设置,而你的pom是特定项目的,所以我认为它应该在那里。


1
投票

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


0
投票

你可以在属性文件中定义你的值,并使用 ${my-pw-key} 在pom.xml中创建一个属性文件,并使用插件------------。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 (初始化以从属性文件中加载数值)

© www.soinside.com 2019 - 2024. All rights reserved.