Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 395
Steven Avatar asked Nov 05 '10 15:11

Steven


1 Answers

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"/>
like image 53
Sean Patrick Floyd Avatar answered Sep 20 '22 13:09

Sean Patrick Floyd