如何重命名最新的快照版本以包含版本号

How to rename latest snapshot version to include a version number

问题:如何重命名文件的快照版本以包含版本号?

我用<version>LATEST</version>下载了最新版本,但是在<destFileName>中如何使用呢?

${project.dependencies[0].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>

我正在使用快照版本,我想更改文件名以仅包含版本号。 (否则文件名为 My_File_Name-version-20160630.212007-10)。

您需要在maven-dependency-plugin的配置中将useBaseVersion设置为true:

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

此参数是在插件的 2.7 版本中引入的。


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。此文件包含为依赖项 groupId:artifactId 部署的所有版本的信息。

这是此类文件的示例

<?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>

其中,它声明了 <latest> 元素中的最新版本。在这种情况下,最新版本将为 1.3.2.BUILD-SNAPSHOT

不过,这是一个SNAPSHOT版本,这意味着同一个1.3.2.BUILD-SNAPSHOT实际上可以有多个快照版本。它们在文件名末尾的 Maven 3 by adding a timestamp 中有所区别。因此,您可以有多个具有不同时间戳的 1.3.2-SNAPSHOT

现在 Maven 知道 1.3.2.BUILD-SNAPSHOT 是要考虑的那个,它会寻找另一个 1.3.2.BUILD-SNAPSHOT 特定的 maven-metadata.xml,即:

<?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>

此文件在 <timestamp> 元素内特别声明了已部署快照版本的最新时间戳。 Maven 将使用并下载这个带时间戳的工件。

所以最后:

  • 基本版本将对应工件的 SNAPSHOT 版本,1.3.2.BUILD-SNAPSHOT- 在本例中;
  • 该版本将对应于最新快照的时间戳版本,在此示例中为 1.3.2.BUILD-20150115.230511-1

因此,useBaseVersion 将允许输出没有时间戳的文件。当处理发布版本而不是快照版本时,两者将是相同的。