使用Maven2过滤功能添加当前日期

51

我有一个Maven2项目,需要在一个属性文件中添加当前版本和当前日期。

对于当前版本,我已经使用了${project.version},它可以正常工作。

我的问题是如何设置当前日期(即使用Maven2构建时的日期)到我的属性文件中:

client.version=Version ${project.version}
client.build=???

此外,如果我能指定日期的格式,那将非常好。

8个回答

83

该功能不支持使用maven 2.2.1资源过滤。

请参阅:https://issues.apache.org/jira/browse/MRESOURCES-99

但是,您可以在父POM中创建自定义属性:

<properties>
    <maven.build.timestamp.format>yyMMdd_HHmm</maven.build.timestamp.format>
    <buildNumber>${maven.build.timestamp}</buildNumber>
</properties>

buildNumber是新属性,可以被过滤到资源中。


3
比起http://maven.apache.org/plugin-developers/cookbook/add-build-time-to-manifest.html,+1更好。顺便提一句,您的代码也可以放在同一个pom.xml文件中的<project><properties> ... </properties></project>标签内。 - Cojones
3
请注意,Jenkins的一个错误正在阻止您使用此功能(仅适用于Jenkins情况),https://issues.jenkins-ci.org/browse/JENKINS-9693。 - Rudy
这个Maven问题已经开放了3年,仍然没有解决! - Karussell
2
@Karussell,你可以自由地为这个项目做出修复的贡献,这比抱怨那些为项目捐赠时间的人太慢来处理你认为重要的问题更有建设性。 - Paul
4
可爱的开源争论。但我没有抱怨。这是事实 ;)! - Karussell

49

您可以使用Maven Buildnumber插件来实现:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>buildnumber-maven-plugin</artifactId>
      <executions>
        <execution>
          <phase>initialize</phase>
          <goals>
            <goal>create</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <doCheck>false</doCheck>
        <doUpdate>false</doUpdate>
        <timestampFormat>{0,date,yyyy-MM-dd HH:mm:ss}</timestampFormat>
      </configuration>
    </plugin>
  </plugins>
</build>
日期现在可以在属性${buildNumber}中获取。

并将当前日期添加到属性中: ${timestamp}。此外,我不得不添加一个虚拟的<scm>。 - jpprade
抱歉我的编辑:看起来<timestampFormat>被这个目标用于格式化${buildNumber}...非常令人困惑。对于那些想要构建号和构建日期的人,您需要使用两个buildnumber-maven-plugin执行:一个是create目标(以及可选的配置),另一个是create-timestamp目标,其中包含自定义的<timestampFormat>配置。混合配置不起作用。 - dma_k
好的,我感觉有点傻,不查找在线资料的情况下,有人能告诉我为什么我必须使用语法{0,date,yyyy-MM-dd HH:mm:ss}来表示yyyy-MM-dd HH:mm:ss吗?这是什么意思? - reallynice
2
我遇到了一个错误:org.codehaus.mojo:buildnumber-maven-plugin:1.3:create 失败:scm url 不能为空。 - DD.

25

自从Maven 2.1 M1版本以来,您现在可以使用${maven.build.timestamp},前提是您还定义了${maven.build.timestamp.format}

<properties>
    ...
    <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
    ...
</properties>

7
注意:目前还不能用于过滤资源文件。 - matt b
1
我认为现在不需要提供<maven.build.timestamp.format>。(我自己曾经被误导以为裸的${maven.build.timestamp}无法工作,但那时我只是在使用Eclipse/m2e/WTP进行测试。一旦我运行类似于mvn compile这样的命令,${maven.build.timestamp}就可以正常更新。) - Arjan

12

Thomas Marti 的回答是朝着正确方向迈出的一步,但有一个更简单的方法不需要在 POM 中添加虚拟的 <scm> 声明。使用 buildnumber-maven-plugin 插件,但使用 create-timestamp 目标。文档并不清楚,下面是获取 YYYY-MM-DD 格式的日期并将其放入 build.date 属性中的示例:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <timestampFormat>yyyy-MM-dd</timestampFormat>
        <timestampPropertyName>build.date</timestampPropertyName>
    </configuration>
</plugin>

在Eclipse中使用m2e时,这个功能无法直接运行。因此,您需要在POM的 <build> 部分中添加以下内容:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>buildnumber-maven-plugin</artifactId>
                                <versionRange>[1.2,)</versionRange>
                                <goals>
                                    <goal>create-timestamp</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnConfiguration>true</runOnConfiguration>
                                    <runOnIncremental>true</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

这告诉m2e,当在Eclipse内进行构建时,您希望它执行插件。

现在,在Eclipse内部或外部构建时,时间戳将被正确生成,并与资源过滤一起使用!

可惜如此简单的功能必须如此困难...


这是我在RAD/Eclipse中找到的唯一可行的解决方案。谢谢。 - Chris Harris
一开始我忽略了你的回答,因为所有与Eclipse配置相关的繁琐操作对我来说都是不必要的(可能是因为m2e已经改进了,自从你写这篇文章以来)。我发布了一个完全适用于我的解决方案,希望它能帮助像我这样的人 :). - AmanicA

11

另一种解决方案是在 pom.xml 中使用 Groovy(可能不如 Thomas Marti 提出的解决方案那么规范):

   <build>
      <resources>
         <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
         </resource>
      </resources>
      <plugins>
         <plugin>
            <groupId>org.codehaus.groovy.maven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <executions>
               <execution>
                  <phase>validate</phase>
                  <goals>
                     <goal>execute</goal>
                  </goals>
                  <configuration>
                     <source>
                     import java.util.Date 
                     import java.text.MessageFormat 
                     def vartimestamp = MessageFormat.format("{0,date,yyyyMMdd-HH:mm:ss}", new Date()) 
                     project.properties['buildtimestamp'] = vartimestamp
                     </source>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>

然后使用buildtimestamp属性:

client.version=${pom.version}
client.build=${buildtimestamp}

1
谢谢!BuildNumber插件对我来说似乎很糟糕,但这个方法非常好用。 - JavadocMD

9

这对我有用。我只想要时间戳。

在pom文件中...

<properties>
    <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
    <dev.build.timestamp>${maven.build.timestamp}</dev.build.timestamp>
</properties>
...
<overlay>
   <groupId>mystuff</groupId>
   <artifactId>mystuff.web</artifactId>
   <filtered>true</filtered>
</overlay>

在JSP文件中...

<div>Built: ${dev.build.timestamp}</div>

例子的结果是...

<div>Built: 20130419-0835</div>

简单就是美。 - Kin Cheung

7
${build.time}存入一个属性文件中,并将以下内容放入pom.xml
<build>
   <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.3</version>
        <configuration>
          <timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat>
          <timestampPropertyName>build.time</timestampPropertyName>
        </configuration>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>create-timestamp</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
   </plugins>
</build>

此外,还请参阅buildnumber-maven-plugin文档


(其他答案让我接近成功,特别是Garret Wilson的答案,但他的eclipse配置对我来说不必要,导致我忽略了他的答案,所以我将准确地发布适用于我的答案。)

如果您想获得日期和时间两个属性,这就是如何操作:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>buildnumber-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>build.date</id>
      <phase>initialize</phase>
      <goals>
        <goal>create-timestamp</goal>
      </goals>
      <configuration>
        <timestampFormat>yyyy-MM-dd</timestampFormat>
        <timestampPropertyName>build.date</timestampPropertyName>
      </configuration>
    </execution>
    <execution>
      <id>build.time</id>
      <phase>initialize</phase>
      <goals>
        <goal>create-timestamp</goal>
      </goals>
      <configuration>
        <timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat>
        <timestampPropertyName>build.time</timestampPropertyName>
      </configuration>
    </execution>
  </executions>
</plugin>

我不得不使用虚拟的SCM,它救了我的一天。我正在将其用于我的CI工具,现在Jenkins每天都会提供日常和夜间构建,我们可以安装到Nexus,而无需启用覆盖。 - Manoj

5

这在Maven 2.1.0版本中对我有效。

${maven.build.timestamp}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接