4

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).

Tunaki
  • 132,869
  • 46
  • 340
  • 423
lili
  • 1,866
  • 8
  • 29
  • 50
  • thank you. 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) – lili Jul 05 '16 at 13:38

1 Answers1

4

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.

Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I wish I could vote more than once for answers like this one! – A_Di-Matteo Jul 05 '16 at 17:30
  • Thanks @A_Di-Matteo :)! – Tunaki Jul 05 '16 at 17:31
  • thanks for the detailed answer! then I guess I'll have to use antrun to bring the file name from "My_File_Name-version-snapshot.exe" to "version-My_File_Name.exe"? – lili Jul 05 '16 at 18:18
  • 1
    @lili Yeah, I've looked at the source code and you can't seem to use placeholders in the ``: either you provide it (so fixed) or it is the default `artifactId-version.type`. Didn't find an enhancement request asking for that neither so that could potentially be asked; however removing the `-SNAPSHOT` from the version is not really something that should be done: it would be best to keep the "real" version (in the example of this answer, you would need to truncate `.BUILD-SNAPSHOT`... versions are not all generic, following the `X.Y.Z-SNAPSHOT` pattern). – Tunaki Jul 05 '16 at 18:25