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

发布于 2024-10-01 00:06:54 字数 945 浏览 5 评论 0 原文

我通过在部署插件的 ~/.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 测试,这些测试需要在我的测试中进行授权。

谢谢!

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?

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.

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>

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>

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城旧梦 2024-10-08 00:06:54

当然。 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 插件将所有项目属性写入文件 并使用 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"/>

Sure. Maven resource filtering is the way to go.

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>

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 Configuration:

<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"/>
做个少女永远怀春 2024-10-08 00:06:54

嗯,@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 是特定于项目的,所以我认为这就是它所属的位置。

Well, @seanizer is on the right track, but this can be simplified since you can already set your properties in 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.

<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. The PropertyPlaceholderConfigurer will work with those as well as any that are defined in a file, which is not required in this specific case. Please note, you will have to include the schema for 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.

蓝海似她心 2024-10-08 00:06:54

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

You can get Maven to substitute a particular value into your xml file when maven builds your project with a certain profile. 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. Look at this for an example.

你的呼吸 2024-10-08 00:06:54

您可以在属性文件中定义您的值,并使用 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初始化包(初始化以从属性文件加载值)

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

        <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)

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

And then run maven mvn initialize package (initialize to load values from property file)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文