From 47b6b67e7cc1f756df29b1b93bf832675aed4a66 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 22 Feb 2024 06:00:54 -0800 Subject: [PATCH] =?UTF-8?q?`EnforceBytecodeVersion`=20fails=20with=20"Rest?= =?UTF-8?q?ricted=20to=20JDK=2011=20yet=20[=E2=80=A6]=20contains=20[?= =?UTF-8?q?=E2=80=A6]=20targeted=20to=20JDK=2017"=20(#582)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../maven/plugins/hpi/InitializeMojo.java | 73 ------------------- .../maven/plugins/hpi/ValidateMojo.java | 10 +++ .../HpiLifecycleMappingProvider.java | 1 - 3 files changed, 10 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java deleted file mode 100644 index bd452ef18d..0000000000 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.jenkinsci.maven.plugins.hpi; - -import hudson.util.VersionNumber; -import io.jenkins.lib.versionnumber.JavaSpecificationVersion; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; - -/** - * Configure Maven for the desired version of Java. - * - * @author Basil Crow - */ -@Mojo(name = "initialize", defaultPhase = LifecyclePhase.INITIALIZE) -public class InitializeMojo extends AbstractJenkinsMojo { - - @Override - public void execute() throws MojoExecutionException { - setCompilerProperties(); - } - - private void setCompilerProperties() throws MojoExecutionException { - if (!project.getProperties().containsKey("maven.compiler.source") - && !project.getProperties().containsKey("maven.compiler.release")) { - // On an older plugin parent POM that predates the setting of these values as Maven properties. - return; - } - - JavaSpecificationVersion javaVersion = getMinimumJavaVersion(); - if (JavaSpecificationVersion.forCurrentJVM().isOlderThan(new VersionNumber("9"))) { - // Should always be set already, but just in case... - setProperty("maven.compiler.source", javaVersion.toString()); - setProperty("maven.compiler.target", javaVersion.toString()); - setProperty("maven.compiler.testSource", javaVersion.toString()); - setProperty("maven.compiler.testTarget", javaVersion.toString()); - // Should never be set already, but just in case... - unsetProperty("maven.compiler.release"); - unsetProperty("maven.compiler.testRelease"); - } else { - /* - * When compiling with a Java 9+ compiler, we always rely on "release" in favor of "source" and "target", - * even when compiling to Java 8 bytecode. - */ - setProperty("maven.compiler.release", Integer.toString(javaVersion.toReleaseVersion())); - setProperty("maven.compiler.testRelease", Integer.toString(javaVersion.toReleaseVersion())); - - /* - * While it does not hurt to have these set to the Java specification version, it is also not needed when - * "release" is in use. - */ - unsetProperty("maven.compiler.source"); - unsetProperty("maven.compiler.target"); - unsetProperty("maven.compiler.testSource"); - unsetProperty("maven.compiler.testTarget"); - } - } - - private void setProperty(String key, String value) { - String currentValue = project.getProperties().getProperty(key); - if (currentValue == null || !currentValue.equals(value)) { - getLog().info("Setting " + key + " to " + value); - project.getProperties().setProperty(key, value); - } - } - - private void unsetProperty(String key) { - String currentValue = project.getProperties().getProperty(key); - if (currentValue != null && !currentValue.isEmpty()) { - getLog().info("Unsetting " + key); - project.getProperties().remove(key); - } - } -} diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java index 639846707e..cf3ec9c8f6 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java @@ -42,6 +42,8 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException("Java " + javaVersion + " or later is necessary to build this plugin."); } writeProfileMarker(javaVersion); + setProperty("maven.compiler.release", Integer.toString(javaVersion.toReleaseVersion())); + setProperty("maven.compiler.testRelease", Integer.toString(javaVersion.toReleaseVersion())); if (new VersionNumber(findJenkinsVersion()).compareTo(new VersionNumber("2.361")) < 0) { throw new MojoExecutionException("This version of maven-hpi-plugin requires Jenkins 2.361 or later"); @@ -149,6 +151,14 @@ private static void delete(Path p) throws MojoExecutionException { } } + private void setProperty(String key, String value) { + String currentValue = project.getProperties().getProperty(key); + if (currentValue == null || !currentValue.equals(value)) { + getLog().info("Setting " + key + " to " + value); + project.getProperties().setProperty(key, value); + } + } + private void check(String tag, String value, String badStart, String goodStart, String reason) { if (value.startsWith(badStart)) { String goodValue = goodStart + value.substring(badStart.length()); diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java index 541dc663b0..1d88bf172b 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java @@ -26,7 +26,6 @@ static Map getBindings() { "validate", new LifecyclePhase( "org.jenkins-ci.tools:maven-hpi-plugin:validate,org.jenkins-ci.tools:maven-hpi-plugin:validate-hpi")); - bindings.put("initialize", new LifecyclePhase("org.jenkins-ci.tools:maven-hpi-plugin:initialize")); bindings.put( "process-resources", new LifecyclePhase("org.apache.maven.plugins:maven-resources-plugin:2.6:resources"));