Maven
POM.xml
Version Control
Coding
Java

Get the Maven project version from pom.xml in Java code

Codemia Knowledge Hub ·

Don't parse pom.xml at runtime. Let Maven inject the project version during the build, either into a filtered resource file or into the JAR manifest, and read that from your code. A running application cannot see pom.xml unless the file is packaged or its contents are copied into some runtime-visible resource.

Why not read pom.xml directly

pom.xml is a build-time artifact. It may not exist beside the deployed JAR, and even if it does, relying on its filesystem location is brittle. Parsing it couples runtime code to assumptions about the build layout and forces the deployed app to carry the build file somewhere accessible.

The practical approaches are:

  • generate a properties resource during the build
  • expose version info in the manifest
  • read that generated runtime artifact from code

Both turn the build-time version into an explicit runtime contract.

Option 1: filter a resource file

Create a resource such as version.properties.

properties
app.version=${project.version}

Then enable Maven resource filtering in pom.xml.

xml
1<build>
2  <resources>
3    <resource>
4      <directory>src/main/resources</directory>
5      <filtering>true</filtering>
6    </resource>
7  </resources>
8</build>

At build time, Maven replaces ${project.version} with the real version. Read it from code:

java
1import java.io.IOException;
2import java.io.InputStream;
3import java.util.Properties;
4
5public final class VersionInfo {
6    public static String getVersion() {
7        Properties props = new Properties();
8        try (InputStream in = VersionInfo.class.getResourceAsStream("/version.properties")) {
9            if (in == null) {
10                return "unknown";
11            }
12            props.load(in);
13            return props.getProperty("app.version", "unknown");
14        } catch (IOException ex) {
15            return "unknown";
16        }
17    }
18}

This is a simple, reliable way to surface the Maven version at runtime.

Option 2: read the JAR manifest

You can also write version information into the manifest and read it through the package metadata.

java
String version = VersionInfo.class.getPackage().getImplementationVersion();
System.out.println(version);

For this to work, the build must populate Implementation-Version in the manifest. It works well when the app always runs from a packaged JAR, but it may return null in tests, IDE launches or exploded-class directories unless the build provides the metadata there too. That is why many teams prefer the filtered-resource approach for consistency across execution modes.

Where a runtime version is useful

  • startup logs
  • diagnostics endpoints
  • CLI --version output
  • support and troubleshooting pages

In all of these, consistency matters more than cleverness, and a build-injected value is the right tool.

Mistakes to avoid

  • Assuming pom.xml is present in production at the same relative path as in the source tree.
  • Using manifest version access and being surprised when it returns null during IDE runs.
  • Forgetting to enable resource filtering, which leaves the literal ${project.version} text in the properties file.
  • Duplicating the version by hand in a second config file, which defeats the point of Maven being the single source of truth.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms