1

I'm displaying some version metadata in my web application, and I do this using a Maven resources filter that will update one of my properties files.

    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>build-info.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>build-info.properties</exclude>
            </excludes>
        </resource>
    </resources>

My properties file

build.version = ${project.version}
build.number = ${BUILD_NUMBER}
build.commit = ${git.commit.id.abbrev}

This all works great, the appropriate information gets added to the resource when I package the application.

My problem is that I tried to use a Spring PropertyPlaceholderConfigurer to wire together a bean containing the version information, but this doesn't like the ${} syntax. I can no longer run the application locally without an ugly workaround. I'm getting the following exception:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'com.mycompany.abc.dm.system.BuildInfo 
... 
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.version' in string value "${project.version}"

Is there anyway around this using the PropertyPlaceholderConfigurer?

Here is the snippet from the Spring XML context

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:build-info.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="1000" />
</bean>
<bean class="com.mycompany.abc.dm.system.BuildInfo">
    <constructor-arg index="0" value="${build.version}"/>
    <constructor-arg index="1" value="${build.number}"/>
    <constructor-arg index="2" value="${build.commit}"/>
</bean>

"Solution"

I never found a way around this issue using the PropertyPlaceholderConfigurer. My solution was to not use PropertyPlaceholderConfigurer. I'm still reading the properties file, just not with Spring magic.

The Gilbert Arenas Dagger
  • 12,071
  • 13
  • 66
  • 80
  • Why do you think `ignoreUnresolvablePlaceholders` is an ugly workaround? – M4ver1k Apr 15 '16 at 04:32
  • I'm confused, are you having Maven filter the Spring XML file directly (which I believe is the correct approach--just modify your pom.xml for that), or are you trying to have the Spring XML file read the resolved values in the build-info.properties file? – Glen Mazza Apr 15 '16 at 04:35
  • 1
    @M4ver1k ignoreunresolvableplaceholders doesn't work here. If that was the solution i would be happy. The workaround I know involves creating another properties file with class path preference – The Gilbert Arenas Dagger Apr 15 '16 at 09:52
  • @GlenMazza I tried filtering the Spring XML file directly, but I still face the same problem. Those properties would not be replaced until deployment so Spring still fails on startup because it cannot replace the bean properties. As far as my goal here, it's just to load the spring application context. I don't care if this build information is resolved when I run locally. – The Gilbert Arenas Dagger Apr 15 '16 at 13:26
  • I would still recommend configuring Maven to filter the Spring XML file directly. This is most easily treated as a Maven issue -- how do you configure Maven to filter values into an XML file -- not a Spring issue, of how do you get Spring to filter from a config file into an XML one. – Glen Mazza Apr 15 '16 at 22:39

0 Answers0