如何在自定义Maven插件中使用$ {maven.build.timestamp}?

时间:2017-04-21 12:45:08

标签: maven maven-plugin

我想在我的自定义Maven插件中访问Maven构建时间戳。因此,我尝试使用特殊变量maven.build.timestamp

  

maven.build.timestamp表示构建开始的时间戳。 自Maven 2.1.0-M1

Parameter#defaultValue

  

参数默认值,最终包含将在注入时解释的${...}表达式:请参阅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] ------------------------------------------------------------------------

环境:

  • Java 8
  • embedded Maven 3.3.3

为什么我的自定义Maven插件中没有解决特殊变量?

1 个答案:

答案 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定义的格式。