From ce77c845145d147b12e9e2059283ee1a06951f54 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Tue, 23 Jan 2024 16:37:43 +0100 Subject: [PATCH] 16: Fix multi-release Jar setup --- .../kotlin/my-java-application.gradle.kts | 9 +++++++++ .../src/main/kotlin/my-java-base.gradle.kts | 6 ++++-- .../main/java/myproject/MyApplication.java | 1 + .../my-project/app/src/main/java17/.gitignore | 0 .../main/java17/myproject/MyApplication.java | 20 +++++++++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) delete mode 100644 16_Source_Sets/my-project/app/src/main/java17/.gitignore create mode 100644 16_Source_Sets/my-project/app/src/main/java17/myproject/MyApplication.java diff --git a/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts b/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts index 37a3503..e5407d9 100644 --- a/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts +++ b/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts @@ -2,3 +2,12 @@ plugins { id("my-java-base") id("application") } + +tasks.register("runJava17") { + classpath = configurations.runtimeClasspath.get() + files(tasks.jar) + javaLauncher.set(javaToolchains.launcherFor { + languageVersion = JavaLanguageVersion.of(17) + }) + mainClass = application.mainClass + group = "application" +} \ No newline at end of file diff --git a/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-base.gradle.kts b/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-base.gradle.kts index 5bcdba8..857b728 100644 --- a/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-base.gradle.kts +++ b/16_Source_Sets/my-build-logic/java-plugins/src/main/kotlin/my-java-base.gradle.kts @@ -34,10 +34,12 @@ jvm.registerJvmLanguageSourceDirectory(sourceSets.main.get(), "java17") { classpath = sourceSets.main.get().compileClasspath doLast { val destRoot = destinationDirectory.get().asFile - val destVersion17 = File(destRoot, "META-INF/version/17").also { it.mkdirs() } + val destVersions = File(destRoot, "META-INF/versions/17").also { it.mkdirs() } + destVersions.deleteRecursively() + destVersions.mkdirs() destRoot.listFiles()?.forEach { if (it.name != "META-INF") { - Files.move(it.toPath(), File(destVersion17, it.name).toPath()) + Files.move(it.toPath(), File(destVersions, it.name).toPath()) } } } diff --git a/16_Source_Sets/my-project/app/src/main/java/myproject/MyApplication.java b/16_Source_Sets/my-project/app/src/main/java/myproject/MyApplication.java index 85ede31..d709afc 100644 --- a/16_Source_Sets/my-project/app/src/main/java/myproject/MyApplication.java +++ b/16_Source_Sets/my-project/app/src/main/java/myproject/MyApplication.java @@ -9,6 +9,7 @@ public final class MyApplication { public static void main(final String[] args) { Service.printMessage(new MessageModel(readeMessageTxt())); + Service.printMessage(new MessageModel("Java 11")); } static String readeMessageTxt() { diff --git a/16_Source_Sets/my-project/app/src/main/java17/.gitignore b/16_Source_Sets/my-project/app/src/main/java17/.gitignore deleted file mode 100644 index e69de29..0000000 diff --git a/16_Source_Sets/my-project/app/src/main/java17/myproject/MyApplication.java b/16_Source_Sets/my-project/app/src/main/java17/myproject/MyApplication.java new file mode 100644 index 0000000..0ca23fd --- /dev/null +++ b/16_Source_Sets/my-project/app/src/main/java17/myproject/MyApplication.java @@ -0,0 +1,20 @@ +package myproject; + +import myproject.data.MessageModel; +import myproject.services.Service; + +import java.util.Scanner; + +public final class MyApplication { + + public static void main(final String[] args) { + Service.printMessage(new MessageModel(readeMessageTxt())); + Service.printMessage(new MessageModel("Java 17")); + } + + static String readeMessageTxt() { + //noinspection ConstantConditions + return new Scanner(MyApplication.class.getResourceAsStream("message.txt")) + .useDelimiter("\n").next(); + } +}