#!/bin/sh branch=`git branch|grep \*|cut -d" " -f 2` commit=`git id` echo "buildNumber=${branch}.${commit}" > "$1"
<properties> <versionfile>${project.basedir}/version.properties</versionfile> </properties>
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <workingDirectory>${project.basedir}</workingDirectory> <executable>/bin/sh</executable> <arguments> <argument>createprop.sh</argument> <argument>${versionfile}</argument> </arguments> </configuration> </plugin>
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>${versionfile}</file> </files> </configuration> </execution> </executions> </plugin>
<plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> <manifestEntries> <Implementation-Version>${project.version}-${buildNumber}</Implementation-Version> <Main-Class>ru.habrahabr.sandello.Version</Main-Class> </manifestEntries> </archive> </configuration> </plugin>
<plugin> <artifactId>maven-clean-plugin</artifactId> <configuration> <filesets> <fileset> <directory>${project.basedir}</directory> <includes> <include>${versionfile}</include> </includes> </fileset> </filesets> </configuration> </plugin>
package ru.habrahabr.sandello ; import java.io.IOException; import java.io.InputStream; import java.util.jar.Attributes; import java.util.jar.Manifest; public class Version { public static void main(String[] args) { new Version().ver(); } private void ver() { final InputStream mfStream = getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"); Manifest mf = new Manifest(); try { mf.read(mfStream); } catch (IOException e) { e.printStackTrace(); } Attributes atts = mf.getMainAttributes(); System.out.println("version: " + atts.getValue(Attributes.Name.IMPLEMENTATION_VERSION)); } }
mvn package.
java -jar my.jar
Source: https://habr.com/ru/post/119466/