mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Work around Gradle 3.3's overzealous deprecation warning
In Gradle 3.3, when an API that will be removed in 4.0 is called, a deprecation warning is output as part of the build. Users have noticed his warning and, quite reasonably, complained about it. Unfortunately, avoiding the warning requires the use of an API that was introduced in Gradle 3.x which we can't use directly as we want to remain compatible with Gradle 2.x. This commit introduces the use of reflection to call ProjectDependency.getTargetConfiguration() via reflection when it's available (Gradle 3) and ProjectDependency.getProjectConfiguration() when it's not (Gradle 2). Closes gh-8154
This commit is contained in:
parent
ab192d01cc
commit
b931f564e6
@ -18,6 +18,7 @@ package org.springframework.boot.gradle.repackage;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
@ -50,6 +51,8 @@ class ProjectLibraries implements Libraries {
|
||||
|
||||
private final boolean excludeDevtools;
|
||||
|
||||
private final TargetConfigurationResolver targetConfigurationResolver;
|
||||
|
||||
private String providedConfigurationName = "providedRuntime";
|
||||
|
||||
private String customConfigurationName = null;
|
||||
@ -65,6 +68,17 @@ class ProjectLibraries implements Libraries {
|
||||
this.project = project;
|
||||
this.extension = extension;
|
||||
this.excludeDevtools = excludeDevTools;
|
||||
this.targetConfigurationResolver = createTargetConfigurationResolver();
|
||||
}
|
||||
|
||||
private static TargetConfigurationResolver createTargetConfigurationResolver() {
|
||||
try {
|
||||
return new Gradle3TargetConfigurationResolver(
|
||||
ProjectDependency.class.getMethod("getTargetConfiguration"));
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return new Gradle2TargetConfigurationResolver();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -127,11 +141,11 @@ class ProjectLibraries implements Libraries {
|
||||
}
|
||||
else if (dependency instanceof ProjectDependency) {
|
||||
ProjectDependency projectDependency = (ProjectDependency) dependency;
|
||||
Configuration dependencyConfiguration = projectDependency
|
||||
.getDependencyProject().getConfigurations()
|
||||
.getByName(projectDependency.getConfiguration());
|
||||
libraries.addAll(
|
||||
getLibrariesForFileDependencies(dependencyConfiguration, scope));
|
||||
libraries
|
||||
.addAll(getLibrariesForFileDependencies(
|
||||
this.targetConfigurationResolver
|
||||
.resolveTargetConfiguration(projectDependency),
|
||||
scope));
|
||||
}
|
||||
}
|
||||
return libraries;
|
||||
@ -264,4 +278,47 @@ class ProjectLibraries implements Libraries {
|
||||
|
||||
}
|
||||
|
||||
private interface TargetConfigurationResolver {
|
||||
|
||||
Configuration resolveTargetConfiguration(ProjectDependency projectDependency);
|
||||
|
||||
}
|
||||
|
||||
private static final class Gradle2TargetConfigurationResolver
|
||||
implements TargetConfigurationResolver {
|
||||
|
||||
@Override
|
||||
public Configuration resolveTargetConfiguration(
|
||||
ProjectDependency projectDependency) {
|
||||
return projectDependency.getProjectConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final class Gradle3TargetConfigurationResolver
|
||||
implements TargetConfigurationResolver {
|
||||
|
||||
private final Method getTargetConfiguration;
|
||||
|
||||
private Gradle3TargetConfigurationResolver(Method getTargetConfiguration) {
|
||||
this.getTargetConfiguration = getTargetConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Configuration resolveTargetConfiguration(
|
||||
ProjectDependency projectDependency) {
|
||||
try {
|
||||
String configurationName = (String) this.getTargetConfiguration
|
||||
.invoke(projectDependency);
|
||||
return projectDependency.getDependencyProject().getConfigurations()
|
||||
.getByName(configurationName == null
|
||||
? Dependency.DEFAULT_CONFIGURATION : configurationName);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new RuntimeException("Failed to get target configuration", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user