Skip to content

Commit fe43d52

Browse files
committed
Merge branch '2.5.x' into 2.6.x
Closes gh-29560
2 parents 1204c1a + 3ed65f2 commit fe43d52

File tree

6 files changed

+206
-105
lines changed

6 files changed

+206
-105
lines changed

buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/MavenPluginPlugin.java

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,16 +17,31 @@
1717
package org.springframework.boot.build.mavenplugin;
1818

1919
import java.io.File;
20+
import java.io.FileInputStream;
21+
import java.io.FileWriter;
2022
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.Writer;
2125
import java.nio.charset.StandardCharsets;
2226
import java.nio.file.Files;
2327
import java.nio.file.Path;
2428
import java.nio.file.StandardCopyOption;
2529
import java.util.Arrays;
30+
import java.util.Properties;
31+
import java.util.function.BiConsumer;
32+
33+
import javax.xml.parsers.DocumentBuilder;
34+
import javax.xml.parsers.DocumentBuilderFactory;
35+
import javax.xml.parsers.ParserConfigurationException;
36+
import javax.xml.xpath.XPath;
37+
import javax.xml.xpath.XPathConstants;
38+
import javax.xml.xpath.XPathExpressionException;
39+
import javax.xml.xpath.XPathFactory;
2640

2741
import io.spring.javaformat.formatter.FileEdit;
2842
import io.spring.javaformat.formatter.FileFormatter;
2943
import org.gradle.api.DefaultTask;
44+
import org.gradle.api.GradleException;
3045
import org.gradle.api.Plugin;
3146
import org.gradle.api.Project;
3247
import org.gradle.api.Task;
@@ -41,6 +56,8 @@
4156
import org.gradle.api.attributes.Usage;
4257
import org.gradle.api.file.CopySpec;
4358
import org.gradle.api.file.DirectoryProperty;
59+
import org.gradle.api.file.FileCollection;
60+
import org.gradle.api.file.RegularFileProperty;
4461
import org.gradle.api.model.ObjectFactory;
4562
import org.gradle.api.plugins.JavaLibraryPlugin;
4663
import org.gradle.api.plugins.JavaPlugin;
@@ -50,8 +67,11 @@
5067
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
5168
import org.gradle.api.tasks.Classpath;
5269
import org.gradle.api.tasks.Copy;
70+
import org.gradle.api.tasks.InputFiles;
5371
import org.gradle.api.tasks.JavaExec;
5472
import org.gradle.api.tasks.OutputDirectory;
73+
import org.gradle.api.tasks.OutputFile;
74+
import org.gradle.api.tasks.PathSensitive;
5575
import org.gradle.api.tasks.PathSensitivity;
5676
import org.gradle.api.tasks.SourceSet;
5777
import org.gradle.api.tasks.SourceSetContainer;
@@ -60,10 +80,15 @@
6080
import org.gradle.api.tasks.bundling.Jar;
6181
import org.gradle.api.tasks.javadoc.Javadoc;
6282
import org.gradle.external.javadoc.StandardJavadocDocletOptions;
83+
import org.w3c.dom.Document;
84+
import org.w3c.dom.Node;
85+
import org.xml.sax.SAXException;
6386

6487
import org.springframework.boot.build.DeployedPlugin;
6588
import org.springframework.boot.build.MavenRepositoryPlugin;
6689
import org.springframework.boot.build.test.IntegrationTestPlugin;
90+
import org.springframework.core.CollectionFactory;
91+
import org.springframework.util.Assert;
6792

6893
/**
6994
* Plugin for building Spring Boot's Maven Plugin.
@@ -88,6 +113,7 @@ public void apply(Project project) {
88113
generateHelpMojoTask);
89114
addDocumentPluginGoalsTask(project, generatePluginDescriptorTask);
90115
addPrepareMavenBinariesTask(project);
116+
addExtractVersionPropertiesTask(project);
91117
}
92118

93119
private void configurePomPackaging(Project project) {
@@ -240,6 +266,14 @@ private String replaceVersionPlaceholder(Project project, String input) {
240266
return input.replace("{{version}}", project.getVersion().toString());
241267
}
242268

269+
private void addExtractVersionPropertiesTask(Project project) {
270+
ExtractVersionProperties extractVersionProperties = project.getTasks().create("extractVersionProperties",
271+
ExtractVersionProperties.class);
272+
extractVersionProperties.setEffectiveBoms(project.getConfigurations().create("versionProperties"));
273+
extractVersionProperties.getDestination().set(project.getLayout().getBuildDirectory().dir("generated-resources")
274+
.map((dir) -> dir.file("extracted-versions.properties")));
275+
}
276+
243277
public static class FormatHelpMojoSourceTask extends DefaultTask {
244278

245279
private Task generator;
@@ -361,4 +395,109 @@ public void createRepository() {
361395

362396
}
363397

398+
public static class ExtractVersionProperties extends DefaultTask {
399+
400+
private final RegularFileProperty destination;
401+
402+
private FileCollection effectiveBoms;
403+
404+
public ExtractVersionProperties() {
405+
this.destination = getProject().getObjects().fileProperty();
406+
}
407+
408+
@InputFiles
409+
@PathSensitive(PathSensitivity.RELATIVE)
410+
public FileCollection getEffectiveBoms() {
411+
return this.effectiveBoms;
412+
}
413+
414+
public void setEffectiveBoms(FileCollection effectiveBoms) {
415+
this.effectiveBoms = effectiveBoms;
416+
}
417+
418+
@OutputFile
419+
public RegularFileProperty getDestination() {
420+
return this.destination;
421+
}
422+
423+
@TaskAction
424+
public void extractVersionProperties() {
425+
EffectiveBom effectiveBom = new EffectiveBom(this.effectiveBoms.getSingleFile());
426+
Properties versions = extractVersionProperties(effectiveBom);
427+
writeProperties(versions);
428+
}
429+
430+
private void writeProperties(Properties versions) {
431+
File outputFile = this.destination.getAsFile().get();
432+
outputFile.getParentFile().mkdirs();
433+
try (Writer writer = new FileWriter(outputFile)) {
434+
versions.store(writer, null);
435+
}
436+
catch (IOException ex) {
437+
throw new GradleException("Failed to write extracted version properties", ex);
438+
}
439+
}
440+
441+
private Properties extractVersionProperties(EffectiveBom effectiveBom) {
442+
Properties versions = CollectionFactory.createSortedProperties(true);
443+
versions.setProperty("project.version", effectiveBom.version());
444+
effectiveBom.property("log4j2.version", versions::setProperty);
445+
effectiveBom.property("maven-jar-plugin.version", versions::setProperty);
446+
effectiveBom.property("maven-war-plugin.version", versions::setProperty);
447+
effectiveBom.property("build-helper-maven-plugin.version", versions::setProperty);
448+
effectiveBom.property("spring-framework.version", versions::setProperty);
449+
effectiveBom.property("jakarta-servlet.version", versions::setProperty);
450+
effectiveBom.property("kotlin.version", versions::setProperty);
451+
return versions;
452+
}
453+
454+
}
455+
456+
private static final class EffectiveBom {
457+
458+
private final Document document;
459+
460+
private final XPath xpath;
461+
462+
private EffectiveBom(File bomFile) {
463+
this.document = loadDocument(bomFile);
464+
this.xpath = XPathFactory.newInstance().newXPath();
465+
}
466+
467+
private Document loadDocument(File bomFile) {
468+
try {
469+
try (InputStream inputStream = new FileInputStream(bomFile)) {
470+
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
471+
DocumentBuilder builder = builderFactory.newDocumentBuilder();
472+
return builder.parse(inputStream);
473+
}
474+
}
475+
catch (ParserConfigurationException | SAXException | IOException ex) {
476+
throw new IllegalStateException(ex);
477+
}
478+
}
479+
480+
private String version() {
481+
return get("version");
482+
}
483+
484+
private void property(String name, BiConsumer<String, String> handler) {
485+
handler.accept(name, get("properties/" + name));
486+
}
487+
488+
private String get(String expression) {
489+
try {
490+
Node node = (Node) this.xpath.compile("/project/" + expression).evaluate(this.document,
491+
XPathConstants.NODE);
492+
String text = (node != null) ? node.getTextContent() : null;
493+
Assert.hasLength(text, () -> "No result for expression " + expression);
494+
return text;
495+
}
496+
catch (XPathExpressionException ex) {
497+
throw new IllegalStateException(ex);
498+
}
499+
}
500+
501+
}
502+
364503
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/build.gradle

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ dependencies {
2626
compileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations")
2727
compileOnly("org.sonatype.plexus:plexus-build-api")
2828

29-
dependenciesBom(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "effectiveBom"))
30-
3129
implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-buildpack-platform"))
3230
implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-loader-tools"))
3331
implementation("org.apache.maven.shared:maven-common-artifact-filters") {
@@ -62,11 +60,8 @@ dependencies {
6260
testImplementation("org.mockito:mockito-core")
6361
testImplementation("org.mockito:mockito-junit-jupiter")
6462
testImplementation("org.springframework:spring-core")
65-
}
6663

67-
task syncSpringBootDependenciesBom(type: Sync) {
68-
destinationDir = file("${buildDir}/generated-resources/org/springframework/boot/maven")
69-
from configurations.dependenciesBom
64+
versionProperties(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "effectiveBom"))
7065
}
7166

7267
syncDocumentationSourceForAsciidoctor {
@@ -77,7 +72,7 @@ syncDocumentationSourceForAsciidoctor {
7772

7873
sourceSets {
7974
intTest {
80-
output.dir("${buildDir}/generated-resources", builtBy: "syncSpringBootDependenciesBom")
75+
output.dir("${buildDir}/generated-resources", builtBy: "extractVersionProperties")
8176
}
8277
}
8378

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/EclipseM2eIntegrationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,8 +36,7 @@ class EclipseM2eIntegrationTests {
3636

3737
@Test // gh-21992
3838
void pluginPomIncludesOptionalShadeDependency() throws Exception {
39-
SpringBootDependenciesBom bom = new SpringBootDependenciesBom();
40-
String version = bom.get("version");
39+
String version = new Versions().get("project.version");
4140
File repository = new File("build/int-test-maven-repository");
4241
File pluginDirectory = new File(repository, "org/springframework/boot/spring-boot-maven-plugin/" + version);
4342
File[] pomFiles = pluginDirectory.listFiles(this::isPomFile);

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/MavenBuild.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,25 +87,13 @@ private File createTempDirectory() {
8787

8888
private Map<String, String> getPomReplacements() {
8989
Map<String, String> replacements = new HashMap<>();
90-
SpringBootDependenciesBom bom = new SpringBootDependenciesBom();
9190
replacements.put("java.version", "1.8");
9291
replacements.put("project.groupId", "org.springframework.boot");
9392
replacements.put("project.artifactId", "spring-boot-maven-plugin");
94-
replacements.put("project.version", bom.get("version"));
95-
putReplacement(replacements, bom, "log4j2.version");
96-
putReplacement(replacements, bom, "maven-jar-plugin.version");
97-
putReplacement(replacements, bom, "maven-war-plugin.version");
98-
putReplacement(replacements, bom, "build-helper-maven-plugin.version");
99-
putReplacement(replacements, bom, "spring-framework.version");
100-
putReplacement(replacements, bom, "jakarta-servlet.version");
101-
putReplacement(replacements, bom, "kotlin.version");
93+
replacements.putAll(new Versions().asMap());
10294
return Collections.unmodifiableMap(replacements);
10395
}
10496

105-
private void putReplacement(Map<String, String> replacements, SpringBootDependenciesBom bom, String property) {
106-
replacements.put(property, bom.get("properties/" + property));
107-
}
108-
10997
MavenBuild project(String project) {
11098
this.projectDir = new File("src/intTest/projects/" + project);
11199
return this;

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/SpringBootDependenciesBom.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)