Allow build info properties to be excluded

Update Maven and Gradle plugins to allow build info properties to be
excluded.

Prior to this commit, the `BuildPropertiesWriter` would fail with an
NPE if the group, artifact, name or version properties were `null`.

This was specifically problematic with the Gradle plugin, since its
DSL allows `null` properties which would either be passed to the writer
or, in the case of `artifact`, converted into a string value of
"unspecified".

See gh-27412
This commit is contained in:
Vedran Pavic
2021-07-16 21:57:57 +02:00
committed by Phillip Webb
parent 2034ad4827
commit ea9faf8690
13 changed files with 355 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import java.util.Map;
import java.util.Properties;
import org.springframework.core.CollectionFactory;
import org.springframework.util.StringUtils;
/**
* A {@code BuildPropertiesWriter} writes the {@code build-info.properties} for
@@ -32,6 +33,7 @@ import org.springframework.core.CollectionFactory;
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.0.0
*/
public final class BuildPropertiesWriter {
@@ -71,10 +73,18 @@ public final class BuildPropertiesWriter {
protected Properties createBuildInfo(ProjectDetails project) {
Properties properties = CollectionFactory.createSortedProperties(true);
properties.put("build.group", project.getGroup());
properties.put("build.artifact", project.getArtifact());
properties.put("build.name", project.getName());
properties.put("build.version", project.getVersion());
if (StringUtils.hasText(project.getGroup())) {
properties.put("build.group", project.getGroup());
}
if (StringUtils.hasText(project.getArtifact())) {
properties.put("build.artifact", project.getArtifact());
}
if (StringUtils.hasText(project.getName())) {
properties.put("build.name", project.getName());
}
if (StringUtils.hasText(project.getVersion())) {
properties.put("build.version", project.getVersion());
}
if (project.getTime() != null) {
properties.put("build.time", DateTimeFormatter.ISO_INSTANT.format(project.getTime()));
}