|
20 | 20 |
|
21 | 21 | import org.apache.commons.io.output.NullOutputStream; |
22 | 22 | import org.elasticsearch.gradle.JdkJarHellCheck; |
23 | | -import org.elasticsearch.test.NamingConventionsCheck; |
24 | 23 | import org.gradle.api.DefaultTask; |
25 | 24 | import org.gradle.api.GradleException; |
26 | 25 | import org.gradle.api.JavaVersion; |
27 | 26 | import org.gradle.api.artifacts.Configuration; |
28 | 27 | import org.gradle.api.file.FileCollection; |
| 28 | +import org.gradle.api.file.FileTree; |
29 | 29 | import org.gradle.api.tasks.Input; |
30 | 30 | import org.gradle.api.tasks.InputFile; |
31 | 31 | import org.gradle.api.tasks.InputFiles; |
|
47 | 47 | import java.util.regex.Matcher; |
48 | 48 | import java.util.regex.Pattern; |
49 | 49 | import java.util.stream.Collectors; |
| 50 | +import java.util.stream.IntStream; |
50 | 51 |
|
51 | 52 | public class ThirdPartyAuditTask extends DefaultTask { |
52 | 53 |
|
@@ -171,19 +172,38 @@ private void extractJars(FileCollection jars) { |
171 | 172 | File jarExpandDir = getJarExpandDir(); |
172 | 173 | // We need to clean up to make sure old dependencies don't linger |
173 | 174 | getProject().delete(jarExpandDir); |
174 | | - jars.forEach(jar -> |
| 175 | + |
| 176 | + jars.forEach(jar -> { |
| 177 | + FileTree jarFiles = getProject().zipTree(jar); |
175 | 178 | getProject().copy(spec -> { |
| 179 | + spec.from(jarFiles); |
| 180 | + spec.into(jarExpandDir); |
| 181 | + // exclude classes from multi release jars |
| 182 | + spec.exclude("META-INF/versions/**"); |
| 183 | + }); |
| 184 | + // Deal with multi release jars: |
| 185 | + // The order is important, we iterate here so we don't depend on the order in which Gradle executes the spec |
| 186 | + // We extract multi release jar classes ( if these exist ) going from 9 - the first to support them, to the |
| 187 | + // current `targetCompatibility` version. |
| 188 | + // Each extract will overwrite the top level classes that existed before it, the result is that we end up |
| 189 | + // with a single version of the class in `jarExpandDir`. |
| 190 | + // This will be the closes version to `targetCompatibility`, the same class that would be loaded in a JVM |
| 191 | + // that has `targetCompatibility` version. |
| 192 | + // This means we only scan classes that would be loaded into `targetCompatibility`, and don't look at any |
| 193 | + // pther version specific implementation of said classes. |
| 194 | + IntStream.rangeClosed( |
| 195 | + Integer.parseInt(JavaVersion.VERSION_1_9.getMajorVersion()), |
| 196 | + Integer.parseInt(targetCompatibility.getMajorVersion()) |
| 197 | + ).forEach(majorVersion -> getProject().copy(spec -> { |
176 | 198 | spec.from(getProject().zipTree(jar)); |
177 | 199 | spec.into(jarExpandDir); |
178 | | - // Exclude classes for multi release jars above target |
179 | | - for (int i = Integer.parseInt(targetCompatibility.getMajorVersion()) + 1; |
180 | | - i <= Integer.parseInt(JavaVersion.VERSION_HIGHER.getMajorVersion()); |
181 | | - i++ |
182 | | - ) { |
183 | | - spec.exclude("META-INF/versions/" + i + "/**"); |
184 | | - } |
185 | | - }) |
186 | | - ); |
| 200 | + String metaInfPrefix = "META-INF/versions/" + majorVersion; |
| 201 | + spec.include(metaInfPrefix + "/**"); |
| 202 | + // Drop the version specific prefix |
| 203 | + spec.eachFile(details -> details.setPath(details.getPath().replace(metaInfPrefix, ""))); |
| 204 | + spec.setIncludeEmptyDirs(false); |
| 205 | + })); |
| 206 | + }); |
187 | 207 | } |
188 | 208 |
|
189 | 209 | private void assertNoJarHell(Set<String> jdkJarHellClasses) { |
@@ -276,9 +296,9 @@ private String formatClassList(Set<String> classList) { |
276 | 296 | private Set<String> runJdkJarHellCheck() throws IOException { |
277 | 297 | ByteArrayOutputStream standardOut = new ByteArrayOutputStream(); |
278 | 298 | ExecResult execResult = getProject().javaexec(spec -> { |
279 | | - URL location = NamingConventionsCheck.class.getProtectionDomain().getCodeSource().getLocation(); |
| 299 | + URL location = JdkJarHellCheck.class.getProtectionDomain().getCodeSource().getLocation(); |
280 | 300 | if (location.getProtocol().equals("file") == false) { |
281 | | - throw new GradleException("Unexpected location for NamingConventionCheck class: " + location); |
| 301 | + throw new GradleException("Unexpected location for JdkJarHellCheck class: " + location); |
282 | 302 | } |
283 | 303 | try { |
284 | 304 | spec.classpath( |
|
0 commit comments