Просмотр исходного кода

Enforce common dependency configuration setup (#78310)

* Enforce common dependency configuration setup

* Tweak dependencies for plugin sql server tests

* Fix test runtime dependencies after disabling transitive support
Rene Groeschke 4 лет назад
Родитель
Сommit
dc3570f9c9

+ 58 - 6
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchJavaBasePlugin.java

@@ -32,9 +32,9 @@ import org.gradle.api.tasks.compile.JavaCompile;
 
 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.
@@ -50,6 +50,7 @@ public class ElasticsearchJavaBasePlugin implements Plugin<Project> {
         project.getPluginManager().apply(ElasticsearchTestBasePlugin.class);
         project.getPluginManager().apply(PrecommitTaskPlugin.class);
 
+        configureConfigurations(project);
         configureCompile(project);
         configureInputNormalization(project);
 
@@ -57,6 +58,54 @@ public class ElasticsearchJavaBasePlugin implements Plugin<Project> {
         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
      */
@@ -90,13 +139,16 @@ public class ElasticsearchJavaBasePlugin implements Plugin<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
      */

+ 1 - 49
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchJavaPlugin.java

@@ -59,48 +59,13 @@ public class ElasticsearchJavaPlugin implements Plugin<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);
@@ -192,17 +157,4 @@ public class ElasticsearchJavaPlugin implements Plugin<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);
-    }
-
 }

+ 0 - 3
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/InternalYamlRestTestPlugin.java

@@ -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;
@@ -31,8 +30,6 @@ public class InternalYamlRestTestPlugin implements Plugin<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);

+ 1 - 1
qa/smoke-test-http/build.gradle

@@ -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
 }

+ 7 - 4
x-pack/plugin/sql/qa/server/build.gradle

@@ -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')
@@ -99,6 +100,8 @@ subprojects {
 
     // spatial dependency
     testRuntimeOnly project(path: xpackModule('spatial'))
+
+    testRuntimeOnly "org.slf4j:slf4j-api:1.7.25"
   }
 
   if (project.name != 'security') {

+ 5 - 0
x-pack/qa/security-tools-tests/build.gradle

@@ -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)