|
@@ -18,6 +18,7 @@
|
|
|
*/
|
|
|
package org.elasticsearch.gradle.util;
|
|
|
|
|
|
+import org.elasticsearch.gradle.ElasticsearchJavaPlugin;
|
|
|
import org.gradle.api.Action;
|
|
|
import org.gradle.api.GradleException;
|
|
|
import org.gradle.api.NamedDomainObjectContainer;
|
|
@@ -25,16 +26,26 @@ import org.gradle.api.PolymorphicDomainObjectContainer;
|
|
|
import org.gradle.api.Project;
|
|
|
import org.gradle.api.Task;
|
|
|
import org.gradle.api.UnknownTaskException;
|
|
|
+import org.gradle.api.artifacts.Configuration;
|
|
|
+import org.gradle.api.plugins.JavaBasePlugin;
|
|
|
import org.gradle.api.plugins.JavaPluginConvention;
|
|
|
import org.gradle.api.provider.Provider;
|
|
|
import org.gradle.api.services.BuildService;
|
|
|
import org.gradle.api.services.BuildServiceRegistration;
|
|
|
import org.gradle.api.services.BuildServiceRegistry;
|
|
|
+import org.gradle.api.tasks.SourceSet;
|
|
|
import org.gradle.api.tasks.SourceSetContainer;
|
|
|
import org.gradle.api.tasks.TaskContainer;
|
|
|
import org.gradle.api.tasks.TaskProvider;
|
|
|
+import org.gradle.api.tasks.testing.Test;
|
|
|
+import org.gradle.plugins.ide.eclipse.model.EclipseModel;
|
|
|
+import org.gradle.plugins.ide.idea.model.IdeaModel;
|
|
|
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
public abstract class GradleUtils {
|
|
|
|
|
@@ -120,4 +131,79 @@ public abstract class GradleUtils {
|
|
|
|
|
|
return (Provider<T>) registration.getService();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add a source set and task of the same name that runs tests.
|
|
|
+ *
|
|
|
+ * IDEs are also configured if setup, and the test task is added to check. The new test source
|
|
|
+ * set extends from the normal test source set to allow sharing of utilities.
|
|
|
+ *
|
|
|
+ * @return A task provider for the newly created test task
|
|
|
+ */
|
|
|
+ public static TaskProvider<?> addTestSourceSet(Project project, String sourceSetName) {
|
|
|
+ project.getPluginManager().apply(ElasticsearchJavaPlugin.class);
|
|
|
+
|
|
|
+ // create our test source set and task
|
|
|
+ SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
|
|
|
+ SourceSet testSourceSet = sourceSets.create(sourceSetName);
|
|
|
+ TaskProvider<Test> testTask = project.getTasks().register(sourceSetName, Test.class);
|
|
|
+ testTask.configure(task -> {
|
|
|
+ task.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
|
|
|
+ task.setTestClassesDirs(testSourceSet.getOutput().getClassesDirs());
|
|
|
+ task.setClasspath(testSourceSet.getRuntimeClasspath());
|
|
|
+ });
|
|
|
+
|
|
|
+ Configuration testCompileConfig = project.getConfigurations().getByName(testSourceSet.getCompileClasspathConfigurationName());
|
|
|
+ Configuration testRuntimeConfig = project.getConfigurations().getByName(testSourceSet.getRuntimeClasspathConfigurationName());
|
|
|
+ testSourceSet.setCompileClasspath(testCompileConfig);
|
|
|
+ testSourceSet.setRuntimeClasspath(project.getObjects().fileCollection().from(testSourceSet.getOutput(), testRuntimeConfig));
|
|
|
+
|
|
|
+ extendSourceSet(project, SourceSet.MAIN_SOURCE_SET_NAME, sourceSetName);
|
|
|
+
|
|
|
+ // setup IDEs
|
|
|
+ String runtimeClasspathName = testSourceSet.getRuntimeClasspathConfigurationName();
|
|
|
+ Configuration runtimeClasspathConfiguration = project.getConfigurations().getByName(runtimeClasspathName);
|
|
|
+ project.getPluginManager().withPlugin("idea", p -> {
|
|
|
+ IdeaModel idea = project.getExtensions().getByType(IdeaModel.class);
|
|
|
+ idea.getModule().setTestSourceDirs(testSourceSet.getJava().getSrcDirs());
|
|
|
+ idea.getModule().getScopes().put("TEST", Map.of("plus", List.of(runtimeClasspathConfiguration)));
|
|
|
+ });
|
|
|
+ project.getPluginManager().withPlugin("eclipse", p -> {
|
|
|
+ EclipseModel eclipse = project.getExtensions().getByType(EclipseModel.class);
|
|
|
+ eclipse.getClasspath().setSourceSets(List.of(testSourceSet));
|
|
|
+ eclipse.getClasspath().getPlusConfigurations().add(runtimeClasspathConfiguration);
|
|
|
+ });
|
|
|
+
|
|
|
+ // add to the check task
|
|
|
+ project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure(check -> check.dependsOn(testTask));
|
|
|
+
|
|
|
+ return testTask;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Extend the configurations of one source set from another.
|
|
|
+ */
|
|
|
+ public static void extendSourceSet(Project project, String parentSourceSetName, String childSourceSetName) {
|
|
|
+ final List<Function<SourceSet, String>> configNameFunctions = Arrays.asList(
|
|
|
+ SourceSet::getCompileConfigurationName,
|
|
|
+ SourceSet::getImplementationConfigurationName,
|
|
|
+ SourceSet::getRuntimeConfigurationName,
|
|
|
+ SourceSet::getRuntimeOnlyConfigurationName
|
|
|
+ );
|
|
|
+ SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
|
|
|
+ SourceSet parent = sourceSets.getByName(parentSourceSetName);
|
|
|
+ SourceSet child = sourceSets.getByName(childSourceSetName);
|
|
|
+
|
|
|
+ for (Function<SourceSet, String> configNameFunction : configNameFunctions) {
|
|
|
+ String parentConfigName = configNameFunction.apply(parent);
|
|
|
+ String childConfigName = configNameFunction.apply(child);
|
|
|
+ Configuration parentConfig = project.getConfigurations().getByName(parentConfigName);
|
|
|
+ Configuration childConfig = project.getConfigurations().getByName(childConfigName);
|
|
|
+ childConfig.extendsFrom(parentConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ // tie this new test source set to the main and test source sets
|
|
|
+ child.setCompileClasspath(project.getObjects().fileCollection().from(child.getCompileClasspath(), parent.getOutput()));
|
|
|
+ child.setRuntimeClasspath(project.getObjects().fileCollection().from(child.getRuntimeClasspath(), parent.getOutput()));
|
|
|
+ }
|
|
|
}
|