Fix optional dependencies having compile scope in poms

Closes gh-729
This commit is contained in:
Andy Wilkinson
2021-06-14 21:42:06 +01:00
parent c0477a6446
commit bf1bfc4075
6 changed files with 30 additions and 10 deletions

View File

@@ -19,13 +19,16 @@ package org.springframework.restdocs.build.optional;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.attributes.Bundling;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.attributes.Usage;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
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
@@ -45,13 +48,26 @@ public class OptionalDependenciesPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
Configuration optional = project.getConfigurations().create(OPTIONAL_CONFIGURATION_NAME);
project.getConfigurations().getByName(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().getByName(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().getByName(JavaPlugin.TEST_COMPILE_CLASSPATH_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().getByName(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().getByName(JavaPlugin.API_ELEMENTS_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().getByName(JavaPlugin.RUNTIME_ELEMENTS_CONFIGURATION_NAME).extendsFrom(optional);
project.getConfigurations().all((configuration) -> {
if (configuration.getName().startsWith("testRuntimeClasspath_")) {
configuration.extendsFrom(optional);
}
});
optional.attributes((attributes) -> attributes.attribute(Usage.USAGE_ATTRIBUTE,
project.getObjects().named(Usage.class, Usage.JAVA_RUNTIME)));
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.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)));
}
}

View File

@@ -18,10 +18,10 @@ dependencies {
merge project(":spring-restdocs-asciidoctor-1.6")
merge project(":spring-restdocs-asciidoctor-2.x")
testImplementation("junit:junit")
testImplementation("org.apache.pdfbox:pdfbox")
testImplementation("org.asciidoctor:asciidoctorj:1.5.8.1")
testImplementation("org.assertj:assertj-core")
testImplementation("junit:junit")
testImplementation("org.springframework:spring-core")
testRuntimeOnly("org.asciidoctor:asciidoctorj-pdf")

View File

@@ -43,6 +43,7 @@ dependencies {
jmustache(platform(project(":spring-restdocs-platform")))
jmustache("com.samskivert:jmustache@jar")
optional(platform(project(":spring-restdocs-platform")))
optional("commons-codec:commons-codec")
optional("javax.validation:validation-api")
optional("junit:junit")

View File

@@ -17,6 +17,7 @@ dependencies {
internal(platform(project(":spring-restdocs-platform")))
testImplementation(project(path: ":spring-restdocs-core", configuration: "testArtifacts"))
testImplementation("junit:junit")
testImplementation("org.assertj:assertj-core")
testImplementation("org.hamcrest:hamcrest-library")
testImplementation("org.mockito:mockito-core")

View File

@@ -16,6 +16,7 @@ dependencies {
testImplementation(project(path: ":spring-restdocs-core", configuration: "testArtifacts"))
testImplementation("com.fasterxml.jackson.core:jackson-databind")
testImplementation("junit:junit")
testImplementation("org.apache.tomcat.embed:tomcat-embed-core:8.5.13")
testImplementation("org.assertj:assertj-core")
testImplementation("org.hamcrest:hamcrest-library")

View File

@@ -14,6 +14,7 @@ dependencies {
internal(platform(project(":spring-restdocs-platform")))
testImplementation(project(path: ":spring-restdocs-core", configuration: "testArtifacts"))
testImplementation("junit:junit")
testImplementation("org.assertj:assertj-core")
testImplementation("org.hamcrest:hamcrest-library")
testImplementation("org.mockito:mockito-core")