Commit 74d00354 authored by Phillip Webb's avatar Phillip Webb

Refine BuildInfoMojo time property

Update `BuildInfoMojo` so that the time property now defaults to
`${session.request.startTime}` rather than the time the Mojo was
created. Also update javadoc to make it clear that any supplied
value will be passed to `Instant.parse`.

See gh-17390
parent 9f090c4f
......@@ -20,6 +20,7 @@ import java.io.File;
import java.time.Instant;
import java.util.Map;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
......@@ -47,6 +48,12 @@ public class BuildInfoMojo extends AbstractMojo {
@Component
private BuildContext buildContext;
/**
* The Maven session.
*/
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;
/**
* The Maven project.
*/
......@@ -60,13 +67,13 @@ public class BuildInfoMojo extends AbstractMojo {
private File outputFile;
/**
* Sets the value used for the {@code build.time} property. Defaults to
* {@link Instant#now} when the {@code mojo} instance was created. To disable
* {@code build.time} property, {@code 'off'} value should be used.
* The value used for the {@code build.time} property in a form suitable for
* {@link Instant#parse(CharSequence)}. Defaults to {@code session.request.startTime}.
* To disable the {@code build.time} property entirely, use {@code 'off'}.
* @since 2.2.0
*/
@Parameter
private String time = Instant.now().toString();
private String time;
/**
* Additional properties to store in the build-info.properties. Each entry is prefixed
......@@ -78,9 +85,9 @@ public class BuildInfoMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(new ProjectDetails(
this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(),
this.project.getName(), getBuildTime(), this.additionalProperties));
ProjectDetails details = new ProjectDetails(this.project.getGroupId(), this.project.getArtifactId(),
this.project.getVersion(), this.project.getName(), getBuildTime(), this.additionalProperties);
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(details);
this.buildContext.refresh(this.outputFile);
}
catch (NullAdditionalPropertyValueException ex) {
......@@ -92,7 +99,13 @@ public class BuildInfoMojo extends AbstractMojo {
}
private Instant getBuildTime() {
return "off".equals(this.time) ? null : Instant.parse(this.time);
if (this.time == null || this.time.isEmpty()) {
return this.session.getRequest().getStartTime().toInstant();
}
if ("off".equalsIgnoreCase(this.time)) {
return null;
}
return Instant.parse(this.time);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment