Resolve versionManagement configuration lazily and preserve exclusions
Previously, the versionManagement configuration was resolved as part of the Boot Gradle plugin being applied. This meant that no dependencies could be added to it and attempting to do so would result in a failure: “You can't change a configuration which is not in unresolved state”. This commit updates ApplyExcludeRules to wrap its processing in a before resolve action. This defers the resolution of the versionManagement configuration until one of the project’s other configurations is being resolved. Fixes #1077 In addition to the above, the transitive exclusions that the Gradle plugin provides were being lost if custom version management provided a version for the same dependency. This commit updates AbstractDependencies to preserve the exclusions from an existing dependency declaration while using the version from the newer dependency. This ensures that the exclusions remain while allowing versions to be overridden. Fixes #1079pull/1082/head
parent
9e93719922
commit
f5f3903538
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gradle.tooling.ProjectConnection;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.dependency.tools.ManagedDependencies;
|
||||
|
||||
/**
|
||||
* Tests for using the Gradle plugin's support for custom version management
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class CustomVersionManagementTests {
|
||||
|
||||
private static final String BOOT_VERSION = ManagedDependencies.get()
|
||||
.find("spring-boot").getVersion();
|
||||
|
||||
private static ProjectConnection project;
|
||||
|
||||
@BeforeClass
|
||||
public static void createProject() throws IOException {
|
||||
project = new ProjectCreator().createProject("custom-version-management");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exclusionsAreStillInPlace() {
|
||||
project.newBuild().forTasks("checkExclusions")
|
||||
.withArguments("-PbootVersion=" + BOOT_VERSION).run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customSpringVersionIsUsed() {
|
||||
project.newBuild().forTasks("checkSpringVersion")
|
||||
.withArguments("-PbootVersion=" + BOOT_VERSION).run();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2014 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gradle.tooling.GradleConnector;
|
||||
import org.gradle.tooling.ProjectConnection;
|
||||
import org.gradle.tooling.internal.consumer.DefaultGradleConnector;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ProjectCreator {
|
||||
|
||||
public ProjectConnection createProject(String name) throws IOException {
|
||||
File projectDirectory = new File("target/" + name);
|
||||
projectDirectory.mkdirs();
|
||||
|
||||
File gradleScript = new File(projectDirectory, "build.gradle");
|
||||
FileCopyUtils.copy(new File("src/test/resources/" + name + ".gradle"),
|
||||
gradleScript);
|
||||
|
||||
GradleConnector gradleConnector = GradleConnector.newConnector();
|
||||
((DefaultGradleConnector) gradleConnector).embedded(true);
|
||||
return gradleConnector.forProjectDirectory(projectDirectory).connect();
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
import org.gradle.api.artifacts.result.UnresolvedDependencyResult;
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}")
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
configurations {
|
||||
exclusions
|
||||
springVersion
|
||||
}
|
||||
|
||||
apply plugin: 'spring-boot'
|
||||
|
||||
dependencies {
|
||||
exclusions "org.springframework.boot:spring-boot-starter"
|
||||
springVersion "org.springframework:spring-core"
|
||||
versionManagement files('../../src/test/resources/custom-versions.properties')
|
||||
}
|
||||
|
||||
task checkExclusions {
|
||||
doFirst {
|
||||
def commonsLogging = getResolvedDependencies(configurations.exclusions)
|
||||
.find { it.selected.id.group == 'commons-logging' }
|
||||
if (commonsLogging) {
|
||||
throw new GradleException("commong-logging is a transitive dependency")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task checkSpringVersion {
|
||||
doFirst {
|
||||
def springVersions = getResolvedDependencies(configurations.springVersion)
|
||||
.findAll{it.selected.id.group == 'org.springframework'}
|
||||
.collect {it.selected.id.version}
|
||||
if (springVersions.size() != 1 || !springVersions.contains("4.0.3.RELEASE")) {
|
||||
throw new GradleException("Spring version was not 4.0.3.RELEASE")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def getResolvedDependencies(def configuration) {
|
||||
def allDependencies = configuration.incoming
|
||||
.resolutionResult.allDependencies
|
||||
.split { it instanceof UnresolvedDependencyResult }
|
||||
|
||||
def unresolved = allDependencies.first()
|
||||
def resolved = allDependencies.last()
|
||||
if (unresolved) {
|
||||
throw new GradleException("Resolution failed: ${unresolved}")
|
||||
}
|
||||
resolved
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
org.springframework\:spring-core=4.0.3.RELEASE
|
||||
org.springframework.boot\:spring-boot-starter=1.1.0.RELEASE
|
Loading…
Reference in New Issue