Skip to content

Commit 273600b

Browse files
committed
Retain distinction between compile and runtime deps of optional deps
Previously, the optional configuration was added to the compile and runtime classpaths of each source set and the the javadoc classpath as well. This had a few disadvantages, the most notable of which is that it meant that the configuration was ifrst resolved and then the outcome of the resolution was added to the compile and runtime classpaths. As a result, none of the attributes on the compile and runtime classpaths were considered to influence variant selection. This commit reworks the optional dependencies plugin so that the compile and runtime classpaths of each source set are now configured to extend from the optional configuration. This allows each classpath configuration's attributes to influence the dependencies that are selected from the optional configuration during resolution. For example, when resolving the compile classpath, compile dependencies (Usage.JAVA_API) will be selected and when resolving the runtime classpath, runtime dependencies (Usage.JAVA_RUNTIME) will be selected. The above-described change means that runtime dependencies of an optional dependencies will no longer leak into the compile classpath. As a result of this, our Gradle plugin's test infrastructure has been updated so that it no longer references runtime dependencies of the Kotlin Gradle plugin at compile time. Closes gh-27965
1 parent ab81e3c commit 273600b

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,19 +19,15 @@
1919
import org.gradle.api.Plugin;
2020
import org.gradle.api.Project;
2121
import org.gradle.api.artifacts.Configuration;
22-
import org.gradle.api.attributes.Usage;
2322
import org.gradle.api.plugins.JavaPlugin;
2423
import org.gradle.api.plugins.JavaPluginConvention;
2524
import org.gradle.api.tasks.SourceSetContainer;
26-
import org.gradle.api.tasks.javadoc.Javadoc;
27-
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
28-
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
2925

3026
/**
3127
* A {@code Plugin} that adds support for Maven-style optional dependencies. Creates a new
3228
* {@code optional} configuration. The {@code optional} configuration is part of the
33-
* project's compile and runtime classpath's but does not affect the classpath of
34-
* dependent projects.
29+
* project's compile and runtime classpaths but does not affect the classpath of dependent
30+
* projects.
3531
*
3632
* @author Andy Wilkinson
3733
*/
@@ -44,22 +40,19 @@ public class OptionalDependenciesPlugin implements Plugin<Project> {
4440

4541
@Override
4642
public void apply(Project project) {
47-
Configuration optional = project.getConfigurations().create(OPTIONAL_CONFIGURATION_NAME);
48-
optional.attributes((attributes) -> attributes.attribute(Usage.USAGE_ATTRIBUTE,
49-
project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)));
43+
Configuration optional = project.getConfigurations().create("optional");
44+
optional.setCanBeConsumed(false);
45+
optional.setCanBeResolved(false);
5046
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
5147
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class)
5248
.getSourceSets();
5349
sourceSets.all((sourceSet) -> {
54-
sourceSet.setCompileClasspath(sourceSet.getCompileClasspath().plus(optional));
55-
sourceSet.setRuntimeClasspath(sourceSet.getRuntimeClasspath().plus(optional));
50+
project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName())
51+
.extendsFrom(optional);
52+
project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName())
53+
.extendsFrom(optional);
5654
});
57-
project.getTasks().withType(Javadoc.class)
58-
.all((javadoc) -> javadoc.setClasspath(javadoc.getClasspath().plus(optional)));
5955
});
60-
project.getPlugins().withType(EclipsePlugin.class,
61-
(eclipsePlugin) -> project.getExtensions().getByType(EclipseModel.class)
62-
.classpath((classpath) -> classpath.getPlusConfigurations().add(optional)));
6356
}
6457

6558
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
import org.gradle.testkit.runner.BuildResult;
4444
import org.gradle.testkit.runner.GradleRunner;
4545
import org.gradle.util.GradleVersion;
46-
import org.jetbrains.kotlin.cli.common.PropertiesKt;
47-
import org.jetbrains.kotlin.compilerRunner.KotlinLogger;
48-
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient;
4946
import org.jetbrains.kotlin.gradle.model.KotlinProject;
5047
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin;
5148
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin;
@@ -107,9 +104,10 @@ private List<File> pluginClasspath() {
107104
new File("build/resources/main"), new File(pathOfJarContaining(LaunchScript.class)),
108105
new File(pathOfJarContaining(ClassVisitor.class)),
109106
new File(pathOfJarContaining(DependencyManagementPlugin.class)),
110-
new File(pathOfJarContaining(PropertiesKt.class)), new File(pathOfJarContaining(KotlinLogger.class)),
107+
new File(pathOfJarContaining("org.jetbrains.kotlin.cli.common.PropertiesKt")),
108+
new File(pathOfJarContaining("org.jetbrains.kotlin.compilerRunner.KotlinLogger")),
111109
new File(pathOfJarContaining(KotlinPlugin.class)), new File(pathOfJarContaining(KotlinProject.class)),
112-
new File(pathOfJarContaining(KotlinCompilerClient.class)),
110+
new File(pathOfJarContaining("org.jetbrains.kotlin.daemon.client.KotlinCompilerClient")),
113111
new File(pathOfJarContaining(KotlinGradleSubplugin.class)),
114112
new File(pathOfJarContaining(ArchiveEntry.class)), new File(pathOfJarContaining(BuildRequest.class)),
115113
new File(pathOfJarContaining(HttpClientConnectionManager.class)),
@@ -119,6 +117,15 @@ private List<File> pluginClasspath() {
119117
new File(pathOfJarContaining(JsonView.class)), new File(pathOfJarContaining(Platform.class)));
120118
}
121119

120+
private String pathOfJarContaining(String className) {
121+
try {
122+
return pathOfJarContaining(Class.forName(className));
123+
}
124+
catch (ClassNotFoundException ex) {
125+
throw new IllegalArgumentException(ex);
126+
}
127+
}
128+
122129
private String pathOfJarContaining(Class<?> type) {
123130
return type.getProtectionDomain().getCodeSource().getLocation().getPath();
124131
}

0 commit comments

Comments
 (0)