Core Java

Exporting the Maven Version Number to a File

When deploying applications, tracking the correct version is essential for debugging, rollback strategies, and ensuring compatibility with other services. In Maven-based Java projects, the version number is defined in the pom.xml file, but storing it in a separate file is often helpful for easy access. Whether for logging, deployment automation, or integration with external systems, writing the project version to a text file ensures consistency across different environments. This article will explore multiple ways to extract and output the Maven project version to a text file.

1. Project Setup

Let’s assume a basic Maven project with the following coordinates:

1
2
3
4
<groupId>com.jcg</groupId>
<artifactId>version-file-writer</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

Our goal is to extract this 1.0.0 value and write it to a version.txt file as part of the build process.

2. Using the Maven Resources Plugin

One of the simplest ways to achieve this is by using the Maven Resources Plugin, which allows filtering and copying resources during the build process. This method writes the version number into a resource file that becomes part of the final JAR or WAR.

Step 1: Create a Version Template

In your src/main/resources directory, create a file named version.txt with the following content:

1
Version: ${project.version}

Step 2: Enable Resource Filtering

Update your pom.xml to enable filtering for resources and copy the version template:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
     
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>     
</build>

This instructs Maven to replace placeholders like ${project.version} with their actual values during the resource processing phase.

Verifying the Output

After running:

1
mvn package

You can verify the output with:

1
cat target/classes/version.txt

Expected output:

1
Version: 1.0.0

This confirms that Maven successfully filtered and embedded the version number into your compiled resources.

3. Using the Maven Antrun Plugin

This approach is ideal when we want to create a standalone version file (e.g., for use in CI/CD pipelines). To begin, add the Maven Antrun Plugin configuration to your pom.xml file within the <build> section. This plugin will allow you to execute a custom task during the build process to write the project version number to a text file.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>write-version</id>
                    <phase>prepare-package</phase>
                    <configuration>
                        <target>
                            <echo file="${project.build.directory}/version.txt">
                               Version: ${project.version}
                            </echo>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In this configuration, the core functionality lies in the <echo> task inside the <target> block. It instructs Maven to write the project’s version number—extracted from ${project.version}—into a file named version.txt located in the target directory. This execution is bound to the prepare-package phase, which ensures the file is created just before the packaging step, making it available alongside the compiled artifacts.

Verifying the Output (Using Maven Antrun Plugin)

After running:

1
mvn package

Check the output with:

1
cat target/version.txt

Expected output:

1
Version: 1.0.0

This confirms that Maven has written the version number directly to the file during during the prepare-package phase. The prepare-package phase occurs right before the package phase in the Maven build lifecycle. It’s the ideal point to perform tasks like generating version files, since all compilation and resource processing are typically completed by then, and we can safely output metadata without interfering with the actual packaging process.

To adjust the file location in the Maven Antrun Plugin, we can simply modify the file attribute in the <echo> task. By default, it uses ${project.build.directory}/version.txt, which places the file in the target directory. However, we can specify any path relative to the project root or even an absolute path.

For example, to place the version file in a custom build-info directory at the project root:

1
2
3
<echo file="build-info/version.txt">
    ${project.version}
</echo>

Or to store it in a subdirectory inside target, such as target/meta/version.txt:

1
2
3
<echo file="${project.build.directory}/meta/version.txt">
    ${project.version}
</echo>

If the target directory doesn’t already exist, Ant will automatically create it when writing the file. This flexibility allows us to organize our version metadata wherever it best fits our build.

4. Conclusion

In this article, we explored how to output the Maven project version number to a text file during the build process. Leveraging either the Maven Resources Plugin or the Maven Antrun Plugin, we can automate version tracking and make version metadata easily accessible. The Resources Plugin is ideal for embedding version information into packaged applications, while the Antrun Plugin provides a straightforward way to write the version number to an external file for use in deployment pipelines or runtime inspection.

Both methods are easy to use but very effective, and they can greatly improve the clarity and tracking of builds in Maven projects.

5. Download the Source Code

This article covered how to output the version number to a text file using Maven.

Download
You can download the full source code of this example here: maven output version number text file
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button