Skip to content

Commit 13870da

Browse files
committed
elastic#34459 replace groovy build-tools with java for AntTask
1 parent bf57b74 commit 13870da

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

build-tools-internal/src/main/groovy/org/elasticsearch/gradle/internal/test/AntFixture.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import org.elasticsearch.gradle.internal.test.Fixture
1515
import org.gradle.api.GradleException
1616
import org.gradle.api.tasks.Internal
1717
import org.gradle.api.tasks.TaskProvider
18+
import org.gradle.api.AntBuilder
1819

1920
/**
2021
* A fixture for integration tests which runs in a separate process launched by Ant.

build-tools-internal/src/main/groovy/org/elasticsearch/gradle/internal/AntTask.groovy renamed to build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/AntTask.java

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
* Side Public License, v 1.
77
*/
88

9-
package org.elasticsearch.gradle.internal
10-
11-
import org.apache.tools.ant.BuildListener
12-
import org.apache.tools.ant.BuildLogger
13-
import org.apache.tools.ant.DefaultLogger
14-
import org.apache.tools.ant.Project
15-
import org.gradle.api.DefaultTask
16-
import org.gradle.api.GradleException
17-
import org.gradle.api.file.FileSystemOperations
18-
import org.gradle.api.tasks.Input
19-
import org.gradle.api.tasks.TaskAction
20-
21-
import javax.inject.Inject
22-
import java.nio.charset.Charset
9+
package org.elasticsearch.gradle.internal;
10+
11+
import org.apache.tools.ant.BuildListener;
12+
import org.apache.tools.ant.BuildLogger;
13+
import org.apache.tools.ant.DefaultLogger;
14+
import org.apache.tools.ant.Project;
15+
import org.gradle.api.AntBuilder;
16+
import org.gradle.api.DefaultTask;
17+
import org.gradle.api.file.FileSystemOperations;
18+
import org.gradle.api.tasks.TaskAction;
19+
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.PrintStream;
22+
import java.io.UnsupportedEncodingException;
23+
import java.nio.charset.Charset;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
import javax.inject.Inject;
2328

2429
/**
2530
* A task which will run ant commands.
@@ -32,66 +37,66 @@ public abstract class AntTask extends DefaultTask {
3237
* A buffer that will contain the output of the ant code run,
3338
* if the output was not already written directly to stdout.
3439
*/
35-
public final ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream()
40+
public final ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
3641

3742
@Inject
3843
protected FileSystemOperations getFileSystemOperations() {
3944
throw new UnsupportedOperationException();
4045
}
4146

4247
@TaskAction
43-
final void executeTask() {
44-
AntBuilder ant = new AntBuilder()
48+
final void executeTask() throws UnsupportedEncodingException {
49+
AntBuilder ant = getAnt();
4550

4651
// remove existing loggers, we add our own
4752
List<BuildLogger> toRemove = new ArrayList<>();
48-
for (BuildListener listener : ant.project.getBuildListeners()) {
53+
for (BuildListener listener : ant.getProject().getBuildListeners()) {
4954
if (listener instanceof BuildLogger) {
50-
toRemove.add(listener);
55+
toRemove.add((BuildLogger) listener);
5156
}
5257
}
5358
for (BuildLogger listener : toRemove) {
54-
ant.project.removeBuildListener(listener)
59+
ant.getProject().removeBuildListener(listener);
5560
}
5661

5762
// otherwise groovy replaces System.out, and you have no chance to debug
5863
// ant.saveStreams = false
5964

60-
final int outputLevel = logger.isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO
61-
final PrintStream stream = useStdout() ? System.out : new PrintStream(outputBuffer, true, Charset.defaultCharset().name())
62-
BuildLogger antLogger = makeLogger(stream, outputLevel)
65+
final int outputLevel = getLogger().isDebugEnabled() ? Project.MSG_DEBUG : Project.MSG_INFO;
66+
final PrintStream stream = useStdout() ? System.out : new PrintStream(outputBuffer, true, Charset.defaultCharset().name());
67+
BuildLogger antLogger = makeLogger(stream, outputLevel);
6368

64-
ant.project.addBuildListener(antLogger)
69+
ant.getProject().addBuildListener(antLogger);
6570
try {
66-
runAnt(ant)
71+
runAnt(ant);
6772
} catch (Exception e) {
6873
// ant failed, so see if we have buffered output to emit, then rethrow the failure
69-
String buffer = outputBuffer.toString()
74+
String buffer = outputBuffer.toString();
7075
if (buffer.isEmpty() == false) {
71-
logger.error("=== Ant output ===\n${buffer}")
76+
getLogger().error("=== Ant output ===\n${buffer}");
7277
}
73-
throw e
78+
throw e;
7479
}
7580
}
7681

7782
/** Runs the doAnt closure. This can be overridden by subclasses instead of having to set a closure. */
78-
protected abstract void runAnt(AntBuilder ant)
83+
protected abstract void runAnt(AntBuilder ant);
7984

8085
/** Create the logger the ant runner will use, with the given stream for error/output. */
8186
protected BuildLogger makeLogger(PrintStream stream, int outputLevel) {
82-
return new DefaultLogger(
83-
errorPrintStream: stream,
84-
outputPrintStream: stream,
85-
messageOutputLevel: outputLevel)
87+
DefaultLogger logger = new DefaultLogger();
88+
logger.setErrorPrintStream(stream);
89+
logger.setOutputPrintStream(stream);
90+
logger.setMessageOutputLevel(outputLevel);
91+
return logger;
8692
}
8793

8894
/**
8995
* Returns true if the ant logger should write to stdout, or false if to the buffer.
9096
* The default implementation writes to the buffer when gradle info logging is disabled.
9197
*/
9298
protected boolean useStdout() {
93-
return logger.isInfoEnabled()
99+
return getLogger().isInfoEnabled();
94100
}
95101

96-
97102
}

0 commit comments

Comments
 (0)