Improvement usage of gradle task avoidance api (#56627) (#56981)

Use gradle task avoidance api wherever it is possible as a drop in replacement in the es build
This commit is contained in:
Rene Groeschke 2020-05-25 09:37:33 +02:00 committed by GitHub
parent 47bf95cee3
commit 28920a45f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 109 additions and 92 deletions

View File

@ -116,7 +116,7 @@ subprojects {
}
}
task updateCIBwcVersions() {
tasks.register("updateCIBwcVersions") {
doLast {
File yml = file(".ci/bwcVersions")
yml.text = ""
@ -150,7 +150,7 @@ allprojects {
}
}
task verifyVersions {
tasks.register("verifyVersions") {
doLast {
if (gradle.startParameter.isOffline()) {
throw new GradleException("Must run in online mode to verify versions")
@ -202,7 +202,7 @@ subprojects {
ext.bwc_tests_enabled = bwc_tests_enabled
}
task verifyBwcTestsEnabled {
tasks.register("verifyBwcTestsEnabled") {
doLast {
if (bwc_tests_enabled == false) {
throw new GradleException('Bwc tests are disabled. They must be re-enabled after completing backcompat behavior backporting.')
@ -210,10 +210,10 @@ task verifyBwcTestsEnabled {
}
}
task branchConsistency {
tasks.register("branchConsistency") {
description 'Ensures this branch is internally consistent. For example, that versions constants match released versions.'
group 'Verification'
dependsOn verifyVersions, verifyBwcTestsEnabled
dependsOn ":verifyVersions", ":verifyBwcTestsEnabled"
}
allprojects {
@ -414,7 +414,7 @@ class Run extends DefaultTask {
}
}
task run(type: Run) {
tasks.register("run", Run) {
dependsOn ':distribution:run'
description = 'Runs elasticsearch in the foreground'
group = 'Verification'
@ -493,7 +493,7 @@ allprojects {
if (realTask == null) {
return
}
project.tasks.create(taskName) {
project.tasks.register(taskName) {
doLast {
println("${realTask.path} dependencies:")
for (Task dep : realTask.getTaskDependencies().getDependencies(realTask)) {

View File

@ -48,7 +48,7 @@ if (project == rootProject) {
Properties props = VersionPropertiesLoader.loadBuildSrcVersion(project.file('version.properties'))
version = props.getProperty("elasticsearch")
task generateVersionProperties(type: WriteProperties) {
def generateVersionProperties = tasks.register("generateVersionProperties", WriteProperties) {
outputFile = "${buildDir}/version.properties"
comment = 'Generated version properties'
properties(props)
@ -237,13 +237,13 @@ if (project != rootProject) {
}
}
task integTest(type: Test) {
tasks.register("integTest", Test) {
inputs.dir(file("src/testKit")).withPropertyName("testkit dir").withPathSensitivity(PathSensitivity.RELATIVE)
systemProperty 'test.version_under_test', version
onlyIf { org.elasticsearch.gradle.info.BuildParams.inFipsJvm == false }
maxParallelForks = System.getProperty('tests.jvms', org.elasticsearch.gradle.info.BuildParams.defaultParallel.toString()) as Integer
}
check.dependsOn(integTest)
check.dependsOn("integTest")
// for now we hardcode the tests for our build to use the gradle jvm.
tasks.withType(Test).configureEach {

View File

@ -1,7 +1,7 @@
apply plugin: 'java'
jar {
archiveName = "${project.name}.jar"
archiveFileName = "${project.name}.jar"
manifest {
attributes 'Main-Class': 'org.elasticsearch.gradle.reaper.Reaper'
}

View File

@ -26,6 +26,8 @@ import org.gradle.api.GradleException
import org.gradle.api.Task
import org.gradle.api.tasks.Exec
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskProvider
/**
* A fixture for integration tests which runs in a separate process launched by Ant.
*/
@ -79,7 +81,7 @@ class AntFixture extends AntTask implements Fixture {
return tmpFile.exists()
}
private final Task stopTask
private final TaskProvider stopTask
AntFixture() {
stopTask = createStopTask()
@ -88,7 +90,7 @@ class AntFixture extends AntTask implements Fixture {
@Override
@Internal
Task getStopTask() {
TaskProvider getStopTask() {
return stopTask
}
@ -222,24 +224,29 @@ class AntFixture extends AntTask implements Fixture {
}
/** Adds a task to kill an elasticsearch node with the given pidfile */
private Task createStopTask() {
private TaskProvider createStopTask() {
final AntFixture fixture = this
final Object pid = "${ -> fixture.pid }"
Exec stop = project.tasks.create(name: "${name}#stop", type: LoggedExec)
stop.onlyIf { fixture.pidFile.exists() }
stop.doFirst {
logger.info("Shutting down ${fixture.name} with pid ${pid}")
}
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
stop.executable = 'Taskkill'
stop.args('/PID', pid, '/F')
} else {
stop.executable = 'kill'
stop.args('-9', pid)
}
stop.doLast {
project.delete(fixture.pidFile)
TaskProvider<Exec> stop = project.tasks.register("${name}#stop", LoggedExec)
stop.configure {
onlyIf { fixture.pidFile.exists() }
doFirst {
logger.info("Shutting down ${fixture.name} with pid ${pid}")
}
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
executable = 'Taskkill'
args('/PID', pid, '/F')
} else {
executable = 'kill'
args('-9', pid)
}
doLast {
project.delete(fixture.pidFile)
}
}
return stop
}

View File

@ -58,10 +58,12 @@ class TestWithDependenciesPlugin implements Plugin<Project> {
String outputDir = "${project.buildDir}/generated-resources/${pluginProject.name}"
String camelName = pluginProject.name.replaceAll(/-(\w)/) { _, c -> c.toUpperCase(Locale.ROOT) }
String taskName = "copy" + camelName[0].toUpperCase(Locale.ROOT) + camelName.substring(1) + "Metadata"
Copy copyPluginMetadata = project.tasks.create(taskName, Copy.class)
copyPluginMetadata.into(outputDir)
copyPluginMetadata.from(pluginProject.tasks.pluginProperties)
copyPluginMetadata.from(pluginProject.file('src/main/plugin-metadata'))
project.tasks.register(taskName, Copy.class) {
into(outputDir)
from(pluginProject.tasks.pluginProperties)
from(pluginProject.file('src/main/plugin-metadata'))
}
project.sourceSets.test.output.dir(outputDir, builtBy: taskName)
}
}

View File

@ -32,12 +32,16 @@ public class ExportElasticsearchBuildResourcesTaskIT extends GradleIntegrationTe
BuildResult result = getGradleRunner(PROJECT_NAME).withArguments("buildResources", "-s", "-i").build();
assertTaskSuccessful(result, ":buildResources");
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml");
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
// using task avoidance api means the task configuration of the sample task is never triggered
assertBuildFileDoesNotExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
result = getGradleRunner(PROJECT_NAME).withArguments("buildResources", "-s", "-i").build();
assertTaskUpToDate(result, ":buildResources");
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml");
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
// using task avoidance api means the task configuration of the sample task is never triggered
assertBuildFileDoesNotExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
}
public void testImplicitTaskDependencyCopy() {
@ -46,8 +50,10 @@ public class ExportElasticsearchBuildResourcesTaskIT extends GradleIntegrationTe
assertTaskSuccessful(result, ":buildResources");
assertTaskSuccessful(result, ":sampleCopyAll");
assertBuildFileExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle.xml");
// This is a side effect of compile time reference
assertBuildFileExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle_suppressions.xml");
// using task avoidance api means the task configuration of the sample task is never triggered
// which means buildResource is not configured to copy this file
assertBuildFileDoesNotExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle_suppressions.xml");
}
public void testImplicitTaskDependencyInputFileOfOther() {

View File

@ -27,13 +27,13 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
@Before
public void setUp() throws Exception {
// Build the sample jars
getGradleRunner("thirdPartyAudit").withArguments("build", "-s").build();
getGradleRunner("thirdPartyAudit").withArguments(":sample_jars:build", "-s").build();
}
public void testElasticsearchIgnored() {
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
"clean",
"empty",
":clean",
":empty",
"-s",
"-PcompileOnlyGroup=elasticsearch.gradle:broken-log4j",
"-PcompileOnlyVersion=0.0.1",
@ -44,9 +44,9 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
}
public void testWithEmptyRules() {
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
"clean",
"empty",
getGradleRunner("thirdPartyAudit").withArguments(
":clean",
":empty",
"-s",
"-PcompileOnlyGroup=other.gradle:broken-log4j",
"-PcompileOnlyVersion=0.0.1",
@ -57,8 +57,8 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
public void testViolationFoundAndCompileOnlyIgnored() {
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
"clean",
"absurd",
":clean",
":absurd",
"-s",
"-PcompileOnlyGroup=other.gradle:broken-log4j",
"-PcompileOnlyVersion=0.0.1",
@ -73,8 +73,8 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
public void testClassNotFoundAndCompileOnlyIgnored() {
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
"clean",
"absurd",
":clean",
":absurd",
"-s",
"-PcompileGroup=other.gradle:broken-log4j",
"-PcompileVersion=0.0.1",
@ -94,8 +94,8 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
public void testJarHellWithJDK() {
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
"clean",
"absurd",
":clean",
":absurd",
"-s",
"-PcompileGroup=other.gradle:jarhellJdk",
"-PcompileVersion=0.0.1",
@ -115,8 +115,8 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
public void testElasticsearchIgnoredWithViolations() {
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
"clean",
"absurd",
":clean",
":absurd",
"-s",
"-PcompileOnlyGroup=elasticsearch.gradle:broken-log4j",
"-PcompileOnlyVersion=0.0.1",

View File

@ -3,10 +3,10 @@ if (distroConfig != null) {
// setup the test distribution as an artifact of this project
String distroType = System.getProperty('tests.distro.type')
Task buildDistro
def buildDistro
File buildFile
if (['rpm', 'deb'].contains(distroType)) {
buildDistro = project.tasks.create("build" + distroType.capitalize(), Copy) {
buildDistro = project.tasks.register("build" + distroType.capitalize(), Copy) {
from 'files'
into 'build/files'
include 'fake_elasticsearch.tar.gz'
@ -21,7 +21,7 @@ if (distroConfig != null) {
extension = "zip"
}
// copy file as is
buildDistro = project.tasks.create("buildArchive", Copy) {
buildDistro = project.tasks.register("buildArchive", Copy) {
from 'files'
include "fake_elasticsearch.${extension}"
into 'build/files'

View File

@ -36,7 +36,7 @@ elasticsearch_distributions {
}
}
task assertDistroFile {
tasks.register("assertDistroFile") {
dependsOn elasticsearch_distributions.test_distro
doLast {
File distroFile = new File(elasticsearch_distributions.test_distro.toString())
@ -50,7 +50,7 @@ task assertDistroFile {
}
if (['rpm', 'deb'].contains(distroType) == false) {
task assertDistroExtracted {
tasks.register("assertDistroExtracted") {
dependsOn elasticsearch_distributions.test_distro.extracted, assertDistroFile
doLast {
File distroExtracted = new File(elasticsearch_distributions.test_distro.extracted.toString())

View File

@ -9,13 +9,13 @@ buildResources {
copy 'checkstyle.xml'
}
task sampleCopyAll(type: Sync) {
tasks.register("sampleCopyAll", Sync) {
/** Note: no explicit dependency. This works with tasks that use the Provider API a.k.a "Lazy Configuration" **/
from buildResources
into "$buildDir/sampleCopyAll"
}
task sample {
tasks.register("sample") {
// This does not work, task dependencies can't be providers
// dependsOn buildResources.resource('minimumRuntimeVersion')
// Nor does this, despite https://github.com/gradle/gradle/issues/3811
@ -29,7 +29,7 @@ task sample {
}
}
task noConfigAfterExecution {
tasks.register("noConfigAfterExecution") {
dependsOn buildResources
doLast {
println "This should cause an error because we are refferencing " +

View File

@ -30,7 +30,7 @@ loggerUsageCheck.enabled = false
// TODO: shouldn't be part of BuildPlugin, should be tested separately
validateNebulaPom.enabled = false
task hello {
tasks.register("hello") {
doFirst {
println "build plugin can be applied"
}

View File

@ -9,7 +9,7 @@ project.gradle.projectsEvaluated {
repository.setUrl(fakeJdkRepo)
}
task numConfigurations {
tasks.register("numConfigurations") {
doLast {
println "NUM CONFIGS: ${project.configurations.size()}"
}

View File

@ -26,21 +26,21 @@ jdks {
}
}
task getLinuxJdk {
tasks.register("getLinuxJdk") {
dependsOn jdks.linux
doLast {
println "JDK HOME: " + jdks.linux
}
}
task getDarwinJdk {
tasks.register("getDarwinJdk") {
dependsOn jdks.darwin
doLast {
println "JDK HOME: " + jdks.darwin
}
}
task getWindowsJdk {
tasks.register("getWindowsJdk") {
dependsOn jdks.windows
doLast {
println "JDK HOME: " + jdks.windows

View File

@ -2,7 +2,7 @@ plugins {
id 'elasticsearch.reaper'
}
task launchReaper {
tasks.register("launchReaper") {
doLast {
def reaper = project.extensions.getByName('reaper')
reaper.registerCommand('test', 'true')

View File

@ -23,7 +23,7 @@ switch (testPreserveFileTimestamps) {
+ testPreserveFileTimestamps + "]")
}
task buildBZip2Tar(type: SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar tar ->
tasks.register("buildBZip2Tar", SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar tar ->
tar.archiveExtension = 'tar.bz2'
tar.compression = Compression.BZIP2
tar.preserveFileTimestamps = preserveFileTimestamps
@ -33,7 +33,7 @@ task buildBZip2Tar(type: SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar
}
}
task buildGZipTar(type: SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar tar ->
tasks.register("buildGZipTar", SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar tar ->
tar.archiveExtension = 'tar.gz'
tar.compression = Compression.GZIP
tar.preserveFileTimestamps = preserveFileTimestamps
@ -43,7 +43,7 @@ task buildGZipTar(type: SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar t
}
}
task buildTar(type: SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar tar ->
tasks.register("buildTar", SymbolicLinkPreservingTar) { SymbolicLinkPreservingTar tar ->
tar.archiveExtension = 'tar'
tar.preserveFileTimestamps = preserveFileTimestamps
from fileTree(source)

View File

@ -28,9 +28,7 @@ allprojects {
}
project(':empty_test_task') {
task emptyTest(type: Test) {
}
tasks.register("emptyTest", Test)
}
project(':all_classes_in_tasks') {

View File

@ -23,7 +23,7 @@ repositories {
jcenter()
}
configurations.create("forbiddenApisCliJar")
configurations.register("forbiddenApisCliJar")
dependencies {
forbiddenApisCliJar 'de.thetaphi:forbiddenapis:2.7'
@ -31,12 +31,12 @@ dependencies {
compile "org.${project.properties.compileGroup}:${project.properties.compileVersion}"
}
task empty(type: ThirdPartyAuditTask) {
tasks.register("empty", ThirdPartyAuditTask) {
targetCompatibility = JavaVersion.VERSION_11
signatureFile = file('third-party-audit-empty.txt')
}
task absurd(type: ThirdPartyAuditTask) {
tasks.register("absurd", ThirdPartyAuditTask) {
targetCompatibility = JavaVersion.VERSION_11
signatureFile = file('third-party-audit-absurd.txt')
}

View File

@ -8,14 +8,11 @@ dependencies {
compile 'org.apache.logging.log4j:log4j-core:2.11.1'
}
// Tests have to clean mid-test but we don't want the sample jars to go away
clean.enabled = false
["0.0.1", "0.0.2"].forEach { v ->
["elasticsearch", "other"].forEach { p ->
task "dummy-${p}-${v}"(type: Jar) {
tasks.register("dummy-${p}-${v}", Jar) {
destinationDir = file("${buildDir}/testrepo/org/${p}/gradle/dummy-io/${v}/")
archiveName = "dummy-io-${v}.jar"
archiveFileName = "dummy-io-${v}.jar"
from sourceSets.main.output
include "**/TestingIO.class"
if (v == "0.0.2") {
@ -32,9 +29,9 @@ clean.enabled = false
["0.0.1"].forEach { v ->
["elasticsearch", "other"].forEach { p ->
task "broken-log4j-${p}-${v}"(type: Jar) {
tasks.register("broken-log4j-${p}-${v}", Jar) {
destinationDir = file("${buildDir}/testrepo/org/${p}/gradle/broken-log4j/${v}/")
archiveName = "broken-log4j-${v}.jar"
archiveFileName = "broken-log4j-${v}.jar"
from sourceSets.main.output
include "**/TestingLog4j.class"
}
@ -42,11 +39,11 @@ clean.enabled = false
}
}
task jarhellJdk(type: Jar) {
tasks.register("jarhellJdk", Jar) {
destinationDir = file("${buildDir}/testrepo/org/other/gradle/jarhellJdk/0.0.1/")
archiveName = "jarhellJdk-0.0.1.jar"
archiveFileName = "jarhellJdk-0.0.1.jar"
from sourceSets.main.output
include "**/String.class"
into "java/lang"
build.dependsOn("jarhellJdk")
}
build.dependsOn("jarhellJdk")

View File

@ -157,7 +157,7 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
Closure createRunBwcGradleTask = { name, extraConfig ->
return tasks.create(name: "$name", type: LoggedExec) {
return tasks.register("$name", LoggedExec) {
dependsOn checkoutBwcBranch
spoolOutput = true
workingDir = checkoutDir
@ -225,10 +225,11 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
return "buildBwc${projectName.replaceAll(/-\w/) { it[1].toUpperCase() }.capitalize()}"
}
task buildBwc {}
def buildBwc = tasks.register("buildBwc");
Closure createBuildBwcTask = { projectName, projectDir, projectArtifact ->
Task bwcTask = createRunBwcGradleTask(buildBwcTaskName(projectName)) {
def bwcTaskName = buildBwcTaskName(projectName)
createRunBwcGradleTask(bwcTaskName) {
inputs.file("${project.buildDir}/refspec")
outputs.files(projectArtifact)
outputs.cacheIf("BWC distribution caching is disabled on 'master' branch") {
@ -245,7 +246,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
}
}
}
buildBwc.dependsOn bwcTask
buildBwc.configure {
dependsOn(bwcTaskName)
}
}
Map<String, File> artifactFiles = [:]

View File

@ -32,7 +32,7 @@ esplugin {
// No unit tests in this example
test.enabled = false
task exampleFixture(type: org.elasticsearch.gradle.test.AntFixture) {
tasks.register("exampleFixture", org.elasticsearch.gradle.test.AntFixture) {
dependsOn testClasses
env 'CLASSPATH', "${-> project.sourceSets.test.runtimeClasspath.asPath}"
executable = "${BuildParams.runtimeJavaHome}/bin/java"

View File

@ -98,7 +98,7 @@ String krb5conf = project(':test:fixtures:krb5kdc-fixture').ext.krb5Conf("hdfs")
// Create HDFS File System Testing Fixtures for HA/Secure combinations
for (String fixtureName : ['hdfsFixture', 'haHdfsFixture', 'secureHdfsFixture', 'secureHaHdfsFixture']) {
project.tasks.create(fixtureName, org.elasticsearch.gradle.test.AntFixture) {
def tsk = project.tasks.register(fixtureName, org.elasticsearch.gradle.test.AntFixture) {
dependsOn project.configurations.hdfsFixture, project(':test:fixtures:krb5kdc-fixture').tasks.postProcessFixture
executable = "${BuildParams.runtimeJavaHome}/bin/java"
env 'CLASSPATH', "${-> project.configurations.hdfsFixture.asPath}"
@ -137,6 +137,10 @@ for (String fixtureName : ['hdfsFixture', 'haHdfsFixture', 'secureHdfsFixture',
args miniHDFSArgs.toArray()
}
// TODO: The task configuration block has side effects that require it currently to be always executed.
// Otherwise tests start failing. Therefore we enforce the task creation for now.
tsk.get()
}
Set disabledIntegTestTaskNames = []

View File

@ -54,20 +54,20 @@ for (Version bwcVersion : BuildParams.bwcVersions.wireCompatible) {
for (kind in ["follower", "leader"]) {
// Attention!! Groovy trap: do not pass `kind` to a closure
tasks.create("${baseName}#${kind}#clusterTest", RestTestRunnerTask) {
tasks.register("${baseName}#${kind}#clusterTest", RestTestRunnerTask) {
systemProperty 'tests.rest.upgrade_state', 'none'
systemProperty 'tests.rest.cluster_name', kind
ext.kindExt = kind
}
tasks.create("${baseName}#${kind}#oneThirdUpgradedTest", RestTestRunnerTask) {
tasks.register("${baseName}#${kind}#oneThirdUpgradedTest", RestTestRunnerTask) {
systemProperty 'tests.rest.upgrade_state', 'one_third'
systemProperty 'tests.rest.cluster_name', kind
dependsOn "${baseName}#leader#clusterTest", "${baseName}#follower#clusterTest"
ext.kindExt = kind
}
tasks.create("${baseName}#${kind}#twoThirdsUpgradedTest", RestTestRunnerTask) {
tasks.register("${baseName}#${kind}#twoThirdsUpgradedTest", RestTestRunnerTask) {
systemProperty 'tests.rest.upgrade_state', 'two_third'
systemProperty 'tests.rest.cluster_name', kind
dependsOn "${baseName}#${kind}#oneThirdUpgradedTest"