Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-54828] Dynamically configure Java compiler based on core #323

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 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,75 @@
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")) {
Comment on lines +35 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This presumes that the POM is configuring these mojo parameters via properties, which is not necessary, and in fact discouraged—best configured via <configuration> in <plugin>.

More robust to look up the effective configuration of the mojo. Do not recall the idiom for that offhand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More robust to look up the effective configuration of the mojo. Do not recall the idiom for that offhand.

Declined. If you want to understand the reason why then just try writing the code and you will quickly find out.

// 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()));

// "release" serves the same purpose as Animal Sniffer.
setProperty("animal.sniffer.skip", "true");

/*
* 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) {
if (project.getProperties().containsKey(key)) {
getLog().info("Unsetting " + key);
project.getProperties().remove(key);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<id>default</id>
<phases>
<validate>org.jenkins-ci.tools:maven-hpi-plugin:validate,org.jenkins-ci.tools:maven-hpi-plugin:validate-hpi</validate>
<initialize>org.jenkins-ci.tools:maven-hpi-plugin:initialize</initialize>
<process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
<compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
<process-classes>org.kohsuke:access-modifier-checker:enforce</process-classes>
Expand Down