Migrating from Maven to Gradle and are using Spring. How do we get gradle properties into Spring?


(Justin Hardin) #1

We use Springs PropertiesFactoryBean and PropertySourcesPlaceholderConfigurer to grab layered properties from the filesystem. Our POM.xml declares bar and then in our .property files we can reference the variable via a placeholder ${foo} ?Spring? magically replaces the value of ${foo} with the value declared in the POM.xml (I’m not 100% sure how this works)

How can we declare a property in build.gradle or gradle.properties or settings.gradle and have that brought into the placeholders in our Spring PropertyBean?

base.properties

foo=${foo}

root-context.xml

<bean id="configProperties"
          class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <set>
                <value>classpath:config/env/base.properties</value>
                <value>classpath:environment.properties</value>
            </set>
        </property>
    </bean>
      <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
    </bean>

pom.xml (snippet)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <properties>
        <foo>bar</foo>
  </properties>

(Perryn Fowler) #2

I suspect that it is actually Maven Resource Filtering replacing your property values:

http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-description.html

If so, then Gradle has a similar mechanism built into any Copy task

http://www.gradle.org/docs/current/userguide/userguide_single.html#filterOnCopy

Note that ‘processResources’ from the Java plugin is such a Copy task


(Justin Hardin) #3

Holy smokes…THANK YOU Perryn.

Another engineer set this stuff up… and I just wasn’t grokking it until today. It all makes perfect sense now. I was missing the key config in our giant POM.xml that was accomplishing this:

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

Wahoo! The pieces are starting to fall together…