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. 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" encodi开发者_运维知识库ng="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!
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"/>
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.
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.
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)
精彩评论