6
6
* Side Public License, v 1.
7
7
*/
8
8
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 ;
23
28
24
29
/**
25
30
* A task which will run ant commands.
@@ -32,66 +37,66 @@ public abstract class AntTask extends DefaultTask {
32
37
* A buffer that will contain the output of the ant code run,
33
38
* if the output was not already written directly to stdout.
34
39
*/
35
- public final ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream ()
40
+ public final ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream ();
36
41
37
42
@ Inject
38
43
protected FileSystemOperations getFileSystemOperations () {
39
44
throw new UnsupportedOperationException ();
40
45
}
41
46
42
47
@ TaskAction
43
- final void executeTask () {
44
- AntBuilder ant = new AntBuilder ()
48
+ final void executeTask () throws UnsupportedEncodingException {
49
+ AntBuilder ant = getAnt ();
45
50
46
51
// remove existing loggers, we add our own
47
52
List <BuildLogger > toRemove = new ArrayList <>();
48
- for (BuildListener listener : ant. project . getBuildListeners()) {
53
+ for (BuildListener listener : ant .getProject () .getBuildListeners ()) {
49
54
if (listener instanceof BuildLogger ) {
50
- toRemove. add(listener);
55
+ toRemove .add (( BuildLogger ) listener );
51
56
}
52
57
}
53
58
for (BuildLogger listener : toRemove ) {
54
- ant. project . removeBuildListener(listener)
59
+ ant .getProject () .removeBuildListener (listener );
55
60
}
56
61
57
62
// otherwise groovy replaces System.out, and you have no chance to debug
58
63
// ant.saveStreams = false
59
64
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 );
63
68
64
- ant. project . addBuildListener(antLogger)
69
+ ant .getProject () .addBuildListener (antLogger );
65
70
try {
66
- runAnt(ant)
71
+ runAnt (ant );
67
72
} catch (Exception e ) {
68
73
// 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 ();
70
75
if (buffer .isEmpty () == false ) {
71
- logger . error(" === Ant output ===\n ${ buffer} " )
76
+ getLogger () .error ("=== Ant output ===\n ${buffer}" );
72
77
}
73
- throw e
78
+ throw e ;
74
79
}
75
80
}
76
81
77
82
/** 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 );
79
84
80
85
/** Create the logger the ant runner will use, with the given stream for error/output. */
81
86
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 ;
86
92
}
87
93
88
94
/**
89
95
* Returns true if the ant logger should write to stdout, or false if to the buffer.
90
96
* The default implementation writes to the buffer when gradle info logging is disabled.
91
97
*/
92
98
protected boolean useStdout () {
93
- return logger . isInfoEnabled()
99
+ return getLogger () .isInfoEnabled ();
94
100
}
95
101
96
-
97
102
}
0 commit comments