我正在尝试找出Maven是否具有一些可用于时标记的内置插件。我创建了一个汇编文件,并正在使用Maven-Asembly插件来创建最终分发(JARS,DOCS,脚本等)。我想将此分发文件命名为domain_year_month_day.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>

作为替代方案,您可以使用The Pom将一些Groovy代码放在 Gmaven插件:

<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 generate-resources 
[INFO] Scanning for projects...
[INFO]                                                                         
...
[INFO] --- gmaven-plugin:1.3:execute (set-custom-property) @ Q4081274 ---
[INFO] 
[INFO] --- gmaven-plugin:1.3:execute (show-custom-property) @ Q4081274 ---
11_02_10
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

再一次,以后在汇编的构建名称中使用此属性。

其他提示

您不需要带有新版本的Maven的Maven-Timestamp-Plugin。自2.1'ish以来,Maven提供了特殊物业Maven.build.timestamp。

您将POM属性中的格式设置为以下内容:

<maven.build.timestamp.format>yyyy-MM-dd'T'HH.mm.ss</maven.build.timestamp.format>

然后在需要时间戳属性的任何地方使用$ {maven.build.timestamp}。看 http://maven.apache.org/guides/introduction/introduction-to-te-the-pom.html 有关详细信息。

作为 ${maven.build.timestamp} 在马文(Maven)中似乎有越野车,解决方法如下:

创建一个新变量(我选择了“ build.timestamp”,在这里) - 并且,可选地指定格式:

pom.xml

<project>
    ...
    <properties>
        ...
        <build.timestamp>${maven.build.timestamp}</build.timestamp>
        <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format> 
                     <!-- default is: yyyyMMdd-HHmm -->
    </properties>
    <build>
    ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>some-assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

...

从任何地方使用自定义变量:

some-emembly.xml

<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>release-${build.timestamp}</id>
    <baseDirectory>/</baseDirectory>
    <includeBaseDirectory>false</includeBaseDirectory>
    <formats>
      <format>zip</format>
    </formats>
    <fileSets>
      <fileSet>
        <directory>${project.build.directory}/${project.artifactId}-${project.version}</directory>
      </fileSet>
    </fileSets>
</assembly>

如果您使用Hudson/Jenkins,则可以使用变量$ {build_id}将时间戳到您要编辑的任何属性文件中。

信息到其他环境变量哈德逊/詹金斯支持的信息,请在此处查看:http://wiki.hudson-ci.org/display/hudson/building+a+software+Project

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top