Glory to Russia
Glory to Russia

Reputation: 18712

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>

Upvotes: 2

Views: 2528

Answers (4)

Tunaki
Tunaki

Reputation: 137064

If you have no control over the creation of this build file and can't change its content, a way to do that would be to read the file using an Ant task with maven-antrun-plugin, store the content inside a property and then use that property for the final name.

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.8</version>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <configuration>
        <target>
          <!-- this task will read the content of the file and store it inside the "build" property -->
          <loadfile property="build" srcFile="BUILD.txt" />
        </target>
        <exportAntProperties>true</exportAntProperties> <!-- Ant properties are not exported as Maven properties by default so we need to add it -->
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <finalName>${project.build.finalName}.${build}.jar</finalName
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <appendAssemblyId>false</appendAssemblyId>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase> <!-- bind the maven-assembly-plugin to the package phase instead -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Running Maven with mvn clean install will generate the correct name for the assembly.

Note however that instead of doing this, you could use the buildnumber-maven-plugin which already deals automatically with build number, freeing you with the burden of maintaining a BUILD.txt file.

Upvotes: 1

francesco foresti
francesco foresti

Reputation: 2043

You can do it with this :

<build>
  <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        ...

put it in your pom (build section), then configure it to access your scm. Then define your bundleFileName, e.g. like this

<bundleFileName>${project.artifactId}-${project.version}.jar</bundleFileName>

Upvotes: 1

Francisco Hernandez
Francisco Hernandez

Reputation: 2468

try this

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

With appendAssemblyId you will avoid the jar-with-dependencies suffix. You should generate the BUILD.txt as a properties files and then access to it as a normal properties file in pom.xml

Upvotes: 1

nobeh
nobeh

Reputation: 10039

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>

Upvotes: 2

Related Questions