diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/AntTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/AntTask.groovy index 1b904150077..7cd9b5b91a7 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/AntTask.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/AntTask.groovy @@ -47,26 +47,26 @@ public class AntTask extends DefaultTask { @TaskAction final void executeTask() { - // capture the current loggers - List savedLoggers = new ArrayList<>(); - for (BuildListener l : project.ant.project.getBuildListeners()) { + AntBuilder ant = new AntBuilder() + + // remove existing loggers, we add our own + List toRemove = new ArrayList<>(); + for (BuildListener l : ant.project.getBuildListeners()) { if (l instanceof BuildLogger) { - savedLoggers.add(l); + toRemove.add(l); } } - // remove them - for (BuildLogger l : savedLoggers) { - project.ant.project.removeBuildListener(l) + for (BuildLogger l : toRemove) { + ant.project.removeBuildListener(l) } - final int outputLevel = logger.isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO + final int outputLevel = logger.isDebugEnabled() ? Project.MSG_DEBUG : (logger.isInfoEnabled() ? Project.MSG_INFO : Project.MSG_WARN) final PrintStream stream = useStdout() ? System.out : new PrintStream(outputBuffer, true, Charset.defaultCharset().name()) BuildLogger antLogger = makeLogger(stream, outputLevel) - // now run the command with just our logger - project.ant.project.addBuildListener(antLogger) + ant.project.addBuildListener(antLogger) try { - runAnt(project.ant) + runAnt(ant) } catch (BuildException e) { // ant failed, so see if we have buffered output to emit, then rethrow the failure String buffer = outputBuffer.toString() @@ -74,12 +74,6 @@ public class AntTask extends DefaultTask { logger.error("=== Ant output ===\n${buffer}") } throw e - } finally { - project.ant.project.removeBuildListener(antLogger) - // add back the old loggers before returning - for (BuildLogger l : savedLoggers) { - project.ant.project.addBuildListener(l) - } } } diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/LicenseHeadersTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/LicenseHeadersTask.groovy index de36af886b8..39cf55c905b 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/LicenseHeadersTask.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/LicenseHeadersTask.groovy @@ -35,16 +35,13 @@ public class LicenseHeadersTask extends AntTask { LicenseHeadersTask() { description = "Checks sources for missing, incorrect, or unacceptable license headers" - - if (ant.project.taskDefinitions.contains('ratReport') == false) { - ant.project.addTaskDefinition('ratReport', Report) - ant.project.addDataTypeDefinition('substringMatcher', SubstringLicenseMatcher) - ant.project.addDataTypeDefinition('approvedLicense', SimpleLicenseFamily) - } } @Override protected void runAnt(AntBuilder ant) { + ant.project.addTaskDefinition('ratReport', Report) + ant.project.addDataTypeDefinition('substringMatcher', SubstringLicenseMatcher) + ant.project.addDataTypeDefinition('approvedLicense', SimpleLicenseFamily) // create a file for the log to go to under reports/ File reportDir = new File(project.buildDir, "reports/licenseHeaders") diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/ThirdPartyAuditTask.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/ThirdPartyAuditTask.groovy index 9ff348f20bb..69db1e773e8 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/ThirdPartyAuditTask.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/ThirdPartyAuditTask.groovy @@ -18,24 +18,19 @@ */ package org.elasticsearch.gradle.precommit +import org.apache.tools.ant.BuildLogger import org.apache.tools.ant.DefaultLogger +import org.apache.tools.ant.Project import org.elasticsearch.gradle.AntTask import org.gradle.api.artifacts.Configuration +import org.gradle.api.file.FileCollection -import java.nio.file.Files import java.nio.file.FileVisitResult +import java.nio.file.Files import java.nio.file.Path import java.nio.file.SimpleFileVisitor import java.nio.file.attribute.BasicFileAttributes -import org.gradle.api.DefaultTask -import org.gradle.api.artifacts.UnknownConfigurationException -import org.gradle.api.file.FileCollection -import org.gradle.api.tasks.TaskAction - -import org.apache.tools.ant.BuildLogger -import org.apache.tools.ant.Project - /** * Basic static checking to keep tabs on third party JARs */ @@ -50,10 +45,6 @@ public class ThirdPartyAuditTask extends AntTask { ThirdPartyAuditTask() { dependsOn(project.configurations.testCompile) description = "Checks third party JAR bytecode for missing classes, use of internal APIs, and other horrors'" - - if (ant.project.taskDefinitions.contains('thirdPartyAudit') == false) { - ant.project.addTaskDefinition('thirdPartyAudit', de.thetaphi.forbiddenapis.ant.AntTask) - } } /** @@ -103,6 +94,8 @@ public class ThirdPartyAuditTask extends AntTask { @Override protected void runAnt(AntBuilder ant) { + ant.project.addTaskDefinition('thirdPartyAudit', de.thetaphi.forbiddenapis.ant.AntTask) + // we only want third party dependencies. FileCollection jars = project.configurations.testCompile.fileCollection({ dependency -> dependency.group.startsWith("org.elasticsearch") == false