Make internal project flag overrideable by tests (#47757)

In order to work with external elasticsearch plugins, some parts of
build-tools need to know when the current build is part of the
elasticsearch project or an external build. We identify these "internal"
builds by a marker in our buildSrc classpath. However, some build-tools
integ tests need to override this flag to test both the external and
internal behavior.

This commit moves the storage of the flag indicating whether we are
running in an internal build to global build info. This will allow
testkit projects to override the flag.
This commit is contained in:
Ryan Ernst 2019-10-09 12:00:59 -07:00 committed by Ryan Ernst
parent 9b3790d4f2
commit 14b979a7bc
7 changed files with 25 additions and 21 deletions

View File

@ -163,7 +163,7 @@ class PluginBuildPlugin implements Plugin<Project> {
private static void configureDependencies(Project project) {
project.dependencies {
if (ClasspathUtils.isElasticsearchProject()) {
if (ClasspathUtils.isElasticsearchProject(project)) {
compileOnly project.project(':server')
testCompile project.project(':test:framework')
} else {

View File

@ -47,7 +47,7 @@ class PrecommitTasks {
}
Configuration jarHellConfig = project.configurations.create("jarHell")
if (ClasspathUtils.isElasticsearchProject() && project.path.equals(":libs:elasticsearch-core") == false) {
if (ClasspathUtils.isElasticsearchProject(project) && project.path.equals(":libs:elasticsearch-core") == false) {
// External plugins will depend on this already via transitive dependencies.
// Internal projects are not all plugins, so make sure the check is available
// we are not doing this for this project itself to avoid jar hell with itself
@ -252,7 +252,7 @@ class PrecommitTasks {
}
private static TaskProvider configureLoggerUsage(Project project) {
Object dependency = ClasspathUtils.isElasticsearchProject() ? project.project(':test:logger-usage') :
Object dependency = ClasspathUtils.isElasticsearchProject(project) ? project.project(':test:logger-usage') :
"org.elasticsearch.test:logger-usage:${VersionProperties.elasticsearch}"
project.configurations.create('loggerUsagePlugin')

View File

@ -21,7 +21,6 @@ package org.elasticsearch.gradle.test
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
@ -121,7 +120,7 @@ class RestIntegTestTask extends DefaultTask {
Boilerplate.maybeCreate(project.configurations, 'restSpec') {
project.dependencies.add(
'restSpec',
ClasspathUtils.isElasticsearchProject() ? project.project(':rest-api-spec') :
ClasspathUtils.isElasticsearchProject(project) ? project.project(':rest-api-spec') :
"org.elasticsearch:rest-api-spec:${VersionProperties.elasticsearch}"
)
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.gradle;
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
@ -35,10 +36,12 @@ public class ReaperPlugin implements Plugin<Project> {
throw new IllegalArgumentException("ReaperPlugin can only be applied to the root project of a build");
}
project.getPlugins().apply(GlobalBuildInfoPlugin.class);
Path inputDir = project.getRootDir().toPath().resolve(".gradle")
.resolve("reaper").resolve("build-" + ProcessHandle.current().pid());
ReaperService service = project.getExtensions().create("reaper", ReaperService.class,
project.getLogger(), project.getBuildDir().toPath(), inputDir);
project, project.getBuildDir().toPath(), inputDir);
project.getGradle().buildFinished(result -> service.shutdown());
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.gradle;
import org.elasticsearch.gradle.tool.ClasspathUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.internal.jvm.Jvm;
@ -40,14 +41,16 @@ public class ReaperService {
private static final String REAPER_CLASS = "org/elasticsearch/gradle/reaper/Reaper.class";
private static final Pattern REAPER_JAR_PATH_PATTERN = Pattern.compile("file:(.*)!/" + REAPER_CLASS);
private Logger logger;
private Path buildDir;
private Path inputDir;
private Path logFile;
private final Logger logger;
private final boolean isInternal;
private final Path buildDir;
private final Path inputDir;
private final Path logFile;
private volatile Process reaperProcess;
public ReaperService(Logger logger, Path buildDir, Path inputDir) {
this.logger = logger;
public ReaperService(Project project, Path buildDir, Path inputDir) {
this.logger = project.getLogger();
this.isInternal = ClasspathUtils.isElasticsearchProject(project);
this.buildDir = buildDir;
this.inputDir = inputDir;
this.logFile = inputDir.resolve("reaper.log");
@ -137,7 +140,7 @@ public class ReaperService {
}
private Path locateReaperJar() {
if (ClasspathUtils.isElasticsearchProject()) {
if (isInternal) {
// when running inside the Elasticsearch build just pull find the jar in the runtime classpath
URL main = this.getClass().getClassLoader().getResource(REAPER_CLASS);
String mainPath = main.getFile();

View File

@ -108,6 +108,7 @@ public class GlobalBuildInfoPlugin implements Plugin<Project> {
ext.set("buildDate", ZonedDateTime.now(ZoneOffset.UTC));
ext.set("testSeed", testSeed);
ext.set("isCi", System.getenv("JENKINS_URL") != null);
ext.set("isInternal", GlobalBuildInfoPlugin.class.getResource("/buildSrc.marker") != null);
});
}

View File

@ -1,12 +1,9 @@
package org.elasticsearch.gradle.tool;
public class ClasspathUtils {
private static boolean isElasticsearchProject;
import org.gradle.api.Project;
import org.gradle.api.plugins.ExtraPropertiesExtension;
static {
// look for buildSrc marker file, if it exists then we are running in the context of the elastic/elasticsearch build
isElasticsearchProject = ClasspathUtils.class.getResource("/buildSrc.marker") != null;
}
public class ClasspathUtils {
private ClasspathUtils() {
}
@ -17,7 +14,8 @@ public class ClasspathUtils {
*
* @return if we are currently running in the `elastic/elasticsearch` project
*/
public static boolean isElasticsearchProject() {
return isElasticsearchProject;
public static boolean isElasticsearchProject(Project project) {
ExtraPropertiesExtension ext = project.getExtensions().getByType(ExtraPropertiesExtension.class);
return (boolean) ext.get("isInternal");
}
}