React to dependency management plugin rather than always applying it

Previously, the Spring Boot plugin would automatically apply the
dependency management plugin and import the spring-boot-dependencies
bom. This made it very difficult to use Spring Boot's plugin without
also using its dependency management. It also made it difficult to
see where the dependency management was coming from.

This commit updates the Spring Boot plugin so that it no longer
automatically applies the dependency management plugin. Instead, the
plugin now reacts to the dependency management plugin being applied
by importing the spring-boot-dependencies bom. Users that do not
wish to use Spring Boot's dependency management capabilities can now
do so by not applying the dependency management plugin.

Closes gh-3164
This commit is contained in:
Andy Wilkinson
2017-03-21 14:56:13 +00:00
parent 2ce8556976
commit b1f9123311
4 changed files with 138 additions and 9 deletions

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.gradle.dependencymanagement;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for the plugin's dependency management features.
*
* @author Andy Wilkinson
*/
public class DependencyManagementIntegrationTests {
@Rule
public GradleBuild gradleBuild = new GradleBuild();
@Test
public void noDependencyManagementIsAppliedByDefault() {
assertThat(this.gradleBuild.build("doesNotHaveDependencyManagement")
.task(":doesNotHaveDependencyManagement").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
}
@Test
public void bomIsImportedWhenDependencyManagementPluginIsApplied() {
assertThat(this.gradleBuild
.build("hasDependencyManagement", "-PapplyDependencyManagementPlugin")
.task(":hasDependencyManagement").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.gradle.testkit;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
@@ -25,6 +26,10 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
@@ -32,6 +37,7 @@ import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.xml.sax.InputSource;
import org.springframework.boot.loader.tools.LaunchScript;
@@ -80,12 +86,18 @@ public class GradleBuild implements TestRule {
}
private String pluginClasspath() {
return new File("build/classes/main").getAbsolutePath() + ","
+ new File("build/resources/main").getAbsolutePath() + ","
+ LaunchScript.class.getProtectionDomain().getCodeSource().getLocation()
.getPath()
+ "," + DependencyManagementPlugin.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();
return absolutePath("bin") + "," + absolutePath("build/classes/main") + ","
+ absolutePath("build/resources/main") + ","
+ pathOfJarContaining(LaunchScript.class) + ","
+ pathOfJarContaining(DependencyManagementPlugin.class);
}
private String absolutePath(String path) {
return new File(path).getAbsolutePath();
}
private String pathOfJarContaining(Class<?> type) {
return type.getProtectionDomain().getCodeSource().getLocation().getPath();
}
public GradleBuild script(String script) {
@@ -102,6 +114,7 @@ public class GradleBuild implements TestRule {
.withProjectDir(this.projectDir).forwardOutput();
List<String> allArguments = new ArrayList<String>();
allArguments.add("-PpluginClasspath=" + pluginClasspath());
allArguments.add("-PbootVersion=" + getBootVersion());
allArguments.addAll(Arrays.asList(arguments));
return gradleRunner.withArguments(allArguments).build();
}
@@ -110,4 +123,23 @@ public class GradleBuild implements TestRule {
}
}
public static String getBootVersion() {
return evaluateExpression(
"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"
+ "/text()");
}
private static String evaluateExpression(String expression) {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile(expression);
String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
return version;
}
catch (Exception ex) {
throw new IllegalStateException("Failed to evaluate expression", ex);
}
}
}