我想在我的自定义Maven插件中访问Maven构建时间戳。因此,我尝试使用特殊变量maven.build.timestamp
:
maven.build.timestamp
表示构建开始的时间戳。 自Maven 2.1.0-M1
参数默认值,最终包含将在注入时解释的
${...}
表达式:请参阅PluginParameterExpressionEvaluator。
但我总是得到值null
。我尝试使用Date
类型和String
类型。
Java代码:
@Mojo(name = "test", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class TestMojo extends AbstractMojo {
@Parameter(defaultValue = "${maven.build.timestamp}", readonly = true)
private Date timestampDate;
@Parameter(defaultValue = "${maven.build.timestamp}", readonly = true)
private String timestampString;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().error("timestampDate: " + timestampDate);
getLog().error("timestampString: " + timestampString);
}
}
插件配置:
<plugin>
<groupId>com.mycompany</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.12</version>
</plugin>
日志:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- test-maven-plugin:0.0.12:test (default-cli) @ test ---
[ERROR] timestampDate: null
[ERROR] timestampString: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.799 s
[INFO] Finished at: 2017-04-21T14:37:20+02:00
[INFO] Final Memory: 8M/223M
[INFO] ------------------------------------------------------------------------
环境:
为什么我的自定义Maven插件中没有解决特殊变量?
答案 0 :(得分:1)
我找到了两个解决方法。
maven.build.timestamp
Java代码:
@Mojo(name = "test", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class TestMojo extends AbstractMojo {
@Parameter
private String timestamp;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().error("timestamp: " + timestamp);
}
}
插件配置:
<plugin>
<groupId>com.mycompany</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.12</version>
<configuration>
<timestampString>${maven.build.timestamp}</timestampString>
</configuration>
</plugin>
缺点:
配置中的锅炉板代码。
session.request.startTime
作为默认值
Java代码:
@Mojo(name = "test", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class TestMojo extends AbstractMojo {
@Parameter(defaultValue = "${session.request.startTime}", readonly = true)
private Date timestamp;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().error("timestamp: " + timestamp);
}
}
插件配置:
<plugin>
<groupId>com.mycompany</groupId>
<artifactId>test-maven-plugin</artifactId>
<version>0.0.12</version>
</plugin>
缺点:
我不确定session.request.startTime
的值始终与maven.build.timestamp
相同。并且不会自动使用maven.build.timestamp.format
定义的格式。