Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including application version number from pom.xml to application bundle

Each pom.xml have:

<groupId>com.vendor</groupId>
<artifactId>product</artifactId>
<version>1.0.13.0-dev</version>  <!-- THIS FIELD -->
<packaging>war</packaging>

version piece is useful to bunch with application so user can see and report them.

But I dislike code duplication and look for a way to avoid putting property file with version info to VCS (Git/SVN) because each time I bump version I must commit into two places (pom.xml and version.properties).

Which way can I make version properties from pom.xml programmatically available in Java application?

like image 326
gavenkoa Avatar asked Apr 01 '13 06:04

gavenkoa


People also ask

How do I add resources to a pom XML file?

Open your pom.xml file and add below under <build>. Note: I’ve added 3 plugins below below. maven-resources-plugin : The Resources Plugin handles the copying of project resources to the output directory. The main resources are the resources associated to the main source code.

How to get the version of an application in Maven?

Alternatively, maven stores the Implementation-Version in the MANIFEST.MF file when building a jar. If you dont mind not having a version until you run it from a jar, you can use this to get the application version. Java allowes for easy access to this using:

How to get the version number of an application?

The upside of this approach is that you always have a version number even when you start your application from your IDE, also by putting your variable in info.app.version, if you use Spring boot actuator, your version will be automatically available under the /info endpoint. However, this approach does require quite a bit of configuration.

How do I find the version of my Spring Boot application?

Spring. Spring Boot. There are a few ways to get your application version with Spring Boot. One approach is using Maven resource filtering to add the version number as an enviroment variable in the application.yml during the build. Another approach is to use the Implementation-Version stored in the manifest file.


2 Answers

Found a solution to use the maven-war-plugin to be more elegant.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
       <webResources>
           <resource>
               <targetPath>WEB-INF/views/jsp</targetPath>
               <directory>${basedir}/src/main/webapp/WEB-INF/views/jsp</directory>
               <includes>
                  <include>footer.jsp</include>
               </includes>
               <filtering>true</filtering>
          </resource>
      </webResources>
  </configuration>
</plugin>

footer.jsp then had:

Application Version: ${project.version}
like image 97
Kutlo Avatar answered Sep 21 '22 13:09

Kutlo


My personal production solution below.

pom.xml:

<?xml version="1.0" encoding="utf-8"?>
<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/xsd/maven-4.0.0.xsd">

    <version>1.0.13.0-dev</version>

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

base.jsp:

<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head></head>
<body>
   <a href='<c:url value="/"/>'>
     <spring:eval expression="@props.getProperty('version')"/>
   </a>
   <jsp:include page="${contentPage}" />
</body>
</html>

proj.properties:

version=${project.version}

applicationContext.xml:

<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean" autowire="byName" >
    <property name="location" value="classpath:proj.properties"/>
</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:proj.properties"/>
</bean>

or just

<util:properties id="props" location="classpath:proj.properties"/>
like image 23
gavenkoa Avatar answered Sep 21 '22 13:09

gavenkoa