Bläddra i källkod

Introduce EnforceDeprecationFailuresPlugin (#58263)

- extract fail on deprecated usage into its own plugin
- apply on all projects
- ensures we don't miss any project (missed xpack/plugin/eql/qa/security before)
Rene Groeschke 5 år sedan
förälder
incheckning
1c284748b1

+ 2 - 0
build.gradle

@@ -426,6 +426,8 @@ gradle.projectsEvaluated {
 }
 
 allprojects {
+  apply plugin: 'elasticsearch.enforce-deprecation-use-failures'
+
   tasks.register('resolveAllDependencies') {
     if (project.path.contains("fixture")) {
       dependsOn tasks.withType(ComposePull)

+ 3 - 0
buildSrc/build.gradle

@@ -131,6 +131,9 @@ dependencies {
   testFixturesApi gradleTestKit()
   testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone:2.23.2'
   testImplementation 'org.mockito:mockito-core:1.9.5'
+  integTestImplementation('org.spockframework:spock-core:1.3-groovy-2.5') {
+    exclude module:"groovy"
+  }
   minimumRuntimeCompile "junit:junit:${props.getProperty('junit')}"
   minimumRuntimeCompile localGroovy()
   minimumRuntimeCompile gradleApi()

+ 116 - 0
buildSrc/src/integTest/groovy/org/elasticsearch/gradle/EnforceDeprecationFailuresPluginFuncTest.groovy

@@ -0,0 +1,116 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.elasticsearch.gradle
+
+import org.gradle.testkit.runner.GradleRunner
+import org.junit.Rule
+import org.junit.rules.TemporaryFolder
+import spock.lang.Specification
+
+import java.lang.management.ManagementFactory
+
+class EnforceDeprecationFailuresPluginFuncTest extends Specification {
+
+    @Rule
+    TemporaryFolder testProjectDir = new TemporaryFolder()
+
+    File settingsFile
+    File buildFile
+
+    def setup() {
+        settingsFile = testProjectDir.newFile('settings.gradle')
+        settingsFile << "rootProject.name = 'hello-world'"
+        buildFile = testProjectDir.newFile('build.gradle')
+        buildFile << """plugins {
+            id 'elasticsearch.enforce-deprecation-use-failures'
+        }
+        """
+    }
+
+    def "fails on testCompile resolution"() {
+        given:
+        buildFile << """
+            apply plugin:'java'
+            dependencies {
+                compile "org.acme:some-lib:1.0"
+            }
+
+            task resolve {
+                doLast {
+                    configurations.testCompile.resolve()
+                }
+            }
+            """
+        when:
+        def result = gradleRunner("resolve").buildAndFail()
+        then:
+        assertOutputContains(result.output, """
+* What went wrong:
+Execution failed for task ':resolve'.
+> Resolving configuration testCompile is no longer supported. Use testImplementation instead.
+""")
+    }
+
+    def "fails on testCompile dependency declaration"() {
+        given:
+        buildFile << """
+            apply plugin:'java-base'
+            sourceSets {
+                test
+            }
+
+            dependencies {
+                testCompile "org.acme:some-lib:1.0"
+            }
+
+            task resolve {
+                doLast {
+                    configurations.testCompile.resolve()
+                }
+            }
+            """
+        when:
+        def result = gradleRunner("resolve").buildAndFail()
+        then:
+        assertOutputContains(result.output, """
+* What went wrong:
+Execution failed for task ':resolve'.
+> Declaring dependencies for configuration testCompile is no longer supported. Use testImplementation instead.
+""")
+    }
+
+    private GradleRunner gradleRunner(String... arguments) {
+        GradleRunner.create()
+            .withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
+            .withProjectDir(testProjectDir.root)
+            .withArguments(arguments)
+            .withPluginClasspath()
+            .forwardOutput()
+    }
+
+    def assertOutputContains(String givenOutput, String expected) {
+        assert normalizedString(givenOutput).contains(normalizedString(expected))
+        true
+    }
+
+    String normalizedString(String input) {
+        return input.readLines().join("\n")
+    }
+}

+ 0 - 15
buildSrc/src/main/java/org/elasticsearch/gradle/ElasticsearchJavaPlugin.java

@@ -117,21 +117,6 @@ public class ElasticsearchJavaPlugin implements Plugin<Project> {
         Configuration testImplementationConfig = project.getConfigurations().getByName(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME);
         testImplementationConfig.extendsFrom(compileOnlyConfig);
 
-        // fail on using deprecated testCompile
-        project.getConfigurations()
-            .getByName(JavaPlugin.TEST_COMPILE_CONFIGURATION_NAME)
-            .getIncoming()
-            .beforeResolve(resolvableDependencies -> {
-                if (resolvableDependencies.getDependencies().size() > 0) {
-                    throw new GradleException(
-                        "Usage of configuration "
-                            + JavaPlugin.TEST_COMPILE_CONFIGURATION_NAME
-                            + " is no longer supported. Use "
-                            + JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME
-                            + " instead."
-                    );
-                }
-            });
         // 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;

+ 93 - 0
buildSrc/src/main/java/org/elasticsearch/gradle/EnforceDeprecationFailuresPlugin.java

@@ -0,0 +1,93 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.elasticsearch.gradle;
+
+import org.gradle.api.GradleException;
+import org.gradle.api.Plugin;
+import org.gradle.api.Project;
+import org.gradle.api.plugins.JavaBasePlugin;
+import org.gradle.api.tasks.SourceSet;
+import org.gradle.api.tasks.SourceSetContainer;
+
+/**
+ * This plugin enforces build failure if certain deprecated apis are used.
+ * <p>
+ * For now we support build failure on:
+ * - declaring dependencies for testCompile
+ * - resolving testCompile configuration
+ */
+public class EnforceDeprecationFailuresPlugin implements Plugin<Project> {
+
+    private Project project;
+
+    @Override
+    public void apply(Project project) {
+        this.project = project;
+        handleDeprecatedConfigurations();
+    }
+
+    private void handleDeprecatedConfigurations() {
+        project.getPlugins().withType(JavaBasePlugin.class, javaBasePlugin -> {
+            SourceSetContainer sourceSetContainer = project.getExtensions().getByType(SourceSetContainer.class);
+            sourceSetContainer.all(
+                sourceSet -> {
+                    // TODO: remove that guard once we removed general compile usage from es build
+                    if (sourceSet.getName().equals("test")) {
+                        failOnCompileConfigurationResolution(sourceSet);
+                        failOnCompileConfigurationDependencyDeclaration(sourceSet);
+                    }
+                }
+            );
+        });
+    }
+
+    private void failOnCompileConfigurationDependencyDeclaration(SourceSet sourceSet) {
+        // fail on using deprecated testCompile
+        project.getConfigurations().getByName(sourceSet.getCompileConfigurationName()).withDependencies(dependencies -> {
+            if (dependencies.size() > 0) {
+                throw new GradleException(
+                    "Declaring dependencies for configuration "
+                        + sourceSet.getCompileConfigurationName()
+                        + " is no longer supported. Use "
+                        + sourceSet.getImplementationConfigurationName()
+                        + " instead."
+                );
+            }
+        });
+    }
+
+    private void failOnCompileConfigurationResolution(SourceSet sourceSet) {
+        project.getConfigurations()
+            .getByName(sourceSet.getCompileConfigurationName())
+            .getIncoming()
+            .beforeResolve(resolvableDependencies -> {
+                if (resolvableDependencies.getDependencies().size() > 0) {
+                    throw new GradleException(
+                        "Resolving configuration "
+                            + sourceSet.getCompileConfigurationName()
+                            + " is no longer supported. Use "
+                            + sourceSet.getImplementationConfigurationName()
+                            + " instead."
+                    );
+                }
+            });
+    }
+
+}

+ 19 - 0
buildSrc/src/main/resources/META-INF/gradle-plugins/elasticsearch.enforce-deprecation-use-failures.properties

@@ -0,0 +1,19 @@
+#
+# Licensed to Elasticsearch under one or more contributor
+# license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright
+# ownership. Elasticsearch licenses this file to you under
+# the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+implementation-class=org.elasticsearch.gradle.EnforceDeprecationFailuresPlugin