Skip to content

Commit

Permalink
Merge pull request #583 from jonesbusy/feature/jdk-default
Browse files Browse the repository at this point in the history
Avoid hardcoding implicit JDK and workarround #580
  • Loading branch information
jonesbusy authored Jan 7, 2025
2 parents 7edcddd + 8825e3b commit 8d368d8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Plugi

// Empty args means Java 8 in Windows and Linux
if (args.size() == 1 && args.get(0) instanceof J.Empty) {
pluginMetadata.addPlatform(new PlatformConfig(Platform.LINUX, JDK.JAVA_8, null, true));
pluginMetadata.addPlatform(new PlatformConfig(Platform.WINDOWS, JDK.JAVA_8, null, true));
pluginMetadata.addPlatform(new PlatformConfig(Platform.LINUX, JDK.getImplicit(), null, true));
pluginMetadata.addPlatform(new PlatformConfig(Platform.WINDOWS, JDK.getImplicit(), null, true));
return method;
}

Expand All @@ -112,7 +112,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Plugi
for (PlatformConfig pc : platforms) {
if (!pc.name().equals(Platform.UNKNOWN)) {
if (platforms.stream().allMatch(p -> p.jdk() == null)) {
newPlatforms.add(new PlatformConfig(pc.name(), JDK.JAVA_8, null, false));
newPlatforms.add(new PlatformConfig(pc.name(), JDK.getImplicit(), null, false));
continue;
}
for (PlatformConfig pc2 : platforms) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,19 @@ private void process(Plugin plugin) {
return;
}

// Handle Java 8 plugins and outdated
if (plugin.getMetadata().getJdks().stream().allMatch(jdk -> jdk.equals(JDK.JAVA_8))) {
String jenkinsVersion = new StaticPomParser(
plugin.getLocalRepository().resolve("pom.xml").toString())
.getJenkinsVersion();
LOG.debug("Found jenkins version {}", jenkinsVersion);
JDK jdk = JDK.get(jenkinsVersion).stream().findFirst().orElse(JDK.JAVA_8);
LOG.info("Plugin support Java {}. Need a first compile to general classes", jdk.getMajor());
// Handle outdated plugin or unparsable Jenkinsfile
if (plugin.getMetadata().getJdks().stream().allMatch(jdk -> jdk.equals(JDK.getImplicit()))) {
LOG.info("Plugin look outdated or without Jenkinsfile.");
StaticPomParser parser = new StaticPomParser(
plugin.getLocalRepository().resolve("pom.xml").toString());
String jenkinsVersion = parser.getJenkinsVersion();
String baseline = parser.getBaseline();
if (baseline != null && jenkinsVersion != null && jenkinsVersion.contains("${jenkins.baseline}")) {
jenkinsVersion = jenkinsVersion.replace("${jenkins.baseline}", baseline);
}
LOG.debug("Found jenkins version from pom {}", jenkinsVersion);
JDK jdk = JDK.get(jenkinsVersion).stream().findFirst().orElse(JDK.min());
LOG.info("Plugin support Java {}. Need a first compile to generate classes", jdk.getMajor());
plugin.verifyQuickBuild(mavenInvoker, jdk);
if (plugin.hasErrors()) {
plugin.raiseLastError();
Expand Down Expand Up @@ -389,7 +394,7 @@ private void collectMetadata(Plugin plugin, boolean retryAfterFirstCompile) {
*/
private JDK compilePlugin(Plugin plugin) {
PluginMetadata metadata = plugin.getMetadata();
JDK jdk = JDK.min(metadata.getJdks());
JDK jdk = JDK.min(metadata.getJdks(), metadata.getJenkinsVersion());
plugin.withJDK(jdk);
plugin.clean(mavenInvoker);
plugin.compile(mavenInvoker);
Expand All @@ -412,7 +417,7 @@ private JDK verifyPlugin(Plugin plugin) {
"No JDKs found in metadata for plugin {}. Using same JDK as rewrite for verification",
plugin.getName());
} else {
jdk = JDK.min(metadata.getJdks());
jdk = JDK.min(metadata.getJdks(), metadata.getJenkinsVersion());
LOG.info("Using minimum JDK {} from metadata for plugin {}", jdk.getMajor(), plugin.getName());
}
// If the plugin was modernized we should find next JDK compatible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public boolean supported(String jenkinsVersion) {
return get(jenkinsVersion).contains(this);
}

/**
* Get the implicit JDK when not specified on Jenkinsfile
* @return The implicit JDK
*/
public static JDK getImplicit() {
return JDK.JAVA_8;
}

/**
* Has next predicate
* @param jdk The JDK
Expand Down Expand Up @@ -221,6 +229,18 @@ public static JDK min(Set<JDK> jdks) {
return jdks.stream().min(JDK::compareMajor).orElseThrow();
}

/**
* Return the minimum JDK
* @param jdks List of JDKS. Can be null or empty
* @return The minimum JDK. If the list is empty, return the minimum JDK available
*/
public static JDK min(Set<JDK> jdks, String jenkinsVersion) {
if (jdks == null || jdks.isEmpty() && jenkinsVersion == null) {
return JDK.min();
}
return JDK.get(jenkinsVersion).stream().min(JDK::compareMajor).orElseThrow();
}

/**
* Return a list of all JDK sorted by major version
* @return The list of JDKs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ public String getJenkinsVersion() {
}
}

/**
* Return the Jenkins baseline of the POM file.
* @return the Jenkins baseline or null if not found
*/
public String getBaseline() {
XPath xPath = XPathFactory.newInstance().newXPath();
try {
return xPath.compile("/project/properties/jenkins.baseline").evaluate(document);
} catch (Exception e) {
LOG.warn("Error getting baseline: {}", e.getMessage());
return null;
}
}

/**
* Return the groupId of the POM file.
* @return the groupId or null if not found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,8 @@ void testPluginWithJenkinsfileWithJdkInfoVersionVar() {
void testPluginWithJenkinsfileDefault() {

// Keep in sync with https://github.com/jenkins-infra/pipeline-library with default JDK and 2 platforms
EXPECTED_METADATA.addPlatform(Platform.LINUX, JDK.JAVA_8, null);
EXPECTED_METADATA.addPlatform(Platform.WINDOWS, JDK.JAVA_8, null);
EXPECTED_METADATA.addPlatform(Platform.LINUX, JDK.getImplicit(), null);
EXPECTED_METADATA.addPlatform(Platform.WINDOWS, JDK.getImplicit(), null);

rewriteRun(
recipeSpec -> recipeSpec.recipe(new FetchMetadata()),
Expand All @@ -574,7 +574,7 @@ void testPluginWithJenkinsfileDefault() {
Set<JDK> jdkVersion = pluginMetadata.getJdks();
assertEquals(1, jdkVersion.size());
assertNull(pluginMetadata.isUseContainerAgent());
assertTrue(jdkVersion.contains(JDK.JAVA_8));
assertTrue(jdkVersion.contains(JDK.getImplicit()));

// Assert platform
Set<Platform> platforms = pluginMetadata.getPlatforms();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Set;
import org.junit.jupiter.api.Test;

public class JDKTest {
Expand Down Expand Up @@ -47,6 +48,17 @@ public void all() {
assertEquals(JDK.JAVA_21, JDK.all().get(3));
}

@Test
public void min() {
assertEquals(JDK.JAVA_8, JDK.min());
assertEquals(JDK.JAVA_8, JDK.min(Set.of(JDK.values())));
assertEquals(JDK.JAVA_8, JDK.min(Set.of(JDK.values()), "2.164.1"));
assertEquals(JDK.JAVA_8, JDK.min(Set.of(JDK.values()), "2.346.1"));
assertEquals(JDK.JAVA_11, JDK.min(Set.of(JDK.values()), "2.361.1"));
assertEquals(JDK.JAVA_11, JDK.min(Set.of(JDK.values()), "2.462.3"));
assertEquals(JDK.JAVA_17, JDK.min(Set.of(JDK.values()), "2.479.1"));
}

@Test
public void getBuildableJdk() {

Expand Down

0 comments on commit 8d368d8

Please sign in to comment.