[spring-projects/spring-boot]允许禁用构建信息目标的 build.time,以便其输出可重复

2024-04-10 873 views
3

参见 gh-17294

回答

7

null我们的 Gradle 插件允许通过将其设置为(将其关闭,完全删除属性)或固定值来禁用构建时间的自动生成。我们应该考虑是否要对 Maven 做同样的事情,而不是仅仅允许它关闭。

3

谢谢,从一开始,我就想做:


    @Parameter(defaultValue = "${maven.build.timestamp}")
    private Date buildTime;

要禁用buildTime需要使用以下配置:

<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>
4

@nosan 看起来它会给我们我们想要的行为。您最终没有提出这种方法有什么原因吗?

0

@wilkinsona我没有任何理由这样做。我不知道哪种方法更好、更合适。抱歉,我一开始没有提到第二种方法。

8

顺便说一句,也许使用插件time而不是buildTime为了与Gradle插件保持一致是有意义的。

8

再次感谢。没有必要道歉。我只是想检查一下我们是否忽略了一些导致该方法行不通的事情。

是的,我认为这time也有道理。它与 Gradle 插件一致,我认为有足够的上下文可以清楚地表明正在配置的构建时间。

9

@wilkinsona第二种方法不起作用。这很奇怪,因为昨天它有效,至少我是这么认为的。

背景

如果属性值为空,Maven则将其视为 null。

考虑到上述情况,以下内容将不起作用:

<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

我自己和你也很困惑。 ? ?‍♂

可能的解决方案

这是如何实现与Gradle插件相同的行为:

点击展开 ```java @Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true) public class BuildInfoMojo extends AbstractMojo { @Component private BuildContext buildContext; /** * Maven 项目。 */ @Parameter(defaultValue = "${project}", readonly = true, required = true) 私有 MavenProject 项目; /** * 生成的build-info.properties的位置。 */ @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties") 私有文件outputFile; /** * 要存储在 build-info.properties 中的其他属性。在生成的 build-info.properties 中,每个条目都以 {@code build.} 为前缀 *。 * @deprecated 自 2.2.0 起 */ @Parameter @Deprecated private Map附加属性; /** * 写入 {@code build-info.properties} 文件的属性。 */ @Parameter私有Map特性; @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("无法生成 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); }返回(时间!=空)? Instant.parse(时间) : null;返回 Instant.now(); @SuppressWarnings("unchecked") 私人地图getAdditionalProperties() { return (地图) getProperties().getOrDefault("additionalProperties", this.additionalProperties); } 私人地图getProperties() { return (this.properties != null) ? this.properties : Collections.emptyMap(); } } ```
1

太可惜了。 Maven 对 null 和空属性的处理相当令人沮丧。

感谢您提供替代解决方案。受其启发,另一个选择是对名为 的附加属性进行特殊处理time。与其他附加属性不同,我们可以允许它null,如果是,则禁用构建时间。如果它非空,我们就会将该值解析为 anInstant并使用它。

另一种选择是提供一个String time属性,该属性可以配置为off禁用该属性,也可以配置为解析为然后用于设置构建时间build.time的值。Instant

8

@wilkinsona PR 已更新。我添加了一种build-info通过插件配置属性的方法。要禁用build.timeoff可以使用值。

0

我刚刚与@snicoll 讨论过这个问题,他赞成这种<time>off<time>方法。

1

感谢您的最新更新,@nosan。我更愿意将此更改集中在构建时间上。您是否有时间恢复允许自定义工件、组、名称和版本的更改?

4

@wilkinsona 当然,我让你知道

6

@wilkinsona PR 已更新。

2

那很快!非常感谢,@nosan。提议的更改现在已在 master 中。

3

再次感谢@wilkinsona