Files
kst4contest/packaging/AddModules.java
T
Rsclub2_2andClaude Opus 5 f014b4697b packaging: derive the jpackage module list from module-info.java instead of maintaining sixteen copies
The jdk.net incident was caused by duplication rather than by a single
oversight: the module list existed in module-info.java, in the jpackage Maven
plugin and in fourteen hardcoded --add-modules arguments across the workflows
and AUR PKGBUILDs. Only the path that CI does not use was kept up to date, so
every packaged build shipped a runtime image without jdk.net.

Add packaging/AddModules.java, a single file source program that reads the
requires clauses and prints the platform modules. It runs identically on the
Linux, macOS and Windows runners without a build step, and skips third party
requires such as jlayer, which is an automatic module and cannot be linked
into a runtime image at all, as well as test only requires and requires
static. All sixteen packaging call sites now resolve the list through it, so
they can no longer drift from the descriptor.

The jpackage Maven plugin takes its modules as individual XML elements and
cannot consume a generated value, so it remains a second copy. To keep it
honest the helper has a pom verification mode, bound to the validate phase
via exec-maven-plugin. Binding it to the build rather than to a workflow
trigger means it also fires on direct pushes to main, on tagged releases, in
both AUR PKGBUILDs and on local builds, none of which run the pull request
check.

The released AUR PKGBUILD builds from a tag tarball that may predate the
helper, and aur-publish.yml rewrites pkgver to the latest release, so it
falls back to the list carried in that tarball's own pom.xml.

Verified that the generated list produces a byte identical runtime image to
the previous hardcoded one, that removing a requires fails the build with a
precise diff, and that the pull request check and the push triggered nightly
AppImage job both succeed under act.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Generated-By: Claude Code (Claude Opus 5)
2026-08-15 01:05:45 +02:00

129 lines
4.5 KiB
Java

/*
* Derives the jpackage --add-modules list from module-info.java so that the
* packaging scripts never drift from the module descriptor again.
*
* Run as a single file source program, which behaves identically on the Linux,
* macOS and Windows runners:
*
* java packaging/AddModules.java print the module list
* java packaging/AddModules.java --verify-pom fail if pom.xml drifted
*
* Only platform modules are emitted. Third party requires such as jlayer are
* skipped because they are supplied as ordinary jars on the class path, and
* automatic modules cannot be linked into a runtime image at all. Test only
* requires such as org.junit.jupiter.api are skipped for the same reason.
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class AddModules {
private static final Path DESCRIPTOR =
Path.of("src", "main", "java", "module-info.java");
private static final Path POM = Path.of("pom.xml");
/** Matches "requires [transitive] [static] some.module;" in any order. */
private static final Pattern REQUIRES = Pattern.compile(
"requires\\s+((?:transitive\\s+|static\\s+)*)([A-Za-z0-9_.]+)\\s*;");
private static final Pattern ADD_MODULE =
Pattern.compile("<addmodule>\\s*([A-Za-z0-9_.]+)\\s*</addmodule>");
private static final Pattern BLOCK_COMMENT =
Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL);
private static final Pattern LINE_COMMENT = Pattern.compile("//[^\\n]*");
private AddModules() {
}
public static void main(String[] args) throws IOException {
boolean verifyPom = args.length > 0 && "--verify-pom".equals(args[0]);
Set<String> required = platformModules(read(DESCRIPTOR));
if (required.isEmpty()) {
fail("No platform modules found in " + DESCRIPTOR);
}
if (!verifyPom) {
System.out.println(String.join(",", required));
return;
}
Set<String> declared = new TreeSet<>();
Matcher matcher = ADD_MODULE.matcher(read(POM));
while (matcher.find()) {
declared.add(matcher.group(1));
}
if (declared.equals(required)) {
System.out.println("pom.xml <addmodules> matches module-info.java ("
+ required.size() + " modules)");
return;
}
Set<String> missing = new TreeSet<>(required);
missing.removeAll(declared);
Set<String> extra = new TreeSet<>(declared);
extra.removeAll(required);
System.err.println("pom.xml <addmodules> drifted from module-info.java.");
if (!missing.isEmpty()) {
System.err.println(" missing in pom.xml: " + String.join(", ", missing));
}
if (!extra.isEmpty()) {
System.err.println(" not required by module-info.java: "
+ String.join(", ", extra));
}
System.err.println(" expected: " + String.join(",", required));
System.exit(1);
}
/** Returns the platform modules required by the given descriptor, sorted. */
static Set<String> platformModules(String source) {
String stripped = LINE_COMMENT.matcher(
BLOCK_COMMENT.matcher(source).replaceAll(" ")).replaceAll(" ");
Set<String> modules = new TreeSet<>();
Matcher matcher = REQUIRES.matcher(stripped);
while (matcher.find()) {
// "requires static" is a compile time only dependency and must not
// be linked into the shipped runtime image.
if (matcher.group(1).contains("static")) {
continue;
}
String module = matcher.group(2);
if (isPlatformModule(module)) {
modules.add(module);
}
}
return modules;
}
private static boolean isPlatformModule(String module) {
return module.startsWith("java.")
|| module.startsWith("jdk.")
|| module.startsWith("javafx.");
}
private static String read(Path path) throws IOException {
if (!Files.isRegularFile(path)) {
fail("Not found: " + path.toAbsolutePath()
+ " (run this from the repository root)");
}
return Files.readString(path);
}
private static void fail(String message) {
System.err.println(message);
System.exit(2);
}
}