Access maven project version in Spring config files
Asked Answered
P

5

5

I would like to display the currently running version of my web application in the page. The project is based on Maven, Spring, and Wicket.

I'd like to somehow get the value of maven's ${project.version} and use it in my spring XML files, similarly to the way I use the Spring PropertyPlaceholderConfigurer to read a property file for settings that I use in my application.

If I had the maven project.version available as a variable in my Spring config, I could do something like this:

<bean id="applicationBean" class="com.mysite.web.WicketApplication">
<property name="version"><value>${project.version}</value></property>
</bean>

How can I do this?

Plenish answered 7/10, 2010 at 23:51 Comment(0)
O
11

You can use Maven filtering as already suggested.

Or you could just read the pom.properties file created by Maven under META-INF directory directly with Spring:

<util:properties id="pom" 
     location="classpath:META-INF/groupId/artifactId/pom.properties" />

and use the bean.

The only drawback of the later approach is that the pom.properties is created at package phase time (and won't be there during, say, test).

Ogren answered 8/10, 2010 at 0:35 Comment(3)
yup, this one's the nicer solution (+1)Nathanialnathaniel
great link, includes exactly what I needed. Shows how to include and exclude filtering for different resources, unlike the getting started guide linked to by @Derek. Also thanks for the alternative, but filtering does the trick just fine.Plenish
Where is that line supposed to go?Immutable
F
7

One technique would be to use mavens filtering. You can insert placeholders in resource files like which then get replaced with values from the build during the resource phase.

Look up "How do I filter resource files?" in the Maven getting started guide

Forensic answered 8/10, 2010 at 0:7 Comment(0)
S
1

Use the @PropertySource annotation to add the pom.properties file created by Maven in the META-INF directory. Note that the file doesn't exist until the package phase. To avoid errors during testing set the ignoreResourceNotFound=true and add a default value on the property being read (e.g. none below). 

@Service
@PropertySource(value = "classpath:META-INF/maven/io.pivotal.poc.tzolov/hawq-rest-server/pom.properties", ignoreResourceNotFound=true)
public class MyClass {
    private String applicationPomVersion;

    @Autowired
    public MyClass(@Value("${version:none}") String applicationVersion ) {
        this.applicationPomVersion = applicationVersion;
    }

    public String getApplicationPomVersion() {
        return this.applicationPomVersion;
    }
}
Stays answered 21/5, 2015 at 10:11 Comment(0)
U
0

We deploy property files outside of the web app. The files can then be filtered at deployment time.

If using Jetty one can put the file under $JETTY_HOME/resources or use the extraClassPath feature to load the property file at runtime.

Ultann answered 1/8, 2011 at 19:23 Comment(0)
P
0

I think the right way of gather application version is the one explained in this thread: https://mcmap.net/q/100548/-get-maven-artifact-version-at-runtime through getClass().getPackage().getImplementationVersion().

Privily answered 4/4, 2016 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.