我有一个Maven2项目,需要在一个属性文件中添加当前版本和当前日期。
对于当前版本,我已经使用了${project.version}
,它可以正常工作。
我的问题是如何设置当前日期(即使用Maven2构建时的日期)到我的属性文件中:
client.version=Version ${project.version}
client.build=???
此外,如果我能指定日期的格式,那将非常好。
该功能不支持使用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是新属性,可以被过滤到资源中。
您可以使用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}中获取。<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自从Maven 2.1 M1版本以来,您现在可以使用${maven.build.timestamp}
,前提是您还定义了${maven.build.timestamp.format}
<properties>
...
<maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
...
</properties>
<maven.build.timestamp.format>
。(我自己曾经被误导以为裸的${maven.build.timestamp}
无法工作,但那时我只是在使用Eclipse/m2e/WTP进行测试。一旦我运行类似于mvn compile
这样的命令,${maven.build.timestamp}
就可以正常更新。) - ArjanThomas 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内部或外部构建时,时间戳将被正确生成,并与资源过滤一起使用!
可惜如此简单的功能必须如此困难...
另一种解决方案是在 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}
这对我有用。我只想要时间戳。
在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>
${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>
这在Maven 2.1.0版本中对我有效。
${maven.build.timestamp}