diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy index ca531aee8d1..d0b833cc884 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy @@ -22,10 +22,12 @@ import org.elasticsearch.gradle.VersionProperties import org.elasticsearch.gradle.testclusters.ElasticsearchCluster import org.elasticsearch.gradle.testclusters.RestTestRunnerTask import org.elasticsearch.gradle.testclusters.TestClustersPlugin +import org.elasticsearch.gradle.tool.Boilerplate import org.elasticsearch.gradle.tool.ClasspathUtils import org.gradle.api.DefaultTask import org.gradle.api.Task import org.gradle.api.execution.TaskExecutionAdapter +import org.gradle.api.file.FileCopyDetails import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging import org.gradle.api.tasks.Copy @@ -39,6 +41,7 @@ import org.gradle.process.CommandLineArgumentProvider import java.nio.charset.StandardCharsets import java.nio.file.Files import java.util.stream.Stream + /** * A wrapper task around setting up a cluster and running rest tests. */ @@ -121,10 +124,10 @@ class RestIntegTestTask extends DefaultTask { runner.systemProperty('test.cluster', System.getProperty("tests.cluster")) } - // copy the rest spec/tests into the test resources - Task copyRestSpec = createCopyRestSpecTask() - runner.dependsOn(copyRestSpec) - + // copy the rest spec/tests onto the test classpath + Copy copyRestSpec = createCopyRestSpecTask() + project.sourceSets.test.output.builtBy(copyRestSpec) + // this must run after all projects have been configured, so we know any project // references can be accessed as a fully configured project.gradle.projectsEvaluated { @@ -222,50 +225,37 @@ class RestIntegTestTask extends DefaultTask { } - /** - * Creates a task (if necessary) to copy the rest spec files. - * - * @param project The project to add the copy task to - * @param includePackagedTests true if the packaged tests should be copied, false otherwise - */ - Task createCopyRestSpecTask() { - project.configurations { - restSpec + Copy createCopyRestSpecTask() { + Boilerplate.maybeCreate(project.configurations, 'restSpec') { + project.dependencies.add( + 'restSpec', + ClasspathUtils.isElasticsearchProject() ? project.project(':rest-api-spec') : + "org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch}" + ) } - project.dependencies { - restSpec ClasspathUtils.isElasticsearchProject() ? project.project(':rest-api-spec') : - "org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch}" - } - Task copyRestSpec = project.tasks.findByName('copyRestSpec') - if (copyRestSpec != null) { - return copyRestSpec - } - Map copyRestSpecProps = [ - name : 'copyRestSpec', - type : Copy, - dependsOn: [project.configurations.restSpec, 'processTestResources'] - ] - copyRestSpec = project.tasks.create(copyRestSpecProps) { - into project.sourceSets.test.output.resourcesDir - } - project.afterEvaluate { - copyRestSpec.from({ project.zipTree(project.configurations.restSpec.singleFile) }) { - include 'rest-api-spec/api/**' - if (includePackaged) { - include 'rest-api-spec/test/**' + + return Boilerplate.maybeCreate(project.tasks, 'copyRestSpec', Copy) { Copy copy -> + copy.dependsOn project.configurations.restSpec + copy.into(project.sourceSets.test.output.resourcesDir) + copy.from({ project.zipTree(project.configurations.restSpec.singleFile) }) { + includeEmptyDirs = false + include 'rest-api-spec/**' + filesMatching('rest-api-spec/test/**') { FileCopyDetails details -> + if (includePackaged == false) { + details.exclude() + } } } - } - if (project.plugins.hasPlugin(IdeaPlugin)) { - project.idea { - module { - if (scopes.TEST != null) { - scopes.TEST.plus.add(project.configurations.restSpec) + + if (project.plugins.hasPlugin(IdeaPlugin)) { + project.idea { + module { + if (scopes.TEST != null) { + scopes.TEST.plus.add(project.configurations.restSpec) + } } } } } - return copyRestSpec } - } diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/tool/Boilerplate.java b/buildSrc/src/main/java/org/elasticsearch/gradle/tool/Boilerplate.java index 760e5f60f1c..f4ce626f7d6 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/tool/Boilerplate.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/tool/Boilerplate.java @@ -20,6 +20,7 @@ package org.elasticsearch.gradle.tool; import org.gradle.api.Action; import org.gradle.api.NamedDomainObjectContainer; +import org.gradle.api.PolymorphicDomainObjectContainer; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.UnknownTaskException; @@ -52,6 +53,16 @@ public abstract class Boilerplate { } + public static T maybeCreate(PolymorphicDomainObjectContainer collection, String name, Class type, Action action) { + return Optional.ofNullable(collection.findByName(name)) + .orElseGet(() -> { + T result = collection.create(name, type); + action.execute(result); + return result; + }); + + } + public static TaskProvider maybeRegister(TaskContainer tasks, String name, Class clazz, Action action) { try { return tasks.named(name, clazz);