Commit b1f91233 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 2ce85569
/*
* Copyright 2012-2016 the original author or authors.
* 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.
......@@ -36,12 +36,16 @@ public class DependencyManagementPluginFeatures implements PluginFeatures {
private static final String SPRING_BOOT_VERSION = DependencyManagementPluginFeatures.class
.getPackage().getImplementationVersion();
private static final String SPRING_BOOT_BOM = "org.springframework.boot:spring-boot-starter-parent:"
private static final String SPRING_BOOT_BOM = "org.springframework.boot:spring-boot-dependencies:"
+ SPRING_BOOT_VERSION;
@Override
public void apply(Project project) {
project.getPlugins().apply(DependencyManagementPlugin.class);
project.getPlugins().withType(DependencyManagementPlugin.class,
(plugin) -> configureDependencyManagement(project));
}
private void configureDependencyManagement(Project project) {
DependencyManagementExtension dependencyManagement = project.getExtensions()
.findByType(DependencyManagementExtension.class);
dependencyManagement.imports(new Action<ImportsHandler>() {
......
/*
* 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);
}
}
......@@ -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);
}
}
}
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
if (project.hasProperty('applyDependencyManagementPlugin')) {
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
resolutionStrategy {
eachDependency {
if (it.requested.group == 'org.springframework.boot') {
it.useVersion project.bootVersion
}
}
}
}
}
repositories {
mavenLocal()
}
task doesNotHaveDependencyManagement {
doLast {
if (project.extensions.findByName('dependencyManagement') != null) {
throw new GradleException('Found dependency management extension')
}
}
}
task hasDependencyManagement {
doLast {
if (!dependencyManagement.managedVersions) {
throw new GradleException('No managed versions have been configured')
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment