Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container? Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container? spring spring

Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container?


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.