我有一个 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} 中可用。
从 Maven 2.1 M1 开始,你现在可以做,${maven.build.timestamp}
只要你还定义${maven.build.timestamp.format}
<properties>
...
<maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
...
</properties>
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>
开箱即用,这在带有 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>
这告诉 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}
这对我有用。我想要的只是时间戳。
在聚...
<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}