dur
dur

Reputation: 16969

How to use ${maven.build.timestamp} in a custom Maven plug-in?

I want to access the Maven build timestamp in my custom Maven plug-in. Therefore, I try to use the special variable maven.build.timestamp :

maven.build.timestamp The timestamp that denotes the start of the build. Since Maven 2.1.0-M1

with Parameter#defaultValue:

parameter default value, eventually containing ${...} expressions which will be interpreted at inject time: see PluginParameterExpressionEvaluator.

but I get always the value null. I tried it with type Date and with type String.

Java code:

@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);
    }
}

Plug-in configuration:

<plugin>
    <groupId>com.mycompany</groupId>
     <artifactId>test-maven-plugin</artifactId>
    <version>0.0.12</version>
</plugin>

Log:

[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] ------------------------------------------------------------------------

Environment:

Why is the special variable not resolved in my custom Maven plug-in?

Upvotes: 2

Views: 3513

Answers (1)

dur
dur

Reputation: 16969

I found two workarounds.

  • maven.build.timestamp in plug-in configuration

    Java code:

    @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);
        }
    }
    

    Plug-in configuration:

    <plugin>
         <groupId>com.mycompany</groupId>
         <artifactId>test-maven-plugin</artifactId>
         <version>0.0.12</version>
         <configuration>
             <timestampString>${maven.build.timestamp}</timestampString>
         </configuration>
    </plugin>
    

    Disadvantage:

    The boiler plate code in the configuration.

  • session.request.startTime as default value

    Java code:

    @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);
        }
    }
    

    Plug-in configuration:

    <plugin>
         <groupId>com.mycompany</groupId>
         <artifactId>test-maven-plugin</artifactId>
         <version>0.0.12</version>
    </plugin>
    

    Disadvantage:

    I'm not sure, that the value of session.request.startTime is always the same as maven.build.timestamp. And the format defined with maven.build.timestamp.format is not automatically used.

Upvotes: 2

Related Questions