Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access maven project version in Spring config files

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?

like image 792
Tauren Avatar asked Oct 07 '10 23:10

Tauren


People also ask

Where would you find a Maven spring boot configuration file?

properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.

What is project version?

Project Version Variables are a copy of the Domain Global Variables. The copy is created when a Project Version Variable Set is added to a selected Project. A copy will be made of each Domain Global Variable within the Project or Project Version.


2 Answers

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).

like image 99
Pascal Thivent Avatar answered Sep 26 '22 00:09

Pascal Thivent


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

like image 20
drekka Avatar answered Sep 23 '22 00:09

drekka