我试图找出 Maven 是否有一些可用于为 Artifact 添加时间戳的内置插件。我创建了一个程序集文件,并使用 maven-assembly 插件来创建最终分发版(jars、docs、scripts 等)。我想将此分发文件命名为 domain_year_month_day.zip。如何将时间戳的日期部分附加到正在生成的最终 zip 文件的末尾。谢谢。
最佳答案
您可以使用 maven-timestamp-plugin设置属性(例如 timestamp
)并稍后在程序集的最终名称中使用它。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>domain_${timestamp}</finalName>
<descriptors>
<descriptor>src/main/assembly/my-descriptor.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
作为替代方案,您可以使用 GMaven plugin 在 POM 中放入一些 Groovy 代码。 :
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>set-custom-property</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def timestamp = new Date().format('MM_dd_yy')
project.properties.setProperty('timestamp', timestamp)
</source>
</configuration>
</execution>
<execution><!-- for demonstration purpose -->
<id>show-custom-property</id>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
println project.properties['timestamp']
</source>
</configuration>
</execution>
</executions>
</plugin>
显示属性的示例输出:
$ mvn 生成资源
[信息] 正在扫描项目...
[信息]
...
[信息] --- gmaven-plugin:1.3:execute (set-custom-property) @ Q4081274 ---
[信息]
[信息] --- gmaven-plugin:1.3:execute (show-custom-property) @ Q4081274 ---
11_02_10
[信息] ----------------------------------------------- -------------------------
[信息] 构建成功
[信息] ----------------------------------------------- -------------------------
...
同样,稍后在程序集的构建名称中使用此属性。
关于maven-2 - 如何在 Maven 中为输出 Artifact 加时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4081274/