Created
March 20, 2024 14:13
-
-
Save mucharafal/7622887ebc54485f4d1a722543a643ba to your computer and use it in GitHub Desktop.
Finding duplicated classes on classpath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task checkClassPath { | |
ext { | |
inConfig = getConfigurations().findByName("runtimeClasspath") | |
} | |
inputs.files(inConfig) | |
doLast { | |
Configuration myConfig = inConfig; | |
Collection<Tuple<Object>> fileTrees = myConfig.resolve().stream().map(jar -> Tuple.of(jar, zipTree(jar))).collect() | |
HashMap<String, Set<String>> classToJars = new HashMap<>() | |
fileTrees.forEach( | |
(File file, FileTree tree) -> { | |
Set<File> classes = tree.getFiles() | |
classes.forEach((File classFile) -> { | |
java.nio.file.Path currentPath = classFile.toPath() | |
java.nio.file.Path parent = currentPath.getParent() | |
while (parent != null && !parent.toFile().getName().equals("expandedArchives")) { | |
currentPath = parent | |
parent = currentPath.getParent() | |
} | |
String jarName = currentPath.toFile().getName().split("_")[0] | |
java.nio.file.Path classRelativePath = currentPath.relativize(classFile.toPath()) | |
classToJars.get(classRelativePath.toString(), new HashSet()).add(jarName) | |
}) | |
} | |
) | |
for (String className : classToJars.keySet()) { | |
Set<String> jars = classToJars.get(className) | |
if (jars.size() > 1) { | |
print("${className} duplicated in ${jars}\n") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment