Skip to content

Commit

Permalink
Merge branch '2.4.x' into 2.5.x
Browse files Browse the repository at this point in the history
Closes gh-27966
  • Loading branch information
wilkinsona committed Sep 13, 2021
2 parents 2004c06 + 273600b commit f530819
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 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.
Expand All @@ -19,19 +19,15 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.attributes.Usage;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
import org.gradle.plugins.ide.eclipse.model.EclipseModel;

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

@Override
public void apply(Project project) {
Configuration optional = project.getConfigurations().create(OPTIONAL_CONFIGURATION_NAME);
optional.attributes((attributes) -> attributes.attribute(Usage.USAGE_ATTRIBUTE,
project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)));
Configuration optional = project.getConfigurations().create("optional");
optional.setCanBeConsumed(false);
optional.setCanBeResolved(false);
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets();
sourceSets.all((sourceSet) -> {
sourceSet.setCompileClasspath(sourceSet.getCompileClasspath().plus(optional));
sourceSet.setRuntimeClasspath(sourceSet.getRuntimeClasspath().plus(optional));
project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName())
.extendsFrom(optional);
project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName())
.extendsFrom(optional);
});
project.getTasks().withType(Javadoc.class)
.all((javadoc) -> javadoc.setClasspath(javadoc.getClasspath().plus(optional)));
});
project.getPlugins().withType(EclipsePlugin.class,
(eclipsePlugin) -> project.getExtensions().getByType(EclipseModel.class)
.classpath((classpath) -> classpath.getPlusConfigurations().add(optional)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.util.GradleVersion;
import org.jetbrains.kotlin.cli.common.PropertiesKt;
import org.jetbrains.kotlin.compilerRunner.KotlinLogger;
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient;
import org.jetbrains.kotlin.gradle.model.KotlinProject;
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin;
Expand Down Expand Up @@ -110,9 +107,10 @@ private List<File> pluginClasspath() {
new File("build/resources/main"), new File(pathOfJarContaining(LaunchScript.class)),
new File(pathOfJarContaining(ClassVisitor.class)),
new File(pathOfJarContaining(DependencyManagementPlugin.class)),
new File(pathOfJarContaining(PropertiesKt.class)), new File(pathOfJarContaining(KotlinLogger.class)),
new File(pathOfJarContaining("org.jetbrains.kotlin.cli.common.PropertiesKt")),
new File(pathOfJarContaining("org.jetbrains.kotlin.compilerRunner.KotlinLogger")),
new File(pathOfJarContaining(KotlinPlugin.class)), new File(pathOfJarContaining(KotlinProject.class)),
new File(pathOfJarContaining(KotlinCompilerClient.class)),
new File(pathOfJarContaining("org.jetbrains.kotlin.daemon.client.KotlinCompilerClient")),
new File(pathOfJarContaining(KotlinCompilerPluginSupportPlugin.class)),
new File(pathOfJarContaining(LanguageSettings.class)),
new File(pathOfJarContaining(ArchiveEntry.class)), new File(pathOfJarContaining(BuildRequest.class)),
Expand All @@ -124,6 +122,15 @@ private List<File> pluginClasspath() {
new File(pathOfJarContaining(Toml.class)), new File(pathOfJarContaining(Lexer.class)));
}

private String pathOfJarContaining(String className) {
try {
return pathOfJarContaining(Class.forName(className));
}
catch (ClassNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
}

private String pathOfJarContaining(Class<?> type) {
return type.getProtectionDomain().getCodeSource().getLocation().getPath();
}
Expand Down

0 comments on commit f530819

Please sign in to comment.