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.
Then enable Maven resource filtering in pom.xml.
At build time, Maven replaces ${project.version} with the real version. Read it from code:
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.
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
--versionoutput - 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.xmlis present in production at the same relative path as in the source tree. - Using manifest version access and being surprised when it returns
nullduring 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.
Related reading
- Retrieving a List from a java.util.stream.Stream in Java 8
- Retrieving number of unacknowledged messages in RabbitMQ queue from Java/ Spring
- Retrieving the inherited attribute names/values using Java Reflection
- Retrofit 2.0 how to get deserialised error response.body
- Retroactively Correct Authors with Git SVN?
- Revert to a commit by a SHA hash in Git?
- Retry policy in CompletionService
- Return collection as read-only

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.