Search code examples
javamavenmaven-assembly-plugin

How can I include a build number in the JAR file name?


I have a project, which is built using Maven assembly plugin.

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

I have a file BUILD.txt, which contains the current build number.

When I call mvn assembly:single, I want maven-assembly-plugin to generate a JAR file (with all dependencies) called myproduct-1.0-SNAPSHOT.BUILD.jar, where BUILD is the text from BUILD.txt (i. e. if BUILD.txt contains 172, the result JAR should be called myproduct-1.0-SNAPSHOT.172.jar).

How can I read the text from BUILD.txt such that I can use it in the finalName setting of the assembly plugin?

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <finalName>${project.build.finalName}.${build}.jar</finalName
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

Solution

  • Approach 1

    • Use Maven Properties Plugin
    • Change your BUILD.txt to contain buildNumber=NNN
    • Use property ${buildNumber} for <finalName>

    Approach 2

    • Assumption You use an SCM like SVN or Git
    • No Need to use BUILD.txt anymore
    • Use Maven Build Number Plugin
    • Use property ${buildNumber} for <finalName>