Make a new ant builder per AntTask invocation

This commit is contained in:
Ryan Ernst 2015-12-18 12:01:54 -08:00
parent 9f1dfdbaea
commit 5b9bf8e738
3 changed files with 20 additions and 36 deletions

View File

@ -47,26 +47,26 @@ public class AntTask extends DefaultTask {
@TaskAction @TaskAction
final void executeTask() { final void executeTask() {
// capture the current loggers AntBuilder ant = new AntBuilder()
List<BuildLogger> savedLoggers = new ArrayList<>();
for (BuildListener l : project.ant.project.getBuildListeners()) { // remove existing loggers, we add our own
List<BuildLogger> toRemove = new ArrayList<>();
for (BuildListener l : ant.project.getBuildListeners()) {
if (l instanceof BuildLogger) { if (l instanceof BuildLogger) {
savedLoggers.add(l); toRemove.add(l);
} }
} }
// remove them for (BuildLogger l : toRemove) {
for (BuildLogger l : savedLoggers) { ant.project.removeBuildListener(l)
project.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()) final PrintStream stream = useStdout() ? System.out : new PrintStream(outputBuffer, true, Charset.defaultCharset().name())
BuildLogger antLogger = makeLogger(stream, outputLevel) BuildLogger antLogger = makeLogger(stream, outputLevel)
// now run the command with just our logger ant.project.addBuildListener(antLogger)
project.ant.project.addBuildListener(antLogger)
try { try {
runAnt(project.ant) runAnt(ant)
} catch (BuildException e) { } catch (BuildException e) {
// ant failed, so see if we have buffered output to emit, then rethrow the failure // ant failed, so see if we have buffered output to emit, then rethrow the failure
String buffer = outputBuffer.toString() String buffer = outputBuffer.toString()
@ -74,12 +74,6 @@ public class AntTask extends DefaultTask {
logger.error("=== Ant output ===\n${buffer}") logger.error("=== Ant output ===\n${buffer}")
} }
throw e throw e
} finally {
project.ant.project.removeBuildListener(antLogger)
// add back the old loggers before returning
for (BuildLogger l : savedLoggers) {
project.ant.project.addBuildListener(l)
}
} }
} }

View File

@ -35,16 +35,13 @@ public class LicenseHeadersTask extends AntTask {
LicenseHeadersTask() { LicenseHeadersTask() {
description = "Checks sources for missing, incorrect, or unacceptable license headers" 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 @Override
protected void runAnt(AntBuilder ant) { 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/ // create a file for the log to go to under reports/
File reportDir = new File(project.buildDir, "reports/licenseHeaders") File reportDir = new File(project.buildDir, "reports/licenseHeaders")

View File

@ -18,24 +18,19 @@
*/ */
package org.elasticsearch.gradle.precommit package org.elasticsearch.gradle.precommit
import org.apache.tools.ant.BuildLogger
import org.apache.tools.ant.DefaultLogger import org.apache.tools.ant.DefaultLogger
import org.apache.tools.ant.Project
import org.elasticsearch.gradle.AntTask import org.elasticsearch.gradle.AntTask
import org.gradle.api.artifacts.Configuration 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.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.SimpleFileVisitor import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes 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 * Basic static checking to keep tabs on third party JARs
*/ */
@ -50,10 +45,6 @@ public class ThirdPartyAuditTask extends AntTask {
ThirdPartyAuditTask() { ThirdPartyAuditTask() {
dependsOn(project.configurations.testCompile) dependsOn(project.configurations.testCompile)
description = "Checks third party JAR bytecode for missing classes, use of internal APIs, and other horrors'" 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 @Override
protected void runAnt(AntBuilder ant) { protected void runAnt(AntBuilder ant) {
ant.project.addTaskDefinition('thirdPartyAudit', de.thetaphi.forbiddenapis.ant.AntTask)
// we only want third party dependencies. // we only want third party dependencies.
FileCollection jars = project.configurations.testCompile.fileCollection({ dependency -> FileCollection jars = project.configurations.testCompile.fileCollection({ dependency ->
dependency.group.startsWith("org.elasticsearch") == false dependency.group.startsWith("org.elasticsearch") == false