Skip to content

Commit

Permalink
Reinstate InitializeMojo
Browse files Browse the repository at this point in the history
This reverts commit 13db53e.
  • Loading branch information
Vlatombe authored and MarkEWaite committed Feb 9, 2024
1 parent d2dd160 commit a084df0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static Map<String, LifecyclePhase> 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"));
Expand Down

0 comments on commit a084df0

Please sign in to comment.