|
| 1 | +import java.io.DataInputStream; |
| 2 | +import java.io.File; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.lang.module.Configuration; |
| 6 | +import java.lang.module.FindException; |
| 7 | +import java.lang.module.ModuleDescriptor; |
| 8 | +import java.lang.module.ModuleFinder; |
| 9 | +import java.lang.module.ResolutionException; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.TreeMap; |
| 17 | +import java.util.jar.JarEntry; |
| 18 | +import java.util.jar.JarFile; |
| 19 | + |
| 20 | +public class JarCheck { |
| 21 | + public static void main(String[] args) throws IOException { |
| 22 | + Path jar = Path.of(args[0]); |
| 23 | + int release = Integer.parseInt(args[1]); |
| 24 | + int expectedMajor = release + 44; |
| 25 | + String runtimeClasspath = args[2]; |
| 26 | + List<String> expectedClasses = Arrays.asList(args).subList(3, args.length); |
| 27 | + List<String> problems = new ArrayList<>(); |
| 28 | + |
| 29 | + TreeMap<Integer, List<String>> byMajor = new TreeMap<>(); |
| 30 | + ModuleDescriptor descriptor = null; |
| 31 | + try (JarFile jarFile = new JarFile(jar.toFile())) { |
| 32 | + for (String expected : expectedClasses) { |
| 33 | + if (jarFile.getJarEntry(expected.replace('.', '/') + ".class") == null) { |
| 34 | + problems.add("The jar has no " + expected + ". Check <sourceDirectory> in pom.xml: Kotlin and Java sources live in src/main/kotlin."); |
| 35 | + } |
| 36 | + } |
| 37 | + for (JarEntry entry : Collections.list(jarFile.entries())) { |
| 38 | + if (!entry.getName().endsWith(".class")) continue; |
| 39 | + try (DataInputStream data = new DataInputStream(jarFile.getInputStream(entry))) { |
| 40 | + data.readInt(); |
| 41 | + data.readUnsignedShort(); |
| 42 | + int major = data.readUnsignedShort(); |
| 43 | + byMajor.computeIfAbsent(major, k -> new ArrayList<>()).add(entry.getName()); |
| 44 | + } |
| 45 | + } |
| 46 | + JarEntry moduleInfo = jarFile.getJarEntry("module-info.class"); |
| 47 | + if (moduleInfo != null) { |
| 48 | + try (InputStream in = jarFile.getInputStream(moduleInfo)) { |
| 49 | + descriptor = ModuleDescriptor.read(in); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + byMajor.forEach((major, classes) -> { |
| 54 | + if (major != expectedMajor) { |
| 55 | + problems.add(classes.size() + " classes are Java " + (major - 44) + " bytecode (major " + major + "), expected Java " + release |
| 56 | + + " (major " + expectedMajor + "), e.g. " + classes.get(0) |
| 57 | + + ". Set <jvmTarget>${java.version}</jvmTarget> on kotlin-maven-plugin and <release>${java.version}</release> on maven-compiler-plugin."); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + if (descriptor == null) { |
| 62 | + problems.add("The jar has no module-info.class. Keep src/main/kotlin/module-info.java, which maven-compiler-plugin compiles into the jar."); |
| 63 | + } else { |
| 64 | + List<Path> modulePath = new ArrayList<>(); |
| 65 | + modulePath.add(jar); |
| 66 | + for (String entry : runtimeClasspath.split(File.pathSeparator)) { |
| 67 | + if (!entry.isEmpty() && !Path.of(entry).equals(jar)) modulePath.add(Path.of(entry)); |
| 68 | + } |
| 69 | + try { |
| 70 | + Configuration.resolve(ModuleFinder.of(modulePath.toArray(Path[]::new)), List.of(ModuleLayer.boot().configuration()), |
| 71 | + ModuleFinder.of(), Set.of(descriptor.name())); |
| 72 | + } catch (FindException | ResolutionException e) { |
| 73 | + problems.add("A JPMS application with only this jar's compile and runtime dependencies can't start: " + e.getMessage() |
| 74 | + + ". A module from a provided or optional dependency must be 'requires static' in module-info.java."); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (!problems.isEmpty()) { |
| 79 | + System.err.println("Jar check failed for " + jar.getFileName() + ":"); |
| 80 | + problems.forEach(p -> System.err.println(" " + p)); |
| 81 | + System.exit(1); |
| 82 | + } |
| 83 | + int classCount = byMajor.values().stream().mapToInt(List::size).sum(); |
| 84 | + System.out.println("Jar check passed for " + jar.getFileName() + ": " + classCount + " classes, all major " + expectedMajor |
| 85 | + + ", module " + descriptor.name() + " resolves against the runtime classpath."); |
| 86 | + } |
| 87 | +} |
0 commit comments