Răsfoiți Sursa

gradle rest resources plugin clean up (#68174)

This commit cleans up some of the rest resources to honor best practices.

These changes include:
* remove SourceSet from copy api/test tasks and move the logic to the plugin.
  * this allows the tasks talk in simpler terms in File and FileCollections.
* prefer constructor injection over method injection for gradle components
* rename tasks to better match usage:
   * copyRestApiCompatSpecsTask -> copyRestCompatApiTask
   * copyRestApiCompatTestTask -> copyRestCompatTestTask
* remove no longer valid TODO's
Jake Landis 4 ani în urmă
părinte
comite
0d404f3207

+ 5 - 5
buildSrc/src/integTest/groovy/org/elasticsearch/gradle/RestResourcesPluginFuncTest.groovy

@@ -28,8 +28,8 @@ class RestResourcesPluginFuncTest extends AbstractRestResourcesFuncTest {
         given:
         internalBuild()
         buildFile << """
-            apply plugin: 'elasticsearch.rest-resources'
             apply plugin: 'elasticsearch.java'
+            apply plugin: 'elasticsearch.rest-resources'
         """
 
         String api = "foo.json"
@@ -47,8 +47,8 @@ class RestResourcesPluginFuncTest extends AbstractRestResourcesFuncTest {
         given:
         internalBuild()
         buildFile << """
-           apply plugin: 'elasticsearch.rest-resources'
            apply plugin: 'elasticsearch.java'
+           apply plugin: 'elasticsearch.rest-resources'
         """
         String api = "foo.json"
         setupRestResources([api])
@@ -67,8 +67,8 @@ class RestResourcesPluginFuncTest extends AbstractRestResourcesFuncTest {
         given:
         internalBuild()
         buildFile << """
-            apply plugin: 'elasticsearch.rest-resources'
             apply plugin: 'elasticsearch.java'
+            apply plugin: 'elasticsearch.rest-resources'
 
             restResources {
                 restTests {
@@ -103,8 +103,8 @@ class RestResourcesPluginFuncTest extends AbstractRestResourcesFuncTest {
         given:
         internalBuild()
         buildFile << """
-            apply plugin: 'elasticsearch.rest-resources'
             apply plugin: 'elasticsearch.java'
+            apply plugin: 'elasticsearch.rest-resources'
 
             restResources {
                 restApi {
@@ -136,8 +136,8 @@ class RestResourcesPluginFuncTest extends AbstractRestResourcesFuncTest {
         given:
         internalBuild()
         buildFile << """
-            apply plugin: 'elasticsearch.rest-resources'
             apply plugin: 'elasticsearch.java'
+            apply plugin: 'elasticsearch.rest-resources'
 
             restResources {
                 restApi {

+ 10 - 10
buildSrc/src/integTest/groovy/org/elasticsearch/gradle/YamlRestCompatTestPluginFuncTest.groovy

@@ -48,8 +48,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
 
         then:
         result.task(':yamlRestCompatTest').outcome == TaskOutcome.NO_SOURCE
-        result.task(':copyRestApiCompatSpecsTask').outcome == TaskOutcome.NO_SOURCE
-        result.task(':copyRestApiCompatTestTask').outcome == TaskOutcome.NO_SOURCE
+        result.task(':copyRestCompatApiTask').outcome == TaskOutcome.NO_SOURCE
+        result.task(':copyRestCompatTestTask').outcome == TaskOutcome.NO_SOURCE
         result.task(':transformCompatTests').outcome == TaskOutcome.NO_SOURCE
     }
 
@@ -100,8 +100,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
 
         then:
         result.task(':yamlRestCompatTest').outcome == TaskOutcome.SKIPPED
-        result.task(':copyRestApiCompatSpecsTask').outcome == TaskOutcome.SUCCESS
-        result.task(':copyRestApiCompatTestTask').outcome == TaskOutcome.SUCCESS
+        result.task(':copyRestCompatApiTask').outcome == TaskOutcome.SUCCESS
+        result.task(':copyRestCompatTestTask').outcome == TaskOutcome.SUCCESS
         result.task(':transformCompatTests').outcome == TaskOutcome.SUCCESS
 
         file("/build/resources/yamlRestCompatTest/rest-api-spec/api/" + api).exists()
@@ -128,8 +128,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
 
         then:
         result.task(':yamlRestCompatTest').outcome == TaskOutcome.SKIPPED
-        result.task(':copyRestApiCompatSpecsTask').outcome == TaskOutcome.UP_TO_DATE
-        result.task(':copyRestApiCompatTestTask').outcome == TaskOutcome.UP_TO_DATE
+        result.task(':copyRestCompatApiTask').outcome == TaskOutcome.UP_TO_DATE
+        result.task(':copyRestCompatTestTask').outcome == TaskOutcome.UP_TO_DATE
         result.task(':transformCompatTests').outcome == TaskOutcome.UP_TO_DATE
     }
 
@@ -157,8 +157,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
         result.task(':check').outcome == TaskOutcome.UP_TO_DATE
         result.task(':checkRestCompat').outcome == TaskOutcome.UP_TO_DATE
         result.task(':yamlRestCompatTest').outcome == TaskOutcome.NO_SOURCE
-        result.task(':copyRestApiCompatSpecsTask').outcome == TaskOutcome.NO_SOURCE
-        result.task(':copyRestApiCompatTestTask').outcome == TaskOutcome.NO_SOURCE
+        result.task(':copyRestCompatApiTask').outcome == TaskOutcome.NO_SOURCE
+        result.task(':copyRestCompatTestTask').outcome == TaskOutcome.NO_SOURCE
         result.task(':transformCompatTests').outcome == TaskOutcome.NO_SOURCE
 
         when:
@@ -171,8 +171,8 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
         result.task(':check').outcome == TaskOutcome.UP_TO_DATE
         result.task(':checkRestCompat').outcome == TaskOutcome.UP_TO_DATE
         result.task(':yamlRestCompatTest').outcome == TaskOutcome.SKIPPED
-        result.task(':copyRestApiCompatSpecsTask').outcome == TaskOutcome.SKIPPED
-        result.task(':copyRestApiCompatTestTask').outcome == TaskOutcome.SKIPPED
+        result.task(':copyRestCompatApiTask').outcome == TaskOutcome.SKIPPED
+        result.task(':copyRestCompatTestTask').outcome == TaskOutcome.SKIPPED
         result.task(':transformCompatTests').outcome == TaskOutcome.SKIPPED
 
     }

+ 15 - 8
buildSrc/src/main/java/org/elasticsearch/gradle/internal/rest/compat/YamlRestCompatTestPlugin.java

@@ -55,8 +55,6 @@ import static org.elasticsearch.gradle.test.rest.RestTestUtil.setupDependencies;
 /**
  * Apply this plugin to run the YAML based REST tests from a prior major version against this version's cluster.
  */
-// TODO: support running tests against multiple prior versions in addition to bwc:minor. To achieve this we will need to create published
-// artifacts that include all of the REST tests for the latest 7.x.y releases
 public class YamlRestCompatTestPlugin implements Plugin<Project> {
 
     public static final String REST_COMPAT_CHECK_TASK_NAME = "checkRestCompat";
@@ -66,7 +64,7 @@ public class YamlRestCompatTestPlugin implements Plugin<Project> {
     private static final Path RELATIVE_REST_API_RESOURCES = Path.of("rest-api-spec/src/main/resources");
     private static final Path RELATIVE_REST_XPACK_RESOURCES = Path.of("x-pack/plugin/src/test/resources");
     private static final Path RELATIVE_REST_PROJECT_RESOURCES = Path.of("src/yamlRestTest/resources");
-    public static final String TEST_INTERMEDIATE_DIR_NAME = "v"
+    private static final String TEST_INTERMEDIATE_DIR_NAME = "v"
         + (Version.fromString(VersionProperties.getVersions().get("elasticsearch")).getMajor() - 1)
         + "restTests";
 
@@ -97,14 +95,22 @@ public class YamlRestCompatTestPlugin implements Plugin<Project> {
         project.getDependencies().add(bwcMinorConfig.getName(), bwcMinor);
 
         Provider<CopyRestApiTask> copyCompatYamlSpecTask = project.getTasks()
-            .register("copyRestApiCompatSpecsTask", CopyRestApiTask.class, task -> {
+            .register("copyRestCompatApiTask", CopyRestApiTask.class, task -> {
                 task.dependsOn(bwcMinorConfig);
                 task.setCoreConfig(bwcMinorConfig);
                 task.setXpackConfig(bwcMinorConfig);
                 task.setAdditionalConfig(bwcMinorConfig);
                 task.getIncludeCore().set(extension.getRestApi().getIncludeCore());
                 task.getIncludeXpack().set(extension.getRestApi().getIncludeXpack());
-                task.setSourceSetName(SOURCE_SET_NAME);
+                task.setOutputResourceDir(yamlCompatTestSourceSet.getOutput().getResourcesDir());
+                task.setSourceResourceDir(
+                    yamlCompatTestSourceSet.getResources()
+                        .getSrcDirs()
+                        .stream()
+                        .filter(f -> f.isDirectory() && f.getName().equals("resources"))
+                        .findFirst()
+                        .orElse(null)
+                );
                 task.setSkipHasRestTestCheck(true);
                 task.setCoreConfigToFileTree(
                     config -> project.fileTree(
@@ -127,14 +133,16 @@ public class YamlRestCompatTestPlugin implements Plugin<Project> {
 
         // copy compatible rest tests
         Provider<CopyRestTestsTask> copyCompatYamlTestTask = project.getTasks()
-            .register("copyRestApiCompatTestTask", CopyRestTestsTask.class, task -> {
+            .register("copyRestCompatTestTask", CopyRestTestsTask.class, task -> {
                 task.dependsOn(bwcMinorConfig);
                 task.setCoreConfig(bwcMinorConfig);
                 task.setXpackConfig(bwcMinorConfig);
                 task.setAdditionalConfig(bwcMinorConfig);
                 task.getIncludeCore().set(extension.getRestTests().getIncludeCore());
                 task.getIncludeXpack().set(extension.getRestTests().getIncludeXpack());
-                task.setSourceSetName(SOURCE_SET_NAME);
+                File resourceDir = yamlCompatTestSourceSet.getOutput().getResourcesDir();
+                File intermediateDir = new File(resourceDir, TEST_INTERMEDIATE_DIR_NAME);
+                task.setOutputResourceDir(intermediateDir);
                 task.setCoreConfigToFileTree(
                     config -> project.fileTree(
                         config.getSingleFile().toPath().resolve(RELATIVE_REST_API_RESOURCES).resolve(RELATIVE_TEST_PATH)
@@ -152,7 +160,6 @@ public class YamlRestCompatTestPlugin implements Plugin<Project> {
                     )
                 );
                 task.dependsOn(copyCompatYamlSpecTask);
-                task.setOutputResourceRoot(TEST_INTERMEDIATE_DIR_NAME);
                 task.onlyIf(t -> isEnabled(project));
             });
 

+ 37 - 83
buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/CopyRestApiTask.java

@@ -20,22 +20,18 @@ package org.elasticsearch.gradle.test.rest;
 
 import org.elasticsearch.gradle.VersionProperties;
 import org.elasticsearch.gradle.info.BuildParams;
-import org.elasticsearch.gradle.util.GradleUtils;
 import org.gradle.api.DefaultTask;
-import org.gradle.api.Project;
 import org.gradle.api.file.ArchiveOperations;
 import org.gradle.api.file.FileCollection;
 import org.gradle.api.file.FileSystemOperations;
 import org.gradle.api.file.FileTree;
 import org.gradle.api.file.ProjectLayout;
-import org.gradle.api.plugins.JavaPluginConvention;
 import org.gradle.api.provider.ListProperty;
 import org.gradle.api.tasks.Input;
 import org.gradle.api.tasks.InputFiles;
 import org.gradle.api.tasks.Internal;
 import org.gradle.api.tasks.OutputDirectory;
 import org.gradle.api.tasks.SkipWhenEmpty;
-import org.gradle.api.tasks.SourceSet;
 import org.gradle.api.tasks.TaskAction;
 import org.gradle.api.tasks.util.PatternFilterable;
 import org.gradle.api.tasks.util.PatternSet;
@@ -46,8 +42,6 @@ import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.util.Objects;
-import java.util.Optional;
-import java.util.Set;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -57,13 +51,17 @@ import static org.elasticsearch.gradle.util.GradleUtils.getProjectPathFromTask;
  * Copies the files needed for the Rest YAML specs to the current projects test resources output directory.
  * This is intended to be be used from {@link RestResourcesPlugin} since the plugin wires up the needed
  * configurations and custom extensions.
+ *
  * @see RestResourcesPlugin
  */
 public class CopyRestApiTask extends DefaultTask {
     private static final String REST_API_PREFIX = "rest-api-spec/api";
+    private static final String REST_TEST_PREFIX = "rest-api-spec/test";
     private final ListProperty<String> includeCore = getProject().getObjects().listProperty(String.class);
     private final ListProperty<String> includeXpack = getProject().getObjects().listProperty(String.class);
-    private String sourceSetName;
+
+    private File outputResourceDir;
+    private File sourceResourceDir;
     private boolean skipHasRestTestCheck;
     private FileCollection coreConfig;
     private FileCollection xpackConfig;
@@ -75,27 +73,21 @@ public class CopyRestApiTask extends DefaultTask {
     private final PatternFilterable corePatternSet;
     private final PatternFilterable xpackPatternSet;
     private final ProjectLayout projectLayout;
+    private final FileSystemOperations fileSystemOperations;
+    private final ArchiveOperations archiveOperations;
 
     @Inject
-    public CopyRestApiTask(ProjectLayout projectLayout) {
-        corePatternSet = getPatternSetFactory().create();
-        xpackPatternSet = getPatternSetFactory().create();
+    public CopyRestApiTask(
+        ProjectLayout projectLayout,
+        Factory<PatternSet> patternSetFactory,
+        FileSystemOperations fileSystemOperations,
+        ArchiveOperations archiveOperations
+    ) {
+        corePatternSet = patternSetFactory.create();
+        xpackPatternSet = patternSetFactory.create();
         this.projectLayout = projectLayout;
-    }
-
-    @Inject
-    protected Factory<PatternSet> getPatternSetFactory() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Inject
-    protected FileSystemOperations getFileSystemOperations() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Inject
-    protected ArchiveOperations getArchiveOperations() {
-        throw new UnsupportedOperationException();
+        this.fileSystemOperations = fileSystemOperations;
+        this.archiveOperations = archiveOperations;
     }
 
     @Input
@@ -108,11 +100,6 @@ public class CopyRestApiTask extends DefaultTask {
         return includeXpack;
     }
 
-    @Input
-    String getSourceSetName() {
-        return sourceSetName;
-    }
-
     @Input
     public boolean isSkipHasRestTestCheck() {
         return skipHasRestTestCheck;
@@ -149,12 +136,7 @@ public class CopyRestApiTask extends DefaultTask {
 
     @OutputDirectory
     public File getOutputDir() {
-        return new File(
-            getSourceSet().orElseThrow(() -> new IllegalArgumentException("could not find source set [" + sourceSetName + "]"))
-                .getOutput()
-                .getResourcesDir(),
-            REST_API_PREFIX
-        );
+        return new File(outputResourceDir, REST_API_PREFIX);
     }
 
     @TaskAction
@@ -163,7 +145,7 @@ public class CopyRestApiTask extends DefaultTask {
         String projectPath = getProjectPathFromTask(getPath());
         if (BuildParams.isInternal()) {
             getLogger().debug("Rest specs for project [{}] will be copied to the test resources.", projectPath);
-            getFileSystemOperations().copy(c -> {
+            fileSystemOperations.copy(c -> {
                 c.from(coreConfigToFileTree.apply(coreConfig));
                 c.into(getOutputDir());
                 c.include(corePatternSet.getIncludes());
@@ -174,10 +156,9 @@ public class CopyRestApiTask extends DefaultTask {
                 projectPath,
                 VersionProperties.getElasticsearch()
             );
-            getFileSystemOperations().copy(c -> {
-                c.from(getArchiveOperations().zipTree(coreConfig.getSingleFile())); // jar file
-                // this ends up as the same dir as outputDir
-                c.into(Objects.requireNonNull(getSourceSet().orElseThrow().getOutput().getResourcesDir()));
+            fileSystemOperations.copy(c -> {
+                c.from(archiveOperations.zipTree(coreConfig.getSingleFile())); // jar file
+                c.into(Objects.requireNonNull(outputResourceDir));
                 if (includeCore.get().isEmpty()) {
                     c.include(REST_API_PREFIX + "/**");
                 } else {
@@ -190,16 +171,16 @@ public class CopyRestApiTask extends DefaultTask {
         // only copy x-pack specs if explicitly instructed
         if (includeXpack.get().isEmpty() == false) {
             getLogger().debug("X-pack rest specs for project [{}] will be copied to the test resources.", projectPath);
-            getFileSystemOperations().copy(c -> {
+            fileSystemOperations.copy(c -> {
                 c.from(xpackConfigToFileTree.apply(xpackConfig));
                 c.into(getOutputDir());
                 c.include(xpackPatternSet.getIncludes());
             });
         }
-        // TODO: once https://github.com/elastic/elasticsearch/pull/62968 lands ensure that this uses `getFileSystemOperations()`
+
         // copy any additional config
         if (additionalConfig != null) {
-            getFileSystemOperations().copy(c -> {
+            fileSystemOperations.copy(c -> {
                 c.from(additionalConfigToFileTree.apply(additionalConfig));
                 c.into(getOutputDir());
             });
@@ -210,19 +191,18 @@ public class CopyRestApiTask extends DefaultTask {
      * Returns true if any files with a .yml extension exist the test resources rest-api-spec/test directory (from source or output dir)
      */
     private boolean projectHasYamlRestTests() {
-        File testSourceResourceDir = getTestSourceResourceDir();
-        File testOutputResourceDir = getTestOutputResourceDir(); // check output for cases where tests are copied programmatically
-
-        if (testSourceResourceDir == null && testOutputResourceDir == null) {
+        if (sourceResourceDir == null && outputResourceDir == null) {
             return false;
         }
         try {
-            if (testSourceResourceDir != null && new File(testSourceResourceDir, "rest-api-spec/test").exists()) {
-                return Files.walk(testSourceResourceDir.toPath().resolve("rest-api-spec/test"))
+            // check source folder for tests
+            if (sourceResourceDir != null && new File(sourceResourceDir, REST_TEST_PREFIX).exists()) {
+                return Files.walk(sourceResourceDir.toPath().resolve(REST_TEST_PREFIX))
                     .anyMatch(p -> p.getFileName().toString().endsWith("yml"));
             }
-            if (testOutputResourceDir != null && new File(testOutputResourceDir, "rest-api-spec/test").exists()) {
-                return Files.walk(testOutputResourceDir.toPath().resolve("rest-api-spec/test"))
+            // check output for cases where tests are copied programmatically
+            if (outputResourceDir != null && new File(outputResourceDir, REST_TEST_PREFIX).exists()) {
+                return Files.walk(new File(outputResourceDir, REST_TEST_PREFIX).toPath())
                     .anyMatch(p -> p.getFileName().toString().endsWith("yml"));
             }
         } catch (IOException e) {
@@ -231,39 +211,12 @@ public class CopyRestApiTask extends DefaultTask {
         return false;
     }
 
-    private File getTestSourceResourceDir() {
-        Optional<SourceSet> testSourceSet = getSourceSet();
-        if (testSourceSet.isPresent()) {
-            SourceSet testSources = testSourceSet.get();
-            Set<File> resourceDir = testSources.getResources()
-                .getSrcDirs()
-                .stream()
-                .filter(f -> f.isDirectory() && f.getParentFile().getName().equals(getSourceSetName()) && f.getName().equals("resources"))
-                .collect(Collectors.toSet());
-            assert resourceDir.size() <= 1;
-            if (resourceDir.size() == 0) {
-                return null;
-            }
-            return resourceDir.iterator().next();
-        } else {
-            return null;
-        }
-    }
-
-    private File getTestOutputResourceDir() {
-        Optional<SourceSet> testSourceSet = getSourceSet();
-        return testSourceSet.map(sourceSet -> sourceSet.getOutput().getResourcesDir()).orElse(null);
+    public void setSourceResourceDir(File sourceResourceDir) {
+        this.sourceResourceDir = sourceResourceDir;
     }
 
-    private Optional<SourceSet> getSourceSet() {
-        Project project = getProject();
-        return project.getConvention().findPlugin(JavaPluginConvention.class) == null
-            ? Optional.empty()
-            : Optional.ofNullable(GradleUtils.getJavaSourceSets(project).findByName(getSourceSetName()));
-    }
-
-    public void setSourceSetName(String sourceSetName) {
-        this.sourceSetName = sourceSetName;
+    public void setOutputResourceDir(File outputResourceDir) {
+        this.outputResourceDir = outputResourceDir;
     }
 
     public void setSkipHasRestTestCheck(boolean skipHasRestTestCheck) {
@@ -303,4 +256,5 @@ public class CopyRestApiTask extends DefaultTask {
     public FileCollection getXpackConfig() {
         return xpackConfig;
     }
+
 }

+ 23 - 61
buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/CopyRestTestsTask.java

@@ -20,22 +20,18 @@ package org.elasticsearch.gradle.test.rest;
 
 import org.elasticsearch.gradle.VersionProperties;
 import org.elasticsearch.gradle.info.BuildParams;
-import org.elasticsearch.gradle.util.GradleUtils;
 import org.gradle.api.DefaultTask;
-import org.gradle.api.Project;
 import org.gradle.api.file.ArchiveOperations;
 import org.gradle.api.file.FileCollection;
 import org.gradle.api.file.FileSystemOperations;
 import org.gradle.api.file.FileTree;
 import org.gradle.api.file.ProjectLayout;
-import org.gradle.api.plugins.JavaPluginConvention;
 import org.gradle.api.provider.ListProperty;
 import org.gradle.api.tasks.Input;
 import org.gradle.api.tasks.InputFiles;
 import org.gradle.api.tasks.Internal;
 import org.gradle.api.tasks.OutputDirectory;
 import org.gradle.api.tasks.SkipWhenEmpty;
-import org.gradle.api.tasks.SourceSet;
 import org.gradle.api.tasks.TaskAction;
 import org.gradle.api.tasks.util.PatternFilterable;
 import org.gradle.api.tasks.util.PatternSet;
@@ -44,7 +40,6 @@ import org.gradle.internal.Factory;
 import javax.inject.Inject;
 import java.io.File;
 import java.util.Objects;
-import java.util.Optional;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -54,6 +49,7 @@ import static org.elasticsearch.gradle.util.GradleUtils.getProjectPathFromTask;
  * Copies the Rest YAML test to the current projects test resources output directory.
  * This is intended to be be used from {@link RestResourcesPlugin} since the plugin wires up the needed
  * configurations and custom extensions.
+ *
  * @see RestResourcesPlugin
  */
 public class CopyRestTestsTask extends DefaultTask {
@@ -61,39 +57,32 @@ public class CopyRestTestsTask extends DefaultTask {
     private final ListProperty<String> includeCore = getProject().getObjects().listProperty(String.class);
     private final ListProperty<String> includeXpack = getProject().getObjects().listProperty(String.class);
 
-    private String sourceSetName;
+    private File outputResourceDir;
     private FileCollection coreConfig;
     private FileCollection xpackConfig;
     private FileCollection additionalConfig;
     private Function<FileCollection, FileTree> coreConfigToFileTree = FileCollection::getAsFileTree;
     private Function<FileCollection, FileTree> xpackConfigToFileTree = FileCollection::getAsFileTree;
     private Function<FileCollection, FileTree> additionalConfigToFileTree = FileCollection::getAsFileTree;
-    private String outputResourceRoot = "";
 
     private final PatternFilterable corePatternSet;
     private final PatternFilterable xpackPatternSet;
     private final ProjectLayout projectLayout;
+    private final FileSystemOperations fileSystemOperations;
+    private final ArchiveOperations archiveOperations;
 
     @Inject
-    public CopyRestTestsTask(ProjectLayout projectLayout) {
-        corePatternSet = getPatternSetFactory().create();
-        xpackPatternSet = getPatternSetFactory().create();
+    public CopyRestTestsTask(
+        ProjectLayout projectLayout,
+        Factory<PatternSet> patternSetFactory,
+        FileSystemOperations fileSystemOperations,
+        ArchiveOperations archiveOperations
+    ) {
+        corePatternSet = patternSetFactory.create();
+        xpackPatternSet = patternSetFactory.create();
         this.projectLayout = projectLayout;
-    }
-
-    @Inject
-    protected Factory<PatternSet> getPatternSetFactory() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Inject
-    protected FileSystemOperations getFileSystemOperations() {
-        throw new UnsupportedOperationException();
-    }
-
-    @Inject
-    protected ArchiveOperations getArchiveOperations() {
-        throw new UnsupportedOperationException();
+        this.fileSystemOperations = fileSystemOperations;
+        this.archiveOperations = archiveOperations;
     }
 
     @Input
@@ -106,11 +95,6 @@ public class CopyRestTestsTask extends DefaultTask {
         return includeXpack;
     }
 
-    @Input
-    String getSourceSetName() {
-        return sourceSetName;
-    }
-
     @SkipWhenEmpty
     @InputFiles
     public FileTree getInputDir() {
@@ -140,15 +124,7 @@ public class CopyRestTestsTask extends DefaultTask {
 
     @OutputDirectory
     public File getOutputDir() {
-        return new File(
-            new File(
-                getSourceSet().orElseThrow(() -> new IllegalArgumentException("could not find source set [" + sourceSetName + "]"))
-                    .getOutput()
-                    .getResourcesDir(),
-                outputResourceRoot
-            ),
-            REST_TEST_PREFIX
-        );
+        return new File(outputResourceDir, REST_TEST_PREFIX);
     }
 
     @TaskAction
@@ -158,7 +134,7 @@ public class CopyRestTestsTask extends DefaultTask {
         if (includeCore.get().isEmpty() == false) {
             if (BuildParams.isInternal()) {
                 getLogger().debug("Rest tests for project [{}] will be copied to the test resources.", projectPath);
-                getFileSystemOperations().copy(c -> {
+                fileSystemOperations.copy(c -> {
                     c.from(coreConfigToFileTree.apply(coreConfig));
                     c.into(getOutputDir());
                     c.include(corePatternSet.getIncludes());
@@ -169,12 +145,9 @@ public class CopyRestTestsTask extends DefaultTask {
                     projectPath,
                     VersionProperties.getElasticsearch()
                 );
-                getFileSystemOperations().copy(c -> {
-                    c.from(getArchiveOperations().zipTree(coreConfig.getSingleFile())); // jar file
-                    // this ends up as the same dir as outputDir
-                    c.into(
-                        new File(Objects.requireNonNull(getSourceSet().orElseThrow().getOutput().getResourcesDir()), outputResourceRoot)
-                    );
+                fileSystemOperations.copy(c -> {
+                    c.from(archiveOperations.zipTree(coreConfig.getSingleFile())); // jar file
+                    c.into(Objects.requireNonNull(outputResourceDir));
                     c.include(
                         includeCore.get().stream().map(prefix -> REST_TEST_PREFIX + "/" + prefix + "*/**").collect(Collectors.toList())
                     );
@@ -184,7 +157,7 @@ public class CopyRestTestsTask extends DefaultTask {
         // only copy x-pack tests if explicitly instructed
         if (includeXpack.get().isEmpty() == false) {
             getLogger().debug("X-pack rest tests for project [{}] will be copied to the test resources.", projectPath);
-            getFileSystemOperations().copy(c -> {
+            fileSystemOperations.copy(c -> {
                 c.from(xpackConfigToFileTree.apply(xpackConfig));
                 c.into(getOutputDir());
                 c.include(xpackPatternSet.getIncludes());
@@ -192,22 +165,15 @@ public class CopyRestTestsTask extends DefaultTask {
         }
         // copy any additional config
         if (additionalConfig != null) {
-            getFileSystemOperations().copy(c -> {
+            fileSystemOperations.copy(c -> {
                 c.from(additionalConfigToFileTree.apply(additionalConfig));
                 c.into(getOutputDir());
             });
         }
     }
 
-    private Optional<SourceSet> getSourceSet() {
-        Project project = getProject();
-        return project.getConvention().findPlugin(JavaPluginConvention.class) == null
-            ? Optional.empty()
-            : Optional.ofNullable(GradleUtils.getJavaSourceSets(project).findByName(getSourceSetName()));
-    }
-
-    public void setSourceSetName(String sourceSetName) {
-        this.sourceSetName = sourceSetName;
+    public void setOutputResourceDir(File outputResourceDir) {
+        this.outputResourceDir = outputResourceDir;
     }
 
     public void setCoreConfig(FileCollection coreConfig) {
@@ -234,10 +200,6 @@ public class CopyRestTestsTask extends DefaultTask {
         this.additionalConfigToFileTree = additionalConfigToFileTree;
     }
 
-    public void setOutputResourceRoot(String outputResourceRoot) {
-        this.outputResourceRoot = outputResourceRoot;
-    }
-
     @Internal
     public FileCollection getCoreConfig() {
         return coreConfig;

+ 36 - 34
buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/RestResourcesPlugin.java

@@ -24,8 +24,8 @@ import org.gradle.api.Plugin;
 import org.gradle.api.Project;
 import org.gradle.api.artifacts.Configuration;
 import org.gradle.api.artifacts.Dependency;
-import org.gradle.api.plugins.JavaBasePlugin;
 import org.gradle.api.provider.Provider;
+import org.gradle.api.tasks.SourceSet;
 import org.gradle.api.tasks.SourceSetContainer;
 
 import java.util.Map;
@@ -92,6 +92,9 @@ public class RestResourcesPlugin implements Plugin<Project> {
     public void apply(Project project) {
         RestResourcesExtension extension = project.getExtensions().create(EXTENSION_NAME, RestResourcesExtension.class);
 
+        SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
+        SourceSet defaultSourceSet = sourceSets.getByName(TEST_SOURCE_SET_NAME);
+
         // tests
         Configuration testConfig = project.getConfigurations().create("restTestConfig");
         Configuration xpackTestConfig = project.getConfigurations().create("restXpackTestConfig");
@@ -102,7 +105,7 @@ public class RestResourcesPlugin implements Plugin<Project> {
                 task.getIncludeCore().set(extension.restTests.getIncludeCore());
                 task.getIncludeXpack().set(extension.restTests.getIncludeXpack());
                 task.setCoreConfig(testConfig);
-                task.setSourceSetName(TEST_SOURCE_SET_NAME);
+                task.setOutputResourceDir(defaultSourceSet.getOutput().getResourcesDir());
                 if (BuildParams.isInternal()) {
                     // core
                     Dependency restTestdependency = project.getDependencies()
@@ -127,38 +130,37 @@ public class RestResourcesPlugin implements Plugin<Project> {
         Configuration xpackSpecConfig = project.getConfigurations().create("restXpackSpec");
         project.getConfigurations().create("restSpecs");
         project.getConfigurations().create("restXpackSpecs");
-        Provider<CopyRestApiTask> copyRestYamlSpecTask = project.getTasks()
-            .register("copyRestApiSpecsTask", CopyRestApiTask.class, task -> {
-                task.getIncludeCore().set(extension.restApi.getIncludeCore());
-                task.getIncludeXpack().set(extension.restApi.getIncludeXpack());
-                task.dependsOn(copyRestYamlTestTask);
-                task.setCoreConfig(specConfig);
-                task.setSourceSetName(TEST_SOURCE_SET_NAME);
-                if (BuildParams.isInternal()) {
-                    Dependency restSpecDependency = project.getDependencies()
-                        .project(Map.of("path", ":rest-api-spec", "configuration", "restSpecs"));
-                    project.getDependencies().add(specConfig.getName(), restSpecDependency);
-                    task.setXpackConfig(xpackSpecConfig);
-                    Dependency restXpackSpecDependency = project.getDependencies()
-                        .project(Map.of("path", ":x-pack:plugin", "configuration", "restXpackSpecs"));
-                    project.getDependencies().add(xpackSpecConfig.getName(), restXpackSpecDependency);
-                    task.dependsOn(task.getXpackConfig());
-                } else {
-                    Dependency dependency = project.getDependencies()
-                        .create("org.elasticsearch:rest-api-spec:" + VersionProperties.getElasticsearch());
-                    project.getDependencies().add(specConfig.getName(), dependency);
-                }
-                task.dependsOn(xpackSpecConfig);
-            });
-
-        project.getPlugins().withType(JavaBasePlugin.class).configureEach(javaBasePlugin -> {
-            SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
-            sourceSets.matching(sourceSet -> sourceSet.getName().equals(TEST_SOURCE_SET_NAME))
-                .configureEach(
-                    testSourceSet -> project.getTasks()
-                        .named(testSourceSet.getProcessResourcesTaskName())
-                        .configure(t -> t.dependsOn(copyRestYamlSpecTask))
-                );
+        Provider<CopyRestApiTask> copyRestYamlApiTask = project.getTasks().register("copyRestApiSpecsTask", CopyRestApiTask.class, task -> {
+            task.getIncludeCore().set(extension.restApi.getIncludeCore());
+            task.getIncludeXpack().set(extension.restApi.getIncludeXpack());
+            task.dependsOn(copyRestYamlTestTask);
+            task.setCoreConfig(specConfig);
+            task.setOutputResourceDir(defaultSourceSet.getOutput().getResourcesDir());
+            task.setSourceResourceDir(
+                defaultSourceSet.getResources()
+                    .getSrcDirs()
+                    .stream()
+                    .filter(f -> f.isDirectory() && f.getName().equals("resources"))
+                    .findFirst()
+                    .orElse(null)
+            );
+            if (BuildParams.isInternal()) {
+                Dependency restSpecDependency = project.getDependencies()
+                    .project(Map.of("path", ":rest-api-spec", "configuration", "restSpecs"));
+                project.getDependencies().add(specConfig.getName(), restSpecDependency);
+                task.setXpackConfig(xpackSpecConfig);
+                Dependency restXpackSpecDependency = project.getDependencies()
+                    .project(Map.of("path", ":x-pack:plugin", "configuration", "restXpackSpecs"));
+                project.getDependencies().add(xpackSpecConfig.getName(), restXpackSpecDependency);
+                task.dependsOn(task.getXpackConfig());
+            } else {
+                Dependency dependency = project.getDependencies()
+                    .create("org.elasticsearch:rest-api-spec:" + VersionProperties.getElasticsearch());
+                project.getDependencies().add(specConfig.getName(), dependency);
+            }
+            task.dependsOn(xpackSpecConfig);
         });
+
+        project.getTasks().named(defaultSourceSet.getProcessResourcesTaskName()).configure(t -> t.dependsOn(copyRestYamlApiTask));
     }
 }

+ 12 - 4
buildSrc/src/main/java/org/elasticsearch/gradle/test/rest/YamlRestTestPlugin.java

@@ -64,15 +64,23 @@ public class YamlRestTestPlugin implements Plugin<Project> {
         setupDependencies(project, yamlTestSourceSet);
 
         // setup the copy for the rest resources
-        project.getTasks()
-            .withType(CopyRestApiTask.class)
-            .configureEach(copyRestApiTask -> { copyRestApiTask.setSourceSetName(SOURCE_SET_NAME); });
+        project.getTasks().withType(CopyRestApiTask.class).configureEach(copyRestApiTask -> {
+            copyRestApiTask.setOutputResourceDir(yamlTestSourceSet.getOutput().getResourcesDir());
+            copyRestApiTask.setSourceResourceDir(
+                yamlTestSourceSet.getResources()
+                    .getSrcDirs()
+                    .stream()
+                    .filter(f -> f.isDirectory() && f.getName().equals("resources"))
+                    .findFirst()
+                    .orElse(null)
+            );
+        });
         project.getTasks()
             .named(yamlTestSourceSet.getProcessResourcesTaskName())
             .configure(t -> t.dependsOn(project.getTasks().withType(CopyRestApiTask.class)));
         project.getTasks()
             .withType(CopyRestTestsTask.class)
-            .configureEach(copyRestTestTask -> copyRestTestTask.setSourceSetName(SOURCE_SET_NAME));
+            .configureEach(copyRestTestTask -> copyRestTestTask.setOutputResourceDir(yamlTestSourceSet.getOutput().getResourcesDir()));
 
         // setup IDE
         GradleUtils.setupIdeForTestSourceSet(project, yamlTestSourceSet);