使用Maven2过滤添加当前日期

Rom*_*las 50 maven-2 filtering

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

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

我的问题是如何在我的属性文件中设置当前日期(即Maven2完成构建的日期):

client.version=Version ${project.version}
client.build=???
Run Code Online (Sandbox Code Playgroud)

(另外,如果我可以指定日期的格式,那将非常棒)

小智 73

功能不适用于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>
Run Code Online (Sandbox Code Playgroud)

其中buildNumber是可以过滤到资源的新属性.

  • 可爱的开源论证.但我并没有抱怨.这是事实;)! (4认同)
  • 请注意,jenkins中的错误阻止您使用此功能(仅适用于jenkins案例)https://issues.jenkins-ci.org/browse/JENKINS-9693 (3认同)
  • +1比http://maven.apache.org/plugin-developers/cookbook/add-build-time-to-manifest.html BTW好得多,你的代码也可以在<project>中的同一个pom.xml里面properties> ... </ properties> </ project>. (2认同)
  • @Karussell,您可以自由地为该项目提供修复,这比投诉那些将时间用于项目的人太慢而无法解决对您来说重要的问题更有效率. (2认同)

Tho*_*rti 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>
Run Code Online (Sandbox Code Playgroud)

然后可以在属性$ {buildNumber}中找到该日期.

  • 我收到错误: org.codehaus.mojo:buildnumber-maven-plugin:1.3:create failed: The scm url can not be null (2认同)

abe*_*t80 22

从Maven 2.1 M1开始,您现在可以${maven.build.timestamp}为您提供定义${maven.build.timestamp.format}

<properties>
    ...
    <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
    ...
</properties>
Run Code Online (Sandbox Code Playgroud)

  • 注意:这在过滤资源文件时不起作用(截至目前) (7认同)

Gar*_*son 12

Thomas Marti的答案是向正确的方向迈出的一步,但是有一种更简单的方法,不需要<scm>在POM中进行虚拟声明.使用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>
Run Code Online (Sandbox Code Playgroud)

开箱即用,这在使用m2e的Eclipse中不起作用,因此您必须在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>
Run Code Online (Sandbox Code Playgroud)

这告诉m2e你希望它在Eclipse中构建时继续运行插件.

现在,当您在Eclipse内部或外部构建时,正确生成时间戳并使用资源过滤!

令人遗憾的是,功能如此简单必须如此艰难......


Rom*_*las 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>
Run Code Online (Sandbox Code Playgroud)

然后使用该buildtimestamp属性:

client.version=${pom.version}
client.build=${buildtimestamp}
Run Code Online (Sandbox Code Playgroud)


小智 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>
Run Code Online (Sandbox Code Playgroud)

并在JSP文件中......

<div>Built: ${dev.build.timestamp}</div>
Run Code Online (Sandbox Code Playgroud)

示例结果是......

<div>Built: 20130419-0835</div>
Run Code Online (Sandbox Code Playgroud)


Ama*_*icA 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>
Run Code Online (Sandbox Code Playgroud)

另请参阅buildnumber-maven-plugin文档.


(其他答案让我特别关注Garret Wilson,但他的日食配置对我来说不是必需的,这让我忽略了他的答案,所以我发布了对我有用的东西.)

如果你想获得一个日期和一个时间的两个属性作为奖励,这就是你如何做到这一点:

<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>
Run Code Online (Sandbox Code Playgroud)


小智 5

它在maven 2.1.0对我有用

$ {} maven.build.timestamp