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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.util.VersionNumber;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import java.io.File;
import java.io.IOException;
Expand All @@ -26,9 +27,64 @@ public class InitializeMojo extends AbstractJenkinsMojo {

@Override
public void execute() throws MojoExecutionException {
setCompilerProperties();
setSurefireProperties();
}

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);
}
}

private void setSurefireProperties() throws MojoExecutionException {
if (JavaSpecificationVersion.forCurrentJVM().isOlderThan(new JavaSpecificationVersion("9"))) {
// nothing to do prior to JEP 261
Expand Down