8357404: jpackage should attempt to get a package version from the JDK's release file if the --version option is not specified [v7]

This commit is contained in:
alexander_matveev 2026-02-04 17:08:22 -08:00
parent 113b367d92
commit aaafb4c31e
14 changed files with 247 additions and 157 deletions

View File

@ -228,7 +228,7 @@ final class MacFromOptions {
var app = superAppBuilder.create();
if (isRuntimeInstaller(options) && !APP_VERSION.containsIn(options)) {
if (!APP_VERSION.containsIn(options)) {
// User didn't explicitly specify the version on the command line. jpackage derived it from the input.
// In this case it should ensure the derived value is valid MacOS version.
UnaryOperator<String> versionNormalizer = version -> {
@ -356,13 +356,13 @@ final class MacFromOptions {
static String normalizeVersion(String version) {
// macOS requires 1, 2 or 3 components version string.
// When reading from release file it can be 1 or 3 or maybe more.
// We always normalize to 3 components.
DottedVersion ver = DottedVersion.greedy(version);
BigInteger[] components = ver.getComponents();
if (components.length >= 4) {
return ver.toComponentsStringWithPadding(3);
// We will always normalize to 3 components if needed.
DottedVersion ver = DottedVersion.lazy(version);
if (ver.getComponentsCount() > 3) {
return ver.trim(3).pad(3).toComponentsString();
} else {
// We should drop any characters. For example: "-ea".
return ver.toComponentsString();
}
return version;
}
}

View File

@ -188,18 +188,15 @@ final class FromOptions {
appBuilder.appImageLayout(runtimeLayout);
if (!APP_VERSION.containsIn(options)) {
// Version is not specified explicitly. Try to get it from the release file.
final Path releaseFile = RuntimeImageUtils.getReleaseFilePath(
PREDEFINED_RUNTIME_IMAGE.getFrom(options));
try {
RuntimeVersionReader.readVersion(releaseFile)
.ifPresent(version -> {
appBuilder.version(version);
Log.verbose(I18N.format("message.release-version",
version, PREDEFINED_RUNTIME_IMAGE.getFrom(options)));
});
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
final var runtimeHome = RuntimeImageUtils.getRuntimeHomeForRuntimeRoot(
predefinedRuntimeImage.orElseThrow());
final var releaseFile = RuntimeImageUtils.getReleaseFilePath(runtimeHome);
RuntimeVersionReader.readVersion(releaseFile)
.ifPresent(version -> {
appBuilder.version(version.toString());
Log.verbose(I18N.format("message.release-version",
version.toString(), predefinedRuntimeImage.orElseThrow()));
});
}
} else {
appBuilder.appImageLayout(appLayout);

View File

@ -61,7 +61,9 @@ record ModuleInfo(String name, Optional<String> version, Optional<String> mainCl
// is linked in the runtime by simply analysing the data
// of `release` file.
final Path releaseFile = RuntimeImageUtils.getReleaseFilePath(cookedRuntime);
final var runtimeHome = RuntimeImageUtils.getRuntimeHomeForRuntimeRoot(
cookedRuntime);
final var releaseFile = RuntimeImageUtils.getReleaseFilePath(runtimeHome);
try (Reader reader = Files.newBufferedReader(releaseFile)) {
Properties props = new Properties();
props.load(reader);

View File

@ -224,21 +224,43 @@ public final class DottedVersion {
return Stream.of(components).map(BigInteger::toString).collect(Collectors.joining("."));
}
public String toComponentsStringWithPadding(int numberOfComponents) {
if (components.length >= numberOfComponents) {
return Stream.of(components).map(BigInteger::toString)
.limit(numberOfComponents).collect(Collectors.joining("."));
} else {
return toComponentsString()
.concat(".0".repeat(numberOfComponents - components.length));
public DottedVersion trim(int componentLimit) {
if (componentLimit < 0) {
throw new IllegalArgumentException();
}
if (components.length > componentLimit) {
components = Arrays.stream(components).limit(componentLimit)
.toArray(BigInteger[]::new);
}
return this;
}
public DottedVersion pad(int componentLimit) {
if (componentLimit <= 0) {
throw new IllegalArgumentException();
}
if (components.length < componentLimit) {
final int origLength = components.length;
components = Arrays.copyOf(components, componentLimit);
Arrays.fill(components, origLength, components.length,
new BigInteger("0"));
}
return this;
}
public int getComponentsCount() {
return components.length;
}
public BigInteger[] getComponents() {
return components;
}
private final BigInteger[] components;
private BigInteger[] components;
private final String value;
private final String suffix;
}

View File

@ -31,19 +31,22 @@ import jdk.internal.util.OperatingSystem;
public final class RuntimeImageUtils {
public static Path getReleaseFilePath(Path runtimePath) {
final Path releaseFile;
if (!OperatingSystem.isMacOS()) {
releaseFile = runtimePath.resolve("release");
} else {
// On Mac `runtimePath` can be runtime root or runtime home.
Path runtimeHome = runtimePath.resolve("Contents/Home");
if (!Files.isDirectory(runtimeHome)) {
runtimeHome = runtimePath;
}
releaseFile = runtimeHome.resolve("release");
}
public static Path getReleaseFilePath(Path runtimeHomePath) {
return runtimeHomePath.resolve("release");
}
return releaseFile;
/**
* Returns platform specific runtime "Home" location for given runtime root.
* <p>
* On Windows and Linux it will return value of runtimeRoot. On macOS for
* runtime image it will return runtimeRoot and for runtime bundle it will
* return path to "Home" folder.
*
* @param runtimeRoot the runtime root path
* @return platform specific runtime "Home" location for given runtime root
*/
public static Path getRuntimeHomeForRuntimeRoot(Path runtimeRoot) {
return MacBundle.fromPath(runtimeRoot).map(MacBundle::homeDir)
.orElse(runtimeRoot);
}
}

View File

@ -34,19 +34,31 @@ import java.util.Properties;
public final class RuntimeVersionReader {
public static Optional<String> readVersion(Path releaseFilePath) throws IOException {
if (!Files.isRegularFile(releaseFilePath)) {
return Optional.empty();
}
// jdk.tools.jlink.internal.plugins.ReleaseInfoPlugin uses java.util.Properties
// to read/write "release" file and puts quotes around version string.
// Implementation of this function is based on behavior of ReleaseInfoPlugin.
public static Optional<Runtime.Version> readVersion(Path releaseFilePath) {
try (Reader reader = Files.newBufferedReader(releaseFilePath)) {
Properties props = new Properties();
props.load(reader);
String version = props.getProperty("JAVA_VERSION");
if (version != null) {
// "JAVA_VERSION" value is set to quoted string in "release"
// file. getProperty() will include leading and trailing quote
// when returning value. We should remove them, since they are
// not part of version.
version = version.replaceAll("^\"|\"$", "");
}
return Optional.ofNullable(version);
return Optional.of(Runtime.Version.parse(version));
} catch (IllegalArgumentException | NullPointerException ex) {
// In case of Runtime.Version.parse() fails return empty optional.
// It will fail for "" or "foo" strings. ReleaseInfoPlugin class
// uses Runtime.Version class to create version for "release" file, so
// if Runtime.Version.parse() fails something is wrong and we should
// just ignore such version from "release" file.
return Optional.empty();
} catch (IOException ex) {
return Optional.empty();
}
}
}

View File

@ -83,7 +83,7 @@ final class WinFromOptions {
var app = appBuilder.create();
if (isRuntimeInstaller(options) && !APP_VERSION.containsIn(options)) {
if (!APP_VERSION.containsIn(options)) {
// User didn't explicitly specify the version on the command line. jpackage derived it from the input.
// In this case it should ensure the derived value is valid Windows version.
UnaryOperator<String> versionNormalizer = version -> {
@ -135,14 +135,13 @@ final class WinFromOptions {
static String normalizeVersion(String version) {
// Windows requires 2 or 4 components version string.
// When reading from release file it can be 1 or 3 or maybe more.
// We always normalize to 4 components.
DottedVersion ver = DottedVersion.greedy(version);
BigInteger[] components = ver.getComponents();
if (components.length == 1 || components.length == 3 ||
components.length >= 5) {
return ver.toComponentsStringWithPadding(4);
// We will always normalize to 4 components if needed.
DottedVersion ver = DottedVersion.lazy(version);
if (ver.getComponentsCount() != 2 || ver.getComponentsCount() != 4) {
return ver.trim(4).pad(4).toComponentsString();
} else {
// We should drop any characters. For example: "-ea".
return ver.toComponentsString();
}
return version;
}
}

View File

@ -251,21 +251,18 @@ public class JPackageCommand extends CommandArguments<JPackageCommand> {
public String version() {
return Optional.ofNullable(getArgumentValue("--app-version")).or(() -> {
if (isRuntime()) {
final Path releaseFile = RuntimeImageUtils.getReleaseFilePath(
final var runtimeHome = RuntimeImageUtils.getRuntimeHomeForRuntimeRoot(
Path.of(getArgumentValue("--runtime-image")));
try {
return RuntimeVersionReader.readVersion(releaseFile).map(releaseVersion -> {
if (TKit.isWindows()) {
return WindowsHelper.getNormalizedVersion(releaseVersion);
} else if (TKit.isOSX()) {
return MacHelper.getNormalizedVersion(releaseVersion);
} else {
return releaseVersion;
}
});
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
final var releaseFile = RuntimeImageUtils.getReleaseFilePath(runtimeHome);
return RuntimeVersionReader.readVersion(releaseFile).map(releaseVersion -> {
if (TKit.isWindows()) {
return WindowsHelper.getNormalizedVersion(releaseVersion.toString());
} else if (TKit.isOSX()) {
return MacHelper.getNormalizedVersion(releaseVersion.toString());
} else {
return releaseVersion.toString();
}
});
} else {
return Optional.empty();
}

View File

@ -1112,14 +1112,13 @@ public final class MacHelper {
static String getNormalizedVersion(String version) {
// macOS requires 1, 2 or 3 components version string.
// We always normalize to 3 components.
DottedVersion ver = DottedVersion.greedy(version);
BigInteger[] components = ver.getComponents();
if (components.length >= 4) {
return ver.toComponentsStringWithPadding(3);
// We will always normalize to 3 components if needed.
DottedVersion ver = DottedVersion.lazy(version);
if (ver.getComponentsCount() > 3) {
return ver.trim(3).pad(3).toComponentsString();
} else {
return ver.toComponentsString();
}
return version;
}
static Path getInstallationDirectory(JPackageCommand cmd) {

View File

@ -59,15 +59,13 @@ public class WindowsHelper {
static String getNormalizedVersion(String version) {
// Windows requires 2 or 4 components version string.
// We always normalize to 4 components.
DottedVersion ver = DottedVersion.greedy(version);
BigInteger[] components = ver.getComponents();
if (components.length == 1 || components.length == 3 ||
components.length >= 5) {
return ver.toComponentsStringWithPadding(4);
// We will always normalize to 4 components if needed.
DottedVersion ver = DottedVersion.lazy(version);
if (ver.getComponentsCount() != 2 || ver.getComponentsCount() != 4) {
return ver.trim(4).pad(4).toComponentsString();
} else {
return ver.toComponentsString();
}
return version;
}
static Path getInstallationDirectory(JPackageCommand cmd) {

View File

@ -66,6 +66,10 @@ public class DottedVersionTest {
static TestConfig lazy(String input, String expectedSuffix, int expectedComponentCount, String expectedToComponent) {
return new TestConfig(input, Type.LAZY.createVersion, expectedSuffix, expectedComponentCount, expectedToComponent, -1);
}
static TestConfig lazy(String input, String expectedToComponent, int numberOfComponents) {
return new TestConfig(input, Type.LAZY.createVersion, "", -1, expectedToComponent, numberOfComponents);
}
}
@ParameterizedTest
@ -120,24 +124,41 @@ public class DottedVersionTest {
@ParameterizedTest
@MethodSource
public void testComponentsStringWithPadding(TestConfig cfg) {
public void testTrim(TestConfig cfg) {
var dv = cfg.createVersion.apply(cfg.input());
assertEquals(cfg.expectedToComponent(),
dv.toComponentsStringWithPadding(cfg.numberOfComponents()));
dv.trim(cfg.numberOfComponents()).toComponentsString());
}
private static List<TestConfig> testComponentsStringWithPadding() {
private static List<TestConfig> testTrim() {
List<TestConfig> data = new ArrayList<>();
for (var type : Type.values()) {
data.addAll(List.of(
new TestConfig("1", type, "1.0.0.0", 4),
new TestConfig("1.2", type, "1.2.0.0", 4),
new TestConfig("1.2.3", type, "1.2.3.0", 4),
new TestConfig("1.2.3.4", type, "1.2.3.4", 4),
new TestConfig("1.2.3.4.5", type, "1.2.3.4", 4)
));
}
data.addAll(List.of(
TestConfig.lazy("", "", 0),
TestConfig.lazy("1", "", 0),
TestConfig.lazy("1.2.3", "1", 1),
TestConfig.lazy("1.2.3", "1.2", 2),
TestConfig.lazy("1.2.3", "1.2.3", 3),
TestConfig.lazy("1.2.3", "1.2.3", 4)
));
return data;
}
@ParameterizedTest
@MethodSource
public void testPad(TestConfig cfg) {
var dv = cfg.createVersion.apply(cfg.input());
assertEquals(cfg.expectedToComponent(),
dv.pad(cfg.numberOfComponents()).toComponentsString());
}
private static List<TestConfig> testPad() {
List<TestConfig> data = new ArrayList<>();
data.addAll(List.of(
TestConfig.lazy("", "0", 1),
TestConfig.lazy("1", "1", 1),
TestConfig.lazy("1", "1.0", 2),
TestConfig.lazy("1.2.3", "1.2.3.0.0", 5)
));
return data;
}

View File

@ -37,58 +37,73 @@ import java.util.Properties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class RuntimeVersionReaderTest {
@Test
public void test_release_file_with_version(@TempDir Path workdir) {
final Optional<String> version;
try {
version = RuntimeVersionReader.readVersion(
createPropFileWithValue(workdir, "JAVA_VERSION", "27.1.2"));
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
assertTrue(version.isPresent());
version.ifPresent(ver -> {
assertEquals("27.1.2", version.get());
@ParameterizedTest
@CsvSource({
"27.1.2, true",
"27.1.2, false",
"27.1.2-ea, true",
"27.1.2-ea, false"
})
public void test_release_file_with_version(String version,
boolean quoteVersion, @TempDir Path workdir) {
final var value = RuntimeVersionReader.readVersion(
createPropFileWithValue(workdir, "JAVA_VERSION", version, quoteVersion));
assertTrue(value.isPresent());
value.ifPresent(val -> {
assertEquals(version, value.get().toString());
});
}
@ParameterizedTest
@CsvSource({
"7.1.2+foo, true",
"foo, true",
"'', true",
"7.1.2+foo, false",
"foo, false",
"'', false"
})
public void test_release_file_with_invalid_version(String version,
boolean quoteVersion, @TempDir Path workdir) {
final var value = RuntimeVersionReader.readVersion(
createPropFileWithValue(workdir, "JAVA_VERSION", version, quoteVersion));
assertFalse(value.isPresent());
}
@Test
public void test_release_file_without_version(@TempDir Path workdir) {
final Optional<String> version;
try {
version = RuntimeVersionReader.readVersion(
createPropFileWithValue(workdir, "JDK_VERSION", "27.1.2"));
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
assertFalse(version.isPresent());
final var value = RuntimeVersionReader.readVersion(
createPropFileWithValue(workdir, "JDK_VERSION", "27.1.2", true));
assertFalse(value.isPresent());
}
@Test
public void test_release_file_invalid_input(@TempDir Path workdir) {
final Optional<String> version;
try {
version = RuntimeVersionReader.readVersion(workdir);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
assertFalse(version.isPresent());
final var value = RuntimeVersionReader.readVersion(workdir);
assertFalse(value.isPresent());
}
private Path createPropFileWithValue(Path workdir, String name, String value)
throws IOException {
private Path createPropFileWithValue(Path workdir, String name, String value,
boolean quoteValue) {
Path releaseFile = workdir.resolve("release");
Properties props = new Properties();
props.setProperty(name, "\"" + value + "\"");
if (quoteValue) {
props.setProperty(name, "\"" + value + "\"");
} else {
props.setProperty(name, value);
}
try (Writer writer = Files.newBufferedWriter(releaseFile)) {
props.store(writer, null);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return releaseFile;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -90,6 +90,14 @@ public final class ModulePathTest3 {
"--strip-native-commands")
.execute();
if (TKit.isOSX()) {
// MacBundle requires valid bundle
if (Files.exists(workDir.resolve("Contents/Home"))) {
Files.createFile(workDir.resolve("Contents/Info.plist"));
Files.createDirectories(workDir.resolve("Contents/MacOS"));
}
}
JPackageCommand cmd = new JPackageCommand()
.setDefaultAppName()
.setPackageType(PackageType.IMAGE)

View File

@ -84,15 +84,15 @@ import jdk.jpackage.test.TKit;
*/
public class RuntimePackageTest {
// @Test
// public static void test() {
// init().run();
// }
@Test
public static void test() {
init().run();
}
// @Test(ifOS = MACOS)
// public static void testFromBundle() {
// init(MacHelper::createRuntimeBundle).run();
// }
@Test(ifOS = MACOS)
public static void testFromBundle() {
init(MacHelper::createRuntimeBundle).run();
}
@Test(ifOS = LINUX)
@Parameter("/usr")
@ -103,34 +103,50 @@ public class RuntimePackageTest {
.run();
}
// @Test
// public static void testName() {
// // Test that jpackage can derive package name from the path to runtime image.
// init()
// .addInitializer(cmd -> cmd.removeArgumentWithValue("--name"))
// // Don't attempt to install this package as it may have an odd name derived from
// // the runtime image path. Say, on Linux for `--runtime-image foo/bar/sed`
// // command line jpackage will create a package named 'sed' that will conflict
// // with the default 'sed' package.
// .run(Action.CREATE_AND_UNPACK);
// }
@Test
public static void testName() {
// Test that jpackage can derive package name from the path to runtime image.
init()
.addInitializer(cmd -> cmd.removeArgumentWithValue("--name"))
// Don't attempt to install this package as it may have an odd name derived from
// the runtime image path. Say, on Linux for `--runtime-image foo/bar/sed`
// command line jpackage will create a package named 'sed' that will conflict
// with the default 'sed' package.
.run(Action.CREATE_AND_UNPACK);
}
@Test
// 27
@Parameter(value = {"27", ""}, ifOS = {LINUX, MACOS})
@Parameter(value = {"27", "27.0.0.0"}, ifOS = WINDOWS)
@Parameter(value = {"27"}, ifOS = {LINUX, MACOS})
// 27.1
@Parameter(value = {"27.1", ""})
@Parameter(value = {"27.1"})
// 27.1.2
@Parameter(value = {"27.1.2"}, ifOS = {LINUX, MACOS})
// 27.1.2.3
@Parameter(value = {"27.1.2.3"}, ifOS = {LINUX, WINDOWS})
// 27.1.2.3.4
@Parameter(value = {"27.1.2.3.4"}, ifOS = LINUX)
// 17.21.3+foo
@Parameter(value = {"17.21.3+foo"}, ifOS = LINUX)
// 17.21.3-ea
@Parameter(value = {"17.21.3-ea"}, ifOS = LINUX)
public static void testReleaseFileVersion(String version) {
testReleaseFileVersion(version, version);
}
@Test
// 27
@Parameter(value = {"27", "27.0.0.0"}, ifOS = WINDOWS)
// 27.1.2
@Parameter(value = {"27.1.2", ""}, ifOS = {LINUX, MACOS})
@Parameter(value = {"27.1.2", "27.1.2.0"}, ifOS = WINDOWS)
// 27.1.2.3
@Parameter(value = {"27.1.2.3", ""}, ifOS = {LINUX, WINDOWS})
@Parameter(value = {"27.1.2.3", "27.1.2"}, ifOS = MACOS)
// 27.1.2.3.4
@Parameter(value = {"27.1.2.3.4", ""}, ifOS = LINUX)
@Parameter(value = {"27.1.2.3.4", "27.1.2"}, ifOS = MACOS)
@Parameter(value = {"27.1.2.3.4", "27.1.2.3"}, ifOS = WINDOWS)
// 17.21.3-ea
@Parameter(value = {"17.21.3-ea", "17.21.3"}, ifOS = MACOS)
@Parameter(value = {"17.21.3-ea", "17.21.3.0"}, ifOS = WINDOWS)
public static void testReleaseFileVersion(String version, String normalizedVersion) {
new PackageTest()
.addInitializer(cmd -> {
@ -157,7 +173,8 @@ public class RuntimePackageTest {
cmd.validateOutput(JPackageStringBundle.MAIN
.cannedFormattedString("message.release-version",
version, runtimeImage.toString()));
if (!normalizedVersion.isEmpty()) {
// Normalization message is only printed if we did normalization.
if (!version.equals(normalizedVersion)) {
cmd.validateOutput(JPackageStringBundle.MAIN
.cannedFormattedString("message.version-normalized",
normalizedVersion, version));