18
18
*/
19
19
package org.elasticsearch.gradle.precommit
20
20
21
+ import org.apache.tools.ant.DefaultLogger
22
+ import org.elasticsearch.gradle.AntTask
23
+ import org.gradle.api.artifacts.Configuration
24
+
21
25
import java.nio.file.Files
22
26
import java.nio.file.FileVisitResult
23
27
import java.nio.file.Path
@@ -35,7 +39,7 @@ import org.apache.tools.ant.Project
35
39
/**
36
40
* Basic static checking to keep tabs on third party JARs
37
41
*/
38
- public class ThirdPartyAuditTask extends DefaultTask {
42
+ public class ThirdPartyAuditTask extends AntTask {
39
43
40
44
// true to be lenient about MISSING CLASSES
41
45
private boolean missingClasses;
@@ -46,6 +50,10 @@ public class ThirdPartyAuditTask extends DefaultTask {
46
50
ThirdPartyAuditTask () {
47
51
dependsOn(project. configurations. testCompile)
48
52
description = " Checks third party JAR bytecode for missing classes, use of internal APIs, and other horrors'"
53
+
54
+ if (ant. project. taskDefinitions. contains(' thirdPartyAudit' ) == false ) {
55
+ ant. project. addTaskDefinition(' thirdPartyAudit' , de.thetaphi.forbiddenapis.ant.AntTask )
56
+ }
49
57
}
50
58
51
59
/**
@@ -84,65 +92,62 @@ public class ThirdPartyAuditTask extends DefaultTask {
84
92
return excludes;
85
93
}
86
94
87
- @TaskAction
88
- public void check () {
89
- AntBuilder ant = new AntBuilder ()
95
+ @Override
96
+ protected BuildLogger makeLogger (PrintStream stream , int outputLevel ) {
97
+ return new DefaultLogger (
98
+ errorPrintStream : stream,
99
+ outputPrintStream : stream,
100
+ // ignore passed in outputLevel for now, until we are filtering warning messages
101
+ messageOutputLevel : Project . MSG_ERR )
102
+ }
90
103
91
- // we are noisy for many reasons, working around performance problems with forbidden-apis, dealing
92
- // with warnings about missing classes, etc. so we use our own "quiet" AntBuilder
93
- ant. project. buildListeners. each { listener ->
94
- if (listener instanceof BuildLogger ) {
95
- listener. messageOutputLevel = Project . MSG_ERR ;
96
- }
97
- };
98
-
104
+ @Override
105
+ protected void runAnt (AntBuilder ant ) {
99
106
// we only want third party dependencies.
100
- FileCollection jars = project. configurations. testCompile. fileCollection({ dependency ->
107
+ FileCollection jars = project. configurations. testCompile. fileCollection({ dependency ->
101
108
dependency. group. startsWith(" org.elasticsearch" ) == false
102
109
})
103
-
110
+
104
111
// we don't want provided dependencies, which we have already scanned. e.g. don't
105
112
// scan ES core's dependencies for every single plugin
106
- try {
107
- jars - = project. configurations. getByName(" provided" )
108
- } catch (UnknownConfigurationException ignored) {}
109
-
113
+ Configuration provided = project. configurations. findByName(' provided' )
114
+ if (provided != null ) {
115
+ jars - = provided
116
+ }
117
+
110
118
// no dependencies matched, we are done
111
119
if (jars. isEmpty()) {
112
120
return ;
113
121
}
114
-
115
- ant. taskdef(name : " thirdPartyAudit" ,
116
- classname : " de.thetaphi.forbiddenapis.ant.AntTask" ,
117
- classpath : project. configurations. buildTools. asPath)
118
-
122
+
123
+
119
124
// print which jars we are going to scan, always
120
125
// this is not the time to try to be succinct! Forbidden will print plenty on its own!
121
126
Set<String > names = new HashSet<> ()
122
127
for (File jar : jars) {
123
128
names. add(jar. getName())
124
129
}
125
130
logger. error(" [thirdPartyAudit] Scanning: " + names)
126
-
131
+
127
132
// warn that classes are missing
128
133
// TODO: move these to excludes list!
129
134
if (missingClasses) {
130
135
logger. warn(" [thirdPartyAudit] WARNING: CLASSES ARE MISSING! Expect NoClassDefFoundError in bug reports from users!" )
131
136
}
132
-
133
- // TODO: forbidden-apis + zipfileset gives O(n^2) behavior unless we dump to a tmpdir first,
137
+
138
+ // TODO: forbidden-apis + zipfileset gives O(n^2) behavior unless we dump to a tmpdir first,
134
139
// and then remove our temp dir afterwards. don't complain: try it yourself.
135
140
// we don't use gradle temp dir handling, just google it, or try it yourself.
136
-
141
+
137
142
File tmpDir = new File (project. buildDir, ' tmp/thirdPartyAudit' )
138
-
143
+
139
144
// clean up any previous mess (if we failed), then unzip everything to one directory
140
145
ant. delete(dir : tmpDir. getAbsolutePath())
141
146
tmpDir. mkdirs()
142
147
for (File jar : jars) {
143
148
ant. unzip(src : jar. getAbsolutePath(), dest : tmpDir. getAbsolutePath())
144
149
}
145
-
150
+
146
151
// convert exclusion class names to binary file names
147
152
String [] excludedFiles = new String [excludes. length];
148
153
for (int i = 0 ; i < excludes. length; i++ ) {
@@ -152,12 +157,12 @@ public class ThirdPartyAuditTask extends DefaultTask {
152
157
throw new IllegalStateException (" bogus thirdPartyAudit exclusion: '" + excludes[i] + " ', not found in any dependency" )
153
158
}
154
159
}
155
-
160
+
156
161
// jarHellReprise
157
162
checkSheistyClasses(tmpDir. toPath(), new HashSet<> (Arrays . asList(excludedFiles)));
158
-
159
- ant. thirdPartyAudit(internalRuntimeForbidden : true ,
160
- failOnUnsupportedJava : false ,
163
+
164
+ ant. thirdPartyAudit(internalRuntimeForbidden : true ,
165
+ failOnUnsupportedJava : false ,
161
166
failOnMissingClasses : ! missingClasses,
162
167
classpath : project. configurations. testCompile. asPath) {
163
168
fileset(dir : tmpDir, excludes : excludedFiles. join(' ,' ))
@@ -169,7 +174,7 @@ public class ThirdPartyAuditTask extends DefaultTask {
169
174
/**
170
175
* check for sheisty classes: if they also exist in the extensions classloader, its jar hell with the jdk!
171
176
*/
172
- private void checkSheistyClasses (Path root , Set<String > excluded ) {
177
+ protected void checkSheistyClasses (Path root , Set<String > excluded ) {
173
178
// system.parent = extensions loader.
174
179
// note: for jigsaw, this evilness will need modifications (e.g. use jrt filesystem!).
175
180
// but groovy/gradle needs to work at all first!
0 commit comments