Java 我可以将Maven中的属性(在settings.xml中定义的密码)注入到Spring容器中吗?
我通过在部署插件的~/.m2/settings.xml(可以是任何地方,包括pom.xml)中定义的属性来定义服务器的密码。我想在集成测试中使用相同的属性。有没有办法做到这一点 如果没有,是否有一种方便的方式在Maven和TestNG之间共享属性 我想编写一个很好的测试套件,可以在不同的持续集成服务器上运行,指向不同的远程主机(开发、测试、登台和生产),而无需修改代码 我正在settings.xml中定义远程服务的凭据:Java 我可以将Maven中的属性(在settings.xml中定义的密码)注入到Spring容器中吗?,java,spring,maven-2,continuous-integration,Java,Spring,Maven 2,Continuous Integration,我通过在部署插件的~/.m2/settings.xml(可以是任何地方,包括pom.xml)中定义的属性来定义服务器的密码。我想在集成测试中使用相同的属性。有没有办法做到这一点 如果没有,是否有一种方便的方式在Maven和TestNG之间共享属性 我想编写一个很好的测试套件,可以在不同的持续集成服务器上运行,指向不同的远程主机(开发、测试、登台和生产),而无需修改代码 我正在settings.xml中定义远程服务的凭据: <properties> <my.host>htt
<properties>
<my.host>http://my.company.com</my.host>
<my.username>my-un</my.username>
<my.password>my-pw</my.password>
</properties>
http://my.company.com
我的联合国
我的
我希望能够使用以下方法引用单元/集成测试(src/test/resources)中的属性:
当Maven使用特定概要文件构建项目时,您可以让Maven将特定值替换到xml文件中。例如,您可以在maven pom中设置一个测试文件,当您使用该概要文件构建时,jar中的xml文件将具有所需的属性。请看一个例子。当然。这是一条路要走
下面是一个示例配置(匹配*-context.xml
的文件将被过滤,其他文件不会被过滤):
src/main/resources
真的
**/*-context.xml
src/main/resources
假的
**/*-context.xml
另一种方法是使用从Spring到并引用该文件
Maven配置:
org.codehaus.mojo
属性maven插件
1.0-α-2
生成测试资源
写入项目属性
${project.build.testOutputDirectory}/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>
您可以在属性文件中定义您的值,并使用pom.xml中的${my pw key}
引用这些值,并使用pluginproperties 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>
然后运行mavenmvn initialize package
(initialize以从属性文件加载值)+1,这正是我在阅读文档后要查找的内容。最初忽略“筛选”是另一回事。根据您的工作流,这些属性(尤其是密码)可能会显示在您的生成中,例如在CI环境中。
<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>
my-pw-key=abc&123 #your password here