Skip to content

Commit

Permalink
Enforce common dependency configuration setup (elastic#78310)
Browse files Browse the repository at this point in the history
* Enforce common dependency configuration setup

* Tweak dependencies for plugin sql server tests

* Fix test runtime dependencies after disabling transitive support
  • Loading branch information
breskeby authored Sep 27, 2021
1 parent 4782cf4 commit dc3570f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;


/**
* A wrapper around Gradle's Java Base plugin that applies our
* common configuration for production code.
Expand All @@ -50,13 +50,62 @@ public void apply(Project project) {
project.getPluginManager().apply(ElasticsearchTestBasePlugin.class);
project.getPluginManager().apply(PrecommitTaskPlugin.class);

configureConfigurations(project);
configureCompile(project);
configureInputNormalization(project);

// convenience access to common versions used in dependencies
project.getExtensions().getExtraProperties().set("versions", VersionProperties.getVersions());
}

/**
* Makes dependencies non-transitive.
* <p>
* Gradle allows setting all dependencies as non-transitive very easily.
* Sadly this mechanism does not translate into maven pom generation. In order
* to effectively make the pom act as if it has no transitive dependencies,
* we must exclude each transitive dependency of each direct dependency.
* <p>
* Determining the transitive deps of a dependency which has been resolved as
* non-transitive is difficult because the process of resolving removes the
* transitive deps. To sidestep this issue, we create a configuration per
* direct dependency version. This specially named and unique configuration
* will contain all of the transitive dependencies of this particular
* dependency. We can then use this configuration during pom generation
* to iterate the transitive dependencies and add excludes.
*/
public static void configureConfigurations(Project project) {
// we are not shipping these jars, we act like dumb consumers of these things
if (project.getPath().startsWith(":test:fixtures") || project.getPath().equals(":build-tools")) {
return;
}
// fail on any conflicting dependency versions
project.getConfigurations().all(configuration -> {
if (configuration.getName().endsWith("Fixture")) {
// just a self contained test-fixture configuration, likely transitive and hellacious
return;
}
configuration.resolutionStrategy(ResolutionStrategy::failOnVersionConflict);
});

// disable transitive dependency management
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
sourceSets.all(sourceSet -> disableTransitiveDependenciesForSourceSet(project, sourceSet));
}

private static void disableTransitiveDependenciesForSourceSet(Project project, SourceSet sourceSet) {
List<String> sourceSetConfigurationNames = List.of(
sourceSet.getApiConfigurationName(),
sourceSet.getImplementationConfigurationName(),
sourceSet.getImplementationConfigurationName(),
sourceSet.getCompileOnlyConfigurationName(),
sourceSet.getRuntimeOnlyConfigurationName()
);

project.getConfigurations().matching(c -> sourceSetConfigurationNames.contains(c.getName()))
.configureEach(GradleUtils::disableTransitiveDependencies);
}

/**
* Adds compiler settings to the project
*/
Expand Down Expand Up @@ -90,13 +139,16 @@ public static void configureCompile(Project project) {
compileOptions.getRelease().set(releaseVersionProviderFromCompileTask(project, compileTask));
});
// also apply release flag to groovy, which is used in build-tools
project.getTasks().withType(GroovyCompile.class).configureEach(compileTask -> {
// TODO: this probably shouldn't apply to groovy at all?
compileTask.getOptions().getRelease().set(releaseVersionProviderFromCompileTask(project, compileTask));
});
project.getTasks()
.withType(GroovyCompile.class)
.configureEach(
compileTask -> {
// TODO: this probably shouldn't apply to groovy at all?
compileTask.getOptions().getRelease().set(releaseVersionProviderFromCompileTask(project, compileTask));
}
);
}


/**
* Apply runtime classpath input normalization so that changes in JAR manifests don't break build cacheability
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,48 +59,13 @@ public void apply(Project project) {
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
project.getPluginManager().apply(JavaLibraryPlugin.class);

configureConfigurations(project);
// configureConfigurations(project);
configureJars(project);
configureJarManifest(project);
configureJavadoc(project);
testCompileOnlyDeps(project);
}

/**
* Makes dependencies non-transitive.
* <p>
* Gradle allows setting all dependencies as non-transitive very easily.
* Sadly this mechanism does not translate into maven pom generation. In order
* to effectively make the pom act as if it has no transitive dependencies,
* we must exclude each transitive dependency of each direct dependency.
* <p>
* Determining the transitive deps of a dependency which has been resolved as
* non-transitive is difficult because the process of resolving removes the
* transitive deps. To sidestep this issue, we create a configuration per
* direct dependency version. This specially named and unique configuration
* will contain all of the transitive dependencies of this particular
* dependency. We can then use this configuration during pom generation
* to iterate the transitive dependencies and add excludes.
*/
public static void configureConfigurations(Project project) {
// we are not shipping these jars, we act like dumb consumers of these things
if (project.getPath().startsWith(":test:fixtures") || project.getPath().equals(":build-tools")) {
return;
}
// fail on any conflicting dependency versions
project.getConfigurations().all(configuration -> {
if (configuration.getName().endsWith("Fixture")) {
// just a self contained test-fixture configuration, likely transitive and hellacious
return;
}
configuration.resolutionStrategy(ResolutionStrategy::failOnVersionConflict);
});

// disable transitive dependency management
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
sourceSets.all(sourceSet -> disableTransitiveDependenciesForSourceSet(project, sourceSet));
}

private static void testCompileOnlyDeps(Project project) {
// we want to test compileOnly deps!
Configuration compileOnlyConfig = project.getConfigurations().getByName(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME);
Expand Down Expand Up @@ -192,17 +157,4 @@ private static void configureJavadoc(Project project) {
project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(t -> t.dependsOn(javadoc));
}

private static void disableTransitiveDependenciesForSourceSet(Project project, SourceSet sourceSet) {
Stream.of(
sourceSet.getApiConfigurationName(),
sourceSet.getImplementationConfigurationName(),
sourceSet.getImplementationConfigurationName(),
sourceSet.getCompileOnlyConfigurationName(),
sourceSet.getRuntimeOnlyConfigurationName()
)
.map(name -> project.getConfigurations().findByName(name))
.filter(Objects::nonNull)
.forEach(GradleUtils::disableTransitiveDependencies);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.gradle.internal.test.rest;

import org.elasticsearch.gradle.internal.ElasticsearchJavaPlugin;
import org.elasticsearch.gradle.internal.test.RestTestBasePlugin;
import org.elasticsearch.gradle.util.GradleUtils;
import org.gradle.api.Plugin;
Expand All @@ -31,8 +30,6 @@ public void apply(Project project) {
project.getPluginManager().apply(RestTestBasePlugin.class);
project.getPluginManager().apply(RestResourcesPlugin.class);

ElasticsearchJavaPlugin.configureConfigurations(project);

// create source set
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
SourceSet yamlTestSourceSet = sourceSets.create(SOURCE_SET_NAME);
Expand Down
2 changes: 1 addition & 1 deletion qa/smoke-test-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ apply plugin: 'elasticsearch.rest-test'
apply plugin: 'elasticsearch.test-with-dependencies'

dependencies {
testImplementation "com.fasterxml.jackson.core:jackson-databind:2.8.11"
testImplementation "com.fasterxml.jackson.core:jackson-databind:2.10.4"
testImplementation project(':modules:transport-netty4') // for http
testImplementation project(':plugins:transport-nio') // for http
}
Expand Down
11 changes: 7 additions & 4 deletions x-pack/plugin/sql/qa/server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ subprojects {
testRuntimeOnly "com.h2database:h2:${h2Version}"

// H2GIS testing dependencies
testRuntimeOnly("org.orbisgis:h2gis:${h2gisVersion}") {
exclude group: "org.locationtech.jts"
exclude group: "com.fasterxml.jackson.core"
}
testRuntimeOnly("org.orbisgis:h2gis:${h2gisVersion}")
testRuntimeOnly("org.orbisgis:h2gis-api:${h2gisVersion}")
testRuntimeOnly("org.orbisgis:h2gis-utilities:${h2gisVersion}")
testRuntimeOnly("org.orbisgis:cts:1.5.2")


testRuntimeOnly project(path: xpackModule('sql:jdbc'))
testRuntimeOnly xpackProject('plugin:sql:sql-client')
Expand All @@ -99,6 +100,8 @@ subprojects {

// spatial dependency
testRuntimeOnly project(path: xpackModule('spatial'))

testRuntimeOnly "org.slf4j:slf4j-api:1.7.25"
}

if (project.name != 'security') {
Expand Down
5 changes: 5 additions & 0 deletions x-pack/qa/security-tools-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ dependencies {
testRuntimeOnly "com.google.guava:guava:${versions.jimfs_guava}"
}

configurations.all {
resolutionStrategy {
forcedModules = ["com.google.guava:guava:${versions.jimfs_guava}"]
}
}
// add test resources from security, so certificate tool tests can use example certs
tasks.named("processTestResources").configure {
from(project(xpackModule('security')).sourceSets.test.resources.srcDirs)
Expand Down

0 comments on commit dc3570f

Please sign in to comment.