Explain Codes LogoExplain Codes Logo

Get Maven artifact version at runtime

java
maven
version
packaging
Alex KataevbyAlex Kataev·Jan 19, 2025
TLDR

To fetch the Maven artifact version at runtime in a jiffy, load pom.properties from META-INF/maven/<groupId>/<artifactId>/ utilizing Java's Properties class:

Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream( "META-INF/maven/com.yourgroup/yourartifact/pom.properties")); // Fetch version, not Harry Potter's broomstick move! String version = prop.getProperty("version"); System.out.println(version);

Update com.yourgroup and yourartifact with your groupId and artifactId to get the version directly.

Superior implementation version usage

In addition to pom.properties, you might need access to the implementation version, stored in a JAR's MANIFEST.MF. Achieve this:

  1. Set the addDefaultImplementationEntries as true in the maven-jar-plugin of your pom.xml:
    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <!-- Platform 9 3/2.0, the Maven Hogwarts special --> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin>
  2. Utilize Package class to retrieve the version:
    Package pkg = MyClass.class.getPackage(); // Abracadabra! Here's your version! String version = pkg.getImplementationVersion(); System.out.println(version);
    Replace MyClass with your class reference from your code.

Valuable fallback mechanisms and considerations

Provide fallbacks for scenarios when the version information is not accessible or available. This could happen in a development environment, where your code isn't packaged in a JAR, or is included through a class folder:

String version = MyClass.class.getPackage().getImplementationVersion(); if (version == null) { // No, "n/a" wasn't my favorite grade in school. It means no version data. Let's set a default. version = "1.0.0-UNKNOWN"; } // Here we go with our version

For multithreaded applications, consider thread safety when accessing these properties.

Taking Maven Archiver for packaging a joyride

The Apache Maven Archiver can assist in streamlining your application's packaging process. Ensuring uniform handling of versioning, it works well with maven-assembly-plugin for packaging artifacts with special requirements.

Tackling different package types, like war

For war packages, the process echoes the one for jars. As in the first method, configure maven-war-plugin in pom.xml to include version details in the MANIFEST.MF file:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin>

Extracting Spring Boot artifact version

For Spring Boot applications, you can harness BuildProperties to access the artifact version:

@Autowired BuildProperties buildProperties; public void printVersion() { // Let's spill the beans, or in this case, the version! System.out.println(buildProperties.getVersion()); }

You'll need to configure Spring Boot Maven Plugin for this method to generate build information.

Surpassing versioning standards in a Spring ecosystem

You can utilize Spring Boot Actuator to expose a /info endpoint that displays application version. Enable it in your application.properties:

management.info.build.enabled=true

Combining Lombok with Spring Boot makes code for injecting properties more simplified.

Identifying and avoiding Maven build pitfalls

Watch out for plugin conflicts during Maven build configuration, as the interaction of certain plugins may affect packaging. Lean on maven shared components to manage plugins effectively.

Enjoying dynamic configurations with OpenAPI and Spring Boot

For web applications, OpenAPI coupled with Spring Boot allows the dynamic inclusion of the version in API configuration files. This improves discoverability and documentation of your API, making version info easily accessible to all API consumers.