From e7348e2344ea2587f731bca670285a9428cd462a Mon Sep 17 00:00:00 2001 From: kilo52 Date: Fri, 5 Jan 2024 15:13:12 +0100 Subject: [PATCH] Improved Java build and runtime compatibility for Java 8. Introduces conditional handling for when a Java project needs compatibility with Java 8. The POM for Java project source templates is adjusted conditionally such that when Java 8 support is specified by the user as a requirement, then the POM will have two additional profiles which activate based on the JDK version which is used to build the project. This effectively instructs Maven to add the --source and --target arguments as compiler options when using JDK-8. However, when using JDK-9 or higher, only the --release argument must be specified as a compiler option. This ensures complete compatibility of the produced class files with the minimum specified Java version. The Maven compiler plugin is added to the affected POMs as an explicit declaration with version 3.12.1 of the plugin. The Maven JAR plugin is updated to version 3.3.0 of the plugin in all affected POMs. --- java/01_executable/source/pom.xml | 11 ++++++++--- java/02_library/source/pom.xml | 11 ++++++++--- java/03_library_native_jni/source/pom.xml | 8 ++++---- java/init.sh | 16 ++++++++++++++-- java/var/pom_profiles | 21 +++++++++++++++++++++ java/var/pom_properties | 1 + 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 java/var/pom_profiles create mode 100644 java/var/pom_properties diff --git a/java/01_executable/source/pom.xml b/java/01_executable/source/pom.xml index 4fd5bd6..6eb57ef 100644 --- a/java/01_executable/source/pom.xml +++ b/java/01_executable/source/pom.xml @@ -10,8 +10,7 @@ ${{VAR_PROJECT_DESCRIPTION}} UTF-8 - ${{VAR_JAVA_VERSION_POM}} - ${{VAR_JAVA_VERSION_POM}} +${{VAR_POM_PROPERTIES}} ${{VAR_PROJECT_ORGANISATION_NAME}} @@ -24,10 +23,15 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} build ${project.artifactId}-${project.version} + + org.apache.maven.plugins + maven-compiler-plugin + 3.12.1 + org.apache.maven.plugins maven-jar-plugin - 2.4 + 3.3.0 @@ -47,4 +51,5 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} +${{VAR_POM_PROFILES}} diff --git a/java/02_library/source/pom.xml b/java/02_library/source/pom.xml index 99f86e8..50c2201 100644 --- a/java/02_library/source/pom.xml +++ b/java/02_library/source/pom.xml @@ -10,8 +10,7 @@ ${{VAR_PROJECT_DESCRIPTION}} UTF-8 - ${{VAR_JAVA_VERSION_POM}} - ${{VAR_JAVA_VERSION_POM}} +${{VAR_POM_PROPERTIES}} ${{VAR_PROJECT_ORGANISATION_NAME}} @@ -24,10 +23,15 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} build ${project.artifactId}-${project.version} + + org.apache.maven.plugins + maven-compiler-plugin + 3.12.1 + org.apache.maven.plugins maven-jar-plugin - 2.4 + 3.3.0 default-jar @@ -89,4 +93,5 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} +${{VAR_POM_PROFILES}} diff --git a/java/03_library_native_jni/source/pom.xml b/java/03_library_native_jni/source/pom.xml index 1296edf..c54cd3a 100644 --- a/java/03_library_native_jni/source/pom.xml +++ b/java/03_library_native_jni/source/pom.xml @@ -10,8 +10,7 @@ ${{VAR_PROJECT_DESCRIPTION}} UTF-8 - ${{VAR_JAVA_VERSION_POM}} - ${{VAR_JAVA_VERSION_POM}} +${{VAR_POM_PROPERTIES}} ${{VAR_PROJECT_ORGANISATION_NAME}} @@ -26,7 +25,7 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} maven-compiler-plugin - 3.10.1 + 3.12.1 -h @@ -37,7 +36,7 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} org.apache.maven.plugins maven-jar-plugin - 2.4 + 3.3.0 default-jar @@ -162,4 +161,5 @@ ${{INCLUDE:java/maven/dependencyUnitTests.xml}} +${{VAR_POM_PROFILES}} diff --git a/java/init.sh b/java/init.sh index c8d21d4..62abd9d 100644 --- a/java/init.sh +++ b/java/init.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Copyright (C) 2023 Raven Computing +# Copyright (C) 2024 Raven Computing # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -51,7 +51,6 @@ function process_files_lvl_1() { replace_var "PROJECT_ARTIFACT_ID" "$var_project_artifact_id"; replace_var "JAVA_VERSION" "$var_java_version"; replace_var "JAVA_VERSION_LABEL" "$var_java_version_label"; - replace_var "JAVA_VERSION_POM" "$var_java_version_pom"; replace_var "NAMESPACE_DECLARATION" "$var_namespace"; replace_var "NAMESPACE_DECLARATION_0" "$var_namespace_0"; replace_var "NAMESPACE_DECLARATION_TRAILING_SEP" "$var_namespace_trailing_sep"; @@ -60,6 +59,19 @@ function process_files_lvl_1() { logW "Docker integration is not yet supported when using JDK 21"; fi + # Check for Java 8 compatibility requirement and adjust the POM based on that. + if [[ "$var_java_version" == "1.8" ]]; then + replace_var "POM_PROPERTIES" ""; + load_var_from_file "POM_PROFILES"; + replace_var "POM_PROFILES" "$VAR_FILE_VALUE"; + else + load_var_from_file "POM_PROPERTIES"; + replace_var "POM_PROPERTIES" "$VAR_FILE_VALUE"; + replace_var "POM_PROFILES" ""; + fi + + replace_var "JAVA_VERSION_POM" "$var_java_version_pom"; + if [ -z "$var_namespace" ]; then replace_var "NAMESPACE_PACKAGE_DECLARATION" ""; else diff --git a/java/var/pom_profiles b/java/var/pom_profiles new file mode 100644 index 0000000..9ca32ef --- /dev/null +++ b/java/var/pom_profiles @@ -0,0 +1,21 @@ + + + compiler-jdk8-compat + + 1.8 + + + 1.8 + 1.8 + + + + compiler-java8-release + + [9,) + + + 8 + + + \ No newline at end of file diff --git a/java/var/pom_properties b/java/var/pom_properties new file mode 100644 index 0000000..b69b507 --- /dev/null +++ b/java/var/pom_properties @@ -0,0 +1 @@ + ${{VAR_JAVA_VERSION_POM}} \ No newline at end of file