Gaspar Zanini
Gaspar Zanini

Reputation: 81

How can I add file version detail on properties to a .jar?

I'm looking to add a product version number to appear in my .jar file information. Currently I'm using Maven in my Spring boot project for API Rest.

I have read a lot of solutions about the manifest versioning. There you have to decompress and access to the META-INF/MANIFEST.MF to check the Implementation-Version. That's too tedious for what I'm looking for.

Like for a .exe. where you can found it under right mouse click -> details -> "product version" or simply checking on File Version column as shown on image. Example of a file version description.

Also I read that JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. Kinda that I'm looking to add a file version to .zip, but I want to ask anyway if that is possible.

Regards, Gaspar.

Upvotes: 1

Views: 1040

Answers (3)

Stuboy
Stuboy

Reputation: 43

If you don't necessarily need the exact version, a mnemonic strategy I've seen is to touch the file timestamp. It may not be suitable and/or you'll have to determine what to do for numbers that fall outside the scope of a timestamp.

eg Use Powershell to set the LastWriteTime (or CreationTime) to match v2.3.4 -> 02:03:04

$ff = Get-Item my.jar
$ff.LastWriteTime         # Monday, 24 April 2023 16:25:56
$ff.LastWriteTime = $(Get-Date "24-apr-2023 02:03:04")
$ff.LastWriteTime         # Monday, 24 April 2023 02:03:04

LastWriteTime will show up in the "Date Modified" column of Windows Explorer.

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 102978

Just include a text file anywhere in your jar. Build systems of all stripes make this trivial. Then write in your code that you read out this text file with the version, and show it on screen.

In maven, gradle, etc, to include a plain text file in the jar, just put it in src/main/resources/version.txt and it gets included automatically.

To read it in java:

public class Main {
  public static String getVersion() {
    try (var in = Main.class.getResourceAsStream("/version.txt")) {
      return new String(in.readAllBytes(), StandardCharsets.UTF_8);
    }
  }
}

This:

  • Asks the classloader to load version.txt using the same systems that load .class files.
  • reads that inputstream fully, and turns it into a string using the UTF_8 encoding.
  • Uses try-with-resources because, it's a resource, you have to do that if you don't want leaks.

Upvotes: 0

Rob Spoor
Rob Spoor

Reputation: 9110

A JAR file itself can't have a version. But you're using Maven, and that means you can already access the Maven version:

try (InputStream inputStream = getClass().getResourceAsStream("/META-INF/maven/<groupId>/<artifactId>/pom.properties")) {
    Properties properties = new Properties();
    properties.load(inputStream);
    // available properties:
    // - artifactId=xxx
    // - groupId=xxx
    // - version=xxx
}

Note that this often doesn't work in unit tests (especially when run from IDEs) because the files are only added to the JAR file.

Upvotes: 1

Related Questions