⬆️ ⬇️

Storing and displaying a version in a java project

I think many have had the task of knowing the exact version of the jar or war released. And I would like to have a way to allow the “simple user” to determine the version of such an archive. Below is a way to solve this problem.



The method does not claim exclusivity, for sure there are others.

So, the statement of the problem:



The project is going maven'om. Sources are stored in git. The programmer works on Linux (for Windows it will not be difficult to adapt). Basically, there are enough tools. It remains only to put everything together.



The action algorithm is as follows:

  1. using the script, we pull out the information from git and write it to a file
  2. read properties from file
  3. write version information to manifest.mf when creating an archive
  4. in manifest.mf specify the Version class
  5. in the Version class we create the main method, in which we pull out the information from manifest.mf




git




To get information from git, we use a separate program, createrepo.sh. You can simply put it in the root of the project.

#!/bin/sh branch=`git branch|grep \*|cut -d" " -f 2` commit=`git id` echo "buildNumber=${branch}.${commit}" > "$1" 


')

maven




Now we set up our pom.xml

You need to define a property that will store the name of the temporary file for the version from git.

 <properties> <versionfile>${project.basedir}/version.properties</versionfile> </properties> 




running the script

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




read version from file

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




recording information in jar

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




delete temporary file during cleaning

 <plugin> <artifactId>maven-clean-plugin</artifactId> <configuration> <filesets> <fileset> <directory>${project.basedir}</directory> <includes> <include>${versionfile}</include> </includes> </fileset> </filesets> </configuration> </plugin> 




java




Create a Version class that will display the archive version

 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)); } } 


All is ready. We collect the project team

mvn package.



As a result, we have an archive that can be run.

java -jar my.jar





The described method, of course, is not without flaws. For example, if the target program should be launched by the java -jar method, then the method will not work. We'll have to modify it: do processing arguments or something else like that. Or, the target program is a set of files that are laid out in Jboss or tomcat. Also have to modify the solution described.

Source: https://habr.com/ru/post/119466/



All Articles