• 2.1.0

Static Reader of META-INF/MANIFEST.MF Files

com.jcabi.manifests.Manifests is a convenient static reader of META-INF/MANIFEST.MF files. Try this:

import com.jcabi.manifests.Manifests;
public class Main {
  public static void main(String[] args) {
    String version = Manifests.read("JCabi-Version");
    System.out.println("version is " + version);
  }
}

As Oracle documentation says the META-INF/MANIFEST.MF is a special file that can contain information about the files packaged in a JAR file. Every JAR file in your classpath contains its own manifest. Every manifest consists of attributes and values. For example, this is META-INF/MANIFEST.MF of junit-4.10.jar:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_26-b03-384-10M3425 (Apple Inc.)

This manifest was generated by the author of JUnit JAR package and contains three attributes Manifest-Version, Ant-Version, and Created-By. When a JAR package is available in the classpath your application can retrieve these attributes from its manifest. Manifests utility class is a convenient reader of all attributes from all manifest files available in the classpath. All you need to provide is a name of the desired attribute.

MANIFEST.MF files may become an effective alternative to .properties files when Manifests utility class is used. You don't need to worry about finding the right location of the right .properties, loading it into memory, and dealing with exceptions. All you do is reading from Manifests:

public class Main {
  public static void main(String[] args) {
    System.out.println("JAR was created by " + Manifests.read("Created-By"));
  }
}

To put information into your manifest maven-jar-plugin or maven-war-plugin can be used, for example:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <archive>
      <manifestEntries>
        <Foo-Version>${project.version}</Foo-Version>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

After packaging of the WAR artifact its MANIFEST.MF file will contain this attribute (among some others):

Foo-Version: 1.0-SNAPSHOT

Read more about using MANIFEST.MF file for versioning of your Java application. Also pay attention to the difference between JAR and WAR applications. Besides that, there is a number of best practices related to unit testing and mocking of MANIFEST.MF attributes.

The only dependency you need in your class path is (you can also download jcabi-manifests-2.1.0.jar and add it to the classpath):

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-manifests</artifactId>
  <version>2.1.0</version>
</dependency>

If you are outside of WAR application you should add Servlet API to compile-time classpath:

<dependency>
  <groupId>jakarta.servlet</groupId>
  <artifactId>jakarta.servlet-api</artifactId>
  <version>5.0.0</version>
  <scope>provided</scope>
</dependency>

Cutting Edge Version

If you want to use current version of the product, you can do it with this configuration in your pom.xml:

<repositories>
  <repository>
    <id>oss.sonatype.org</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>com.jcabi</groupId>
    <artifactId>jcabi-manifests</artifactId>
    <version>2.1.0-SNAPSHOT</version>
  </dependency>
</dependencies>