-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Allow build info goal's build.time to be disabled so that its output is repeatable #17390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Our Gradle plugin allows the automatic generation of the build time to be disabled by either setting it to |
Thanks, @Parameter(defaultValue = "${maven.build.timestamp}")
private Date buildTime; to disable <plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<configuration>
<buildTime/>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
|
@nosan That looks like it would give us the behaviour that we would like. Was there a reason that you didn’t propose that approach in the end? |
@wilkinsona |
Btw, Maybe it makes sense to use |
Thanks again. There's no need for an apology. I just wanted to check that we hadn't overlooked something that meant the approach would not work. Yeah, I think that |
@wilkinsona BackgroundIf the value of a property is empty, Considering the above, the following would not work: <plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<configuration>
<time/>
</configuration>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin> @Parameter
private Date time = new Date(); // as is, <time/> -> empty -> null -> ignored
@Parameter(defaultValue = "...")
private Date time; // default value, <time/> -> empty -> null -> ignored I confused myself and you as well. 😕 🤦♂ Possible solutionThis is how to achieve the same behavior as Click to expand@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class BuildInfoMojo extends AbstractMojo {
@Component
private BuildContext buildContext;
/**
* The Maven project.
*/
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
/**
* The location of the generated build-info.properties.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties")
private File outputFile;
/**
* Additional properties to store in the build-info.properties. Each entry is prefixed
* by {@code build.} in the generated build-info.properties.
* @deprecated since 2.2.0
*/
@Parameter
@Deprecated
private Map<String, String> additionalProperties;
/**
* The properties that are written into the {@code build-info.properties} file.
*/
@Parameter
private Map<String, Object> properties;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
new BuildPropertiesWriter(this.outputFile).writeBuildProperties(new ProjectDetails(getGroup(),
getArtifact(), getVersion(), getName(), getTime(), getAdditionalProperties()));
this.buildContext.refresh(this.outputFile);
}
catch (NullAdditionalPropertyValueException ex) {
throw new MojoFailureException("Failed to generate build-info.properties. " + ex.getMessage(), ex);
}
catch (Exception ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}
}
private String getGroup() {
return Objects.toString(getProperties().getOrDefault("group", this.project.getGroupId()));
}
private String getArtifact() {
return Objects.toString(getProperties().getOrDefault("artifact", this.project.getArtifactId()));
}
private String getVersion() {
return Objects.toString(getProperties().getOrDefault("version", this.project.getVersion()));
}
private String getName() {
return Objects.toString(getProperties().getOrDefault("name", this.project.getName()));
}
private Instant getTime() {
if (getProperties().containsKey("time")) {
String time = Objects.toString(getProperties().get("time"), null);
return (time != null) ? Instant.parse(time) : null;
}
return Instant.now();
}
@SuppressWarnings("unchecked")
private Map<String, String> getAdditionalProperties() {
return (Map<String, String>) getProperties().getOrDefault("additionalProperties", this.additionalProperties);
}
private Map<String, Object> getProperties() {
return (this.properties != null) ? this.properties : Collections.emptyMap();
}
} |
That's a shame. Maven's handling of null and empty properties is rather frustrating. Thanks for the alternative solution. Another option, inspired by it, would be to special-case the handling of an additional property named Yet another option would be to provide a |
@wilkinsona |
418d692
to
a0daa58
Compare
I've just discussed this with @snicoll and he is in favour of the |
Thanks for the latest updates, @nosan. I'd prefer to keep this change focussed on the build time. Do you have time to revert the changes that allow the artifact, group, name, and version to be customized? |
@wilkinsona sure, I let you know |
…ut is repeatable. Prior to this commit, there was no way to disable `build.time`. This commit introduces a `<time/>` configuration property that can be used to set a custom time or to disable `build.time` at all. To disable `build.time`, `off` value should be used. Closes spring-projectsgh-17294
@wilkinsona PR has been updated. |
That was quick! Thanks very much, @nosan. The proposed changes are now in master. |
Thanks once again, @wilkinsona |
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
see gh-17294