Created
April 9, 2025 12:51
-
-
Save lucamolteni/bcf034a4f5e86a862c3dc1114bfeb5bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.io.*; | |
import java.nio.file.*; | |
import java.util.*; | |
import java.util.jar.*; | |
import java.util.zip.*; | |
public class CheckSplitPackages { | |
public static void main(String[] args) throws IOException { | |
var rootDir = Paths.get(args.length > 0 ? args[0] : "."); | |
System.out.println("π Scanning all JARs under: " + rootDir + " (excluding test JARs)"); | |
var jarFiles = Files.walk(rootDir) | |
.filter(p -> p.toString().endsWith(".jar")) | |
.filter(p -> !p.getFileName().toString().endsWith("-tests.jar")) | |
.toList(); | |
System.out.println("Found " + jarFiles.size() + " non-test JAR files to analyze"); | |
var packageMap = new HashMap<String, String>(); | |
var seen = new HashMap<String, Set<String>>(); | |
var foundConflict = false; | |
for (var jarPath : jarFiles) { | |
var jarName = jarPath.getFileName().toString(); | |
try (var jar = new JarFile(jarPath.toFile())) { | |
jar.stream() | |
.filter(e -> !e.isDirectory() && e.getName().endsWith(".class")) | |
.map(e -> { | |
var pkg = e.getName().contains("/") ? | |
e.getName().substring(0, e.getName().lastIndexOf('/')).replace('/', '.') : | |
""; | |
return pkg; | |
}) | |
.filter(pkg -> !pkg.isEmpty()) | |
.forEach(pkg -> { | |
var previous = packageMap.putIfAbsent(pkg, jarName); | |
if (previous != null && !previous.equals(jarName)) { | |
var jars = seen.computeIfAbsent(pkg, k -> new LinkedHashSet<>()); | |
if (jars.add(jarName)) { | |
if (jars.size() == 1) { | |
System.out.println("π¨ Split package detected: " + pkg); | |
System.out.println(" β³ in: " + previous); | |
System.out.println(" β³ and: " + jarName); | |
} else { | |
System.out.println(" β³ also in: " + jarName); | |
} | |
foundConflict = true; | |
} | |
} | |
}); | |
} | |
} | |
if (!foundConflict) { | |
System.out.println("β No split packages found!"); | |
} else { | |
System.out.println("β Split packages detected β please fix before modularizing."); | |
System.exit(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment