lili
lili

Reputation: 1906

How to rename latest snapshot version to include a version number

Question: How can I rename the snapshot version of file to include the version number?

I use <version>LATEST</version> to download the latest version, but how can I use it in the <destFileName>?

${project.dependencies[0].version} gives me version LATEST.

<artifactItem>
    <groupId>my.group</groupId>
    <artifactId>my.artifact</artifactId>
    <version>LATEST</version>
    <type>exe</type>
    <overWrite>true</overWrite
    <outputDirectory>target/downloads</outputDirectory>
    <destFileName>${project.dependencies[0].version}_My_File_Name.exe</destFileName>
</artifactItem>

I'm using Snapshot version and I want to change the file name to include only the version number. (otherwise the file name is My_File_Name-version-20160630.212007-10).

Upvotes: 4

Views: 2418

Answers (1)

Tunaki
Tunaki

Reputation: 137309

You need to set useBaseVersion to true in the configuration of the maven-dependency-plugin:

<configuration>
    <artifactItems>
        <artifactItem>
            <groupId>my.group</groupId>
            <artifactId>my.artifact</artifactId>
            <version>LATEST</version>
            <type>exe</type>
            <overWrite>true</overWrite>
            <outputDirectory>target/downloads</outputDirectory>
        </artifactItem>
    </artifactItems>
    <useBaseVersion>true</useBaseVersion>
</configuration>

This parameter was introduced in version 2.7 of the plugin.


A bit of explanation, using as example the artifact org.springframework.batch:spring-batch-admin-manager. When you are using "LATEST" as the version inside a dependency, Maven will fetch a file from the configured remote repositories called maven-metadata.xml. This file contains the information of all the versions that are deployed for the groupId:artifactId of the dependency.

This is an example of such a file

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
  <groupId>org.springframework.batch</groupId>
  <artifactId>spring-batch-admin-manager</artifactId>
  <versioning>
    <latest>1.3.2.BUILD-SNAPSHOT</latest> <!-- This is the LATEST to use! -->
    <release></release>
    <versions>
      <version>1.3.1.BUILD-SNAPSHOT</version>
      <version>1.3.2.BUILD-SNAPSHOT</version>
    </versions>
    <lastUpdated>20150122163642</lastUpdated>
  </versioning>
</metadata>

Among others, it declares what is the latest version inside the <latest> element. In this case, the latest version would be 1.3.2.BUILD-SNAPSHOT.

However, this is a SNAPSHOT version, which means that there can actually be multiple snapshot versions for the same 1.3.2.BUILD-SNAPSHOT. They are differentiated in Maven 3 by adding a timestamp at the end of the filename. Therefore, you can have multiple 1.3.2-SNAPSHOT with different timestamps.

Now that Maven knows that 1.3.2.BUILD-SNAPSHOT is the one to be considered, it looks for another maven-metadata.xml that is specific to 1.3.2.BUILD-SNAPSHOT, which is:

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
  <groupId>org.springframework.batch</groupId>
  <artifactId>spring-batch-admin-manager</artifactId>
  <version>1.3.2.BUILD-SNAPSHOT</version>
  <versioning>
    <snapshot>
      <timestamp>20150115.230511</timestamp>  <!-- This is the timestamp to use! -->
      <buildNumber>1</buildNumber>
    </snapshot>
    <lastUpdated>20150122163642</lastUpdated>
    <snapshotVersions>
      <snapshotVersion>
        <extension>jar</extension>
        <value>1.3.2.BUILD-20150115.230511-1</value>
        <updated>20150115230511</updated>
      </snapshotVersion>
      <!-- omitted for brevity -->
  </versioning>
</metadata>

This file notably declares the latest timestamp of the deployed snapshot version, inside the <timestamp> element. Maven will use and download this timestamped artifact.

So finally:

  • The base version will correspond to the SNAPSHOT version of the artifact, 1.3.2.BUILD-SNAPSHOT- in this example;
  • The version will correspond to the timestamped version of the latest snapshot, 1.3.2.BUILD-20150115.230511-1 in this example.

As such, useBaseVersion will allow to output a file without the timestamp. When dealing with a release version instead of a snapshot version, both will be the same.

Upvotes: 4

Related Questions