From a084df09866e184cee638c1017b839ead1f06524 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Wed, 7 Feb 2024 08:55:15 +0100 Subject: [PATCH] Reinstate `InitializeMojo` This reverts commit 13db53e3521e91047a5f2c02156ce5202f66e465. --- .../maven/plugins/hpi/InitializeMojo.java | 73 +++++++++++++++++++ .../HpiLifecycleMappingProvider.java | 1 + 2 files changed, 74 insertions(+) create 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 new file mode 100644 index 0000000000..bd452ef18d --- /dev/null +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java @@ -0,0 +1,73 @@ +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/extensions/HpiLifecycleMappingProvider.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java index 1d88bf172b..541dc663b0 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,6 +26,7 @@ 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"));