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?
Have a look at the Maven Resources Plugin. In your version.properties
(should be in the resources
folder), define a line like version=${project.version}
. Make sure to turn on filtering for the plugin (all described in detail at the link above) and you should be good to go!
Just run mvn resources:resources
(or mvn compile
) and check the generated file.
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}
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"/>
You can simply configure the maven-war-plugin, maven-jar-plugin, maven-ear-plugin like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>
Reading information from manifest.mf can be done using jacabi.