Skip to content

Commit

Permalink
Fix Gradle Java version lookup to match the Maven lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Mar 19, 2024
1 parent e9931ce commit cef5e29
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class LookupBuildInfoCommand implements Runnable {
BootstrapMavenContext mavenContext;

// Variable so can be overridden by the tests.
String CACHE_PATH = "/v2/cache/rebuild-default/0";
String cachePath = "/v2/cache/rebuild-default/0";

@Override
public void run() {
Expand Down Expand Up @@ -271,7 +271,7 @@ protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt, Durat

if (artifact != null && (buildRecipeInfo == null || buildRecipeInfo.getJavaVersion() == null)) {
Log.infof("Lookup Build JDK for artifact %s", artifact);
var optBuildJdk = getBuildJdk(cacheUrl + CACHE_PATH, artifact);
var optBuildJdk = getBuildJdk(cacheUrl + cachePath, artifact);
if (optBuildJdk.isPresent()) {
var buildJdk = optBuildJdk.get();
Log.infof("Setting build JDK to %s for artifact %s", buildJdk.version(), artifact);
Expand Down Expand Up @@ -507,7 +507,8 @@ private static void handleGradleBuild(InvocationBuilder builder, Path gradleFile
var specifiedJavaVersion = GradleUtils.getSpecifiedJavaVersion(gradleFile);

if (!specifiedJavaVersion.isEmpty()) {
builder.minJavaVersion(new JavaVersion(specifiedJavaVersion));
Log.infof("Detected Java version %s in Gradle build file %s", specifiedJavaVersion, gradleFile);
MavenJavaVersionDiscovery.filterJavaVersions(builder, specifiedJavaVersion);
}
ArrayList<String> inv = new ArrayList<>(GradleUtils.getGradleArgs(gradleFile));
if (skipTests) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* Utility class for Gradle.
*/
public final class GradleUtils {

/**
* Format for applying plugins.
*/
Expand All @@ -33,11 +32,12 @@ public final class GradleUtils {
*/
public static final String MAVEN_PUBLISH_PLUGIN = String.format(PLUGIN_FORMAT, "maven-publish", "maven-publish");

static final String BUILD_GRADLE = "build.gradle";
public static final String BUILD_GRADLE = "build.gradle";

static final String BUILD_GRADLE_KTS = "build.gradle.kts";
public static final String BUILD_GRADLE_KTS = "build.gradle.kts";

private static final String GRADLE_WRAPPER_PROPERTIES = "gradle/wrapper/gradle-wrapper.properties";

private static final String OLD_GRADLE_WRAPPER_PROPERTIES = ".gradle-wrapper/gradle-wrapper.properties";

private static final String DISTRIBUTION_URL_KEY = "distributionUrl";
Expand All @@ -61,6 +61,8 @@ public final class GradleUtils {
*/
public static final List<String> MAVEN_PLUGIN_GRADLE_ARGS = List.of("assemble", "uploadArchives");

public static final String JAVA_COMPATIBILITY = "^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?(?<version>(\\d+)([._]\\d+){0,2})['\"]?";

private GradleUtils() {

}
Expand Down Expand Up @@ -125,22 +127,9 @@ private static int getMajorVersion(String version) {
* @throws IOException if an error occurs while reading from the build files
*/
public static String getSpecifiedJavaVersion(Path buildFile) throws IOException {
if (isInBuildGradle(buildFile,
"^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?1[2-7][^0-9]['\"]?")) {
return "17";
}

if (isInBuildGradle(buildFile,
"^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?(1[._])?(9|1[0-1])[^0-9]['\"]?")) {
return "11";
}

if (isInBuildGradle(buildFile,
"^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?(1[._])?[1-8][^0-9]['\"]?")) {
return "8";
}

return "";
var content = Files.readString(buildFile);
var matcher = Pattern.compile(JAVA_COMPATIBILITY, Pattern.DOTALL | Pattern.MULTILINE).matcher(content);
return matcher.find() ? matcher.group("version").replace('_', '.') : "";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static com.redhat.hacbs.container.verifier.MavenUtils.getCompilerSource;
import static com.redhat.hacbs.container.verifier.MavenUtils.getCompilerTarget;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand All @@ -21,7 +20,6 @@
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.PropertiesBasedValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import com.redhat.hacbs.container.analyser.build.InvocationBuilder;
import com.redhat.hacbs.container.analyser.build.JavaVersion;
Expand All @@ -48,9 +46,7 @@ public static String interpolate(String value, Model model) {
return value;
}

public static void filterJavaVersions(Path pomFile, Model model, InvocationBuilder invocationBuilder)
throws IOException, XmlPullParserException {

public static void filterJavaVersions(Path pomFile, Model model, InvocationBuilder invocationBuilder) {
//if the toolchains plugin is configured we don't filter anything
if (model.getBuild() != null && model.getBuild().getPlugins() != null) {
for (var i : model.getBuild().getPlugins()) {
Expand Down Expand Up @@ -90,15 +86,8 @@ public static void filterJavaVersions(Path pomFile, Model model, InvocationBuild
javaVersion = parsed;
}
}
if (javaVersion > 0) {
if (javaVersion <= 5) {
invocationBuilder.maxJavaVersion(JAVA_8);
} else if (javaVersion == 6) {
invocationBuilder.maxJavaVersion(JAVA_11);
} else {
invocationBuilder.minJavaVersion(new JavaVersion(Integer.toString(javaVersion)));
}
}

filterJavaVersions(invocationBuilder, javaVersion);

for (var module : model.getModules()) {
try {
Expand All @@ -115,4 +104,24 @@ public static void filterJavaVersions(Path pomFile, Model model, InvocationBuild
}
}
}

public static void filterJavaVersions(InvocationBuilder invocationBuilder, String javaVersion) {
filterJavaVersions(invocationBuilder, JavaVersion.toVersion(javaVersion));
}

public static void filterJavaVersions(InvocationBuilder invocationBuilder, int javaVersion) {
if (javaVersion > 0) {
if (javaVersion <= 5) {
invocationBuilder.maxJavaVersion(JAVA_8);
Log.infof("Set max Java version to %s", JAVA_8);
} else if (javaVersion == 6) {
invocationBuilder.maxJavaVersion(JAVA_11);
Log.infof("Set max Java version to %s", JAVA_11);
} else {
var version = new JavaVersion(Integer.toString(javaVersion));
invocationBuilder.minJavaVersion(version);
Log.infof("Set min Java version to %s", version);
}
}
}
}
Loading

0 comments on commit cef5e29

Please sign in to comment.