-
Notifications
You must be signed in to change notification settings - Fork 86
/
InitializeMojo.java
73 lines (65 loc) · 3.11 KB
/
InitializeMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
}
}
}