Skip to content

Commit

Permalink
Merge branch '2.5.x' into 2.6.x
Browse files Browse the repository at this point in the history
Closes gh-29560
  • Loading branch information
wilkinsona committed Jan 25, 2022
2 parents 1204c1a + 3ed65f2 commit fe43d52
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
Expand All @@ -17,16 +17,31 @@
package org.springframework.boot.build.mavenplugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Properties;
import java.util.function.BiConsumer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import io.spring.javaformat.formatter.FileEdit;
import io.spring.javaformat.formatter.FileFormatter;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand All @@ -41,6 +56,8 @@
import org.gradle.api.attributes.Usage;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.JavaPlugin;
Expand All @@ -50,8 +67,11 @@
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Copy;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
Expand All @@ -60,10 +80,15 @@
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.external.javadoc.StandardJavadocDocletOptions;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import org.springframework.boot.build.DeployedPlugin;
import org.springframework.boot.build.MavenRepositoryPlugin;
import org.springframework.boot.build.test.IntegrationTestPlugin;
import org.springframework.core.CollectionFactory;
import org.springframework.util.Assert;

/**
* Plugin for building Spring Boot's Maven Plugin.
Expand All @@ -88,6 +113,7 @@ public void apply(Project project) {
generateHelpMojoTask);
addDocumentPluginGoalsTask(project, generatePluginDescriptorTask);
addPrepareMavenBinariesTask(project);
addExtractVersionPropertiesTask(project);
}

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

private void addExtractVersionPropertiesTask(Project project) {
ExtractVersionProperties extractVersionProperties = project.getTasks().create("extractVersionProperties",
ExtractVersionProperties.class);
extractVersionProperties.setEffectiveBoms(project.getConfigurations().create("versionProperties"));
extractVersionProperties.getDestination().set(project.getLayout().getBuildDirectory().dir("generated-resources")
.map((dir) -> dir.file("extracted-versions.properties")));
}

public static class FormatHelpMojoSourceTask extends DefaultTask {

private Task generator;
Expand Down Expand Up @@ -361,4 +395,109 @@ public void createRepository() {

}

public static class ExtractVersionProperties extends DefaultTask {

private final RegularFileProperty destination;

private FileCollection effectiveBoms;

public ExtractVersionProperties() {
this.destination = getProject().getObjects().fileProperty();
}

@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileCollection getEffectiveBoms() {
return this.effectiveBoms;
}

public void setEffectiveBoms(FileCollection effectiveBoms) {
this.effectiveBoms = effectiveBoms;
}

@OutputFile
public RegularFileProperty getDestination() {
return this.destination;
}

@TaskAction
public void extractVersionProperties() {
EffectiveBom effectiveBom = new EffectiveBom(this.effectiveBoms.getSingleFile());
Properties versions = extractVersionProperties(effectiveBom);
writeProperties(versions);
}

private void writeProperties(Properties versions) {
File outputFile = this.destination.getAsFile().get();
outputFile.getParentFile().mkdirs();
try (Writer writer = new FileWriter(outputFile)) {
versions.store(writer, null);
}
catch (IOException ex) {
throw new GradleException("Failed to write extracted version properties", ex);
}
}

private Properties extractVersionProperties(EffectiveBom effectiveBom) {
Properties versions = CollectionFactory.createSortedProperties(true);
versions.setProperty("project.version", effectiveBom.version());
effectiveBom.property("log4j2.version", versions::setProperty);
effectiveBom.property("maven-jar-plugin.version", versions::setProperty);
effectiveBom.property("maven-war-plugin.version", versions::setProperty);
effectiveBom.property("build-helper-maven-plugin.version", versions::setProperty);
effectiveBom.property("spring-framework.version", versions::setProperty);
effectiveBom.property("jakarta-servlet.version", versions::setProperty);
effectiveBom.property("kotlin.version", versions::setProperty);
return versions;
}

}

private static final class EffectiveBom {

private final Document document;

private final XPath xpath;

private EffectiveBom(File bomFile) {
this.document = loadDocument(bomFile);
this.xpath = XPathFactory.newInstance().newXPath();
}

private Document loadDocument(File bomFile) {
try {
try (InputStream inputStream = new FileInputStream(bomFile)) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
return builder.parse(inputStream);
}
}
catch (ParserConfigurationException | SAXException | IOException ex) {
throw new IllegalStateException(ex);
}
}

private String version() {
return get("version");
}

private void property(String name, BiConsumer<String, String> handler) {
handler.accept(name, get("properties/" + name));
}

private String get(String expression) {
try {
Node node = (Node) this.xpath.compile("/project/" + expression).evaluate(this.document,
XPathConstants.NODE);
String text = (node != null) ? node.getTextContent() : null;
Assert.hasLength(text, () -> "No result for expression " + expression);
return text;
}
catch (XPathExpressionException ex) {
throw new IllegalStateException(ex);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ dependencies {
compileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations")
compileOnly("org.sonatype.plexus:plexus-build-api")

dependenciesBom(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "effectiveBom"))

implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-buildpack-platform"))
implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-loader-tools"))
implementation("org.apache.maven.shared:maven-common-artifact-filters") {
Expand Down Expand Up @@ -62,11 +60,8 @@ dependencies {
testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter")
testImplementation("org.springframework:spring-core")
}

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

syncDocumentationSourceForAsciidoctor {
Expand All @@ -77,7 +72,7 @@ syncDocumentationSourceForAsciidoctor {

sourceSets {
intTest {
output.dir("${buildDir}/generated-resources", builtBy: "syncSpringBootDependenciesBom")
output.dir("${buildDir}/generated-resources", builtBy: "extractVersionProperties")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
Expand Down Expand Up @@ -36,8 +36,7 @@ class EclipseM2eIntegrationTests {

@Test // gh-21992
void pluginPomIncludesOptionalShadeDependency() throws Exception {
SpringBootDependenciesBom bom = new SpringBootDependenciesBom();
String version = bom.get("version");
String version = new Versions().get("project.version");
File repository = new File("build/int-test-maven-repository");
File pluginDirectory = new File(repository, "org/springframework/boot/spring-boot-maven-plugin/" + version);
File[] pomFiles = pluginDirectory.listFiles(this::isPomFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.
Expand Down Expand Up @@ -87,25 +87,13 @@ private File createTempDirectory() {

private Map<String, String> getPomReplacements() {
Map<String, String> replacements = new HashMap<>();
SpringBootDependenciesBom bom = new SpringBootDependenciesBom();
replacements.put("java.version", "1.8");
replacements.put("project.groupId", "org.springframework.boot");
replacements.put("project.artifactId", "spring-boot-maven-plugin");
replacements.put("project.version", bom.get("version"));
putReplacement(replacements, bom, "log4j2.version");
putReplacement(replacements, bom, "maven-jar-plugin.version");
putReplacement(replacements, bom, "maven-war-plugin.version");
putReplacement(replacements, bom, "build-helper-maven-plugin.version");
putReplacement(replacements, bom, "spring-framework.version");
putReplacement(replacements, bom, "jakarta-servlet.version");
putReplacement(replacements, bom, "kotlin.version");
replacements.putAll(new Versions().asMap());
return Collections.unmodifiableMap(replacements);
}

private void putReplacement(Map<String, String> replacements, SpringBootDependenciesBom bom, String property) {
replacements.put(property, bom.get("properties/" + property));
}

MavenBuild project(String project) {
this.projectDir = new File("src/intTest/projects/" + project);
return this;
Expand Down

This file was deleted.

0 comments on commit fe43d52

Please sign in to comment.