Skip to content

Commit

Permalink
Add Java 21 support for running tests (#622)
Browse files Browse the repository at this point in the history
This commit adds a Gradle property 'testToolchain' that when set will
configure the Java toolchain plugin accordingly.

Additionally:
-------------
* Replace the custom toolchain plugin with the vanilla one provided by
  Gradle

* Move the Java conventions from Java plugin to Groovy script

* [CI] Add Java 21 to check-samples.yml
  • Loading branch information
onobc authored Mar 22, 2024
1 parent eefff55 commit 19957ac
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 176 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/check-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ jobs:
needs: [prerequisites]
strategy:
matrix:
springBootVersion: [ "${{ needs.prerequisites.outputs.boot_version }}" ]
include:
- javaVersion: 17
springBootVersion: "${{ needs.prerequisites.outputs.boot_version }}"
- javaVersion: 21
springBootVersion: "${{ needs.prerequisites.outputs.boot_version }}"
runs-on: ubuntu-latest
if: needs.prerequisites.outputs.runjobs
steps:
Expand All @@ -50,12 +54,14 @@ jobs:
LOCAL_REPOSITORY_PATH: ${{ github.workspace }}/build/publications/repos
VERSION: ${{ needs.prerequisites.outputs.project_version }}
BOOT_VERSION: ${{ matrix.springBootVersion }}
JAVA_VERSION: ${{ matrix.javaVersion }}
run: |
./gradlew publishMavenJavaPublicationToLocalRepository
./gradlew \
--init-script ./spring-pulsar-sample-apps/sample-apps-check-ci.gradle \
-PlocalRepositoryPath="$LOCAL_REPOSITORY_PATH" \
-PspringPulsarVersion="$VERSION" \
-PspringBootVersion="$BOOT_VERSION" \
-PtestToolchain="$JAVA_VERSION" \
-PsampleTests \
:runAllSampleTests
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ apply from: 'gradle/jacoco-conventions.gradle'
apply from: 'gradle/aggregate-jacoco-report.gradle'
apply from: 'gradle/update-copyrights.gradle'
apply from: 'gradle/version-catalog-update.gradle'
apply from: 'gradle/java-conventions.gradle'

allprojects {
group = 'org.springframework.pulsar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.gradle.api.JavaVersion;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand All @@ -33,7 +31,6 @@
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.plugins.quality.Checkstyle;
import org.gradle.api.plugins.quality.CheckstyleExtension;
import org.gradle.api.plugins.quality.CheckstylePlugin;
Expand All @@ -49,7 +46,6 @@

import org.springframework.pulsar.gradle.optional.OptionalDependenciesPlugin;
import org.springframework.pulsar.gradle.testing.TestFailuresPlugin;
import org.springframework.pulsar.gradle.toolchain.ToolchainPlugin;

import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
import io.spring.javaformat.gradle.tasks.CheckFormat;
Expand Down Expand Up @@ -109,12 +105,10 @@ public class JavaConventionsPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPlugins().withType(JavaBasePlugin.class, (java) -> {
configureSpringJavaFormat(project);
configureJavaConventions(project);
configureJavadocConventions(project);
configureTestConventions(project);
configureJarManifestConventions(project);
configureDependencyManagement(project);
configureToolchain(project);
});
}

Expand All @@ -131,32 +125,6 @@ private void configureSpringJavaFormat(Project project) {
.add(project.getDependencies().create("io.spring.javaformat:spring-javaformat-checkstyle:" + version));
}

private void configureJavaConventions(Project project) {
if (!project.hasProperty("toolchainVersion")) {
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
javaPluginExtension.setSourceCompatibility(JavaVersion.toVersion(SOURCE_AND_TARGET_COMPATIBILITY));
}
project.getTasks().withType(JavaCompile.class, (compile) -> {
compile.getOptions().setEncoding("UTF-8");
List<String> args = compile.getOptions().getCompilerArgs();
if (!args.contains("-parameters")) {
args.add("-parameters");
}
if (project.hasProperty("toolchainVersion")) {
compile.setSourceCompatibility(SOURCE_AND_TARGET_COMPATIBILITY);
compile.setTargetCompatibility(SOURCE_AND_TARGET_COMPATIBILITY);
}
else if (buildingWithJava17(project)) {
args.addAll(Arrays.asList("-Werror", "-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:rawtypes",
"-Xlint:varargs"));
}
});
}

private boolean buildingWithJava17(Project project) {
return !project.hasProperty("toolchainVersion") && JavaVersion.current() == JavaVersion.VERSION_17;
}

private void configureJavadocConventions(Project project) {
project.getTasks().withType(Javadoc.class, (javadoc) -> {
CoreJavadocOptions options = (CoreJavadocOptions) javadoc.getOptions();
Expand Down Expand Up @@ -245,8 +213,4 @@ private void configureDependencyManagement(Project project) {
.getByName(OptionalDependenciesPlugin.OPTIONAL_CONFIGURATION_NAME).extendsFrom(dependencyManagement));
}

private void configureToolchain(Project project) {
project.getPlugins().apply(ToolchainPlugin.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.springframework.pulsar.gradle;

import io.spring.gradle.convention.ArtifactoryPlugin;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand All @@ -27,7 +25,10 @@
import org.springframework.pulsar.gradle.check.SonarQubeConventionsPlugin;
import org.springframework.pulsar.gradle.publish.SpringNexusPublishPlugin;

import io.spring.gradle.convention.ArtifactoryPlugin;

/**
* Plugin for the root project.
* @author Chris Bono
*/
public class RootProjectPlugin implements Plugin<Project> {
Expand Down

This file was deleted.

This file was deleted.

26 changes: 26 additions & 0 deletions gradle/java-conventions.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def toolchainVersion() {
if (project.hasProperty('testToolchain')) {
return project.property('testToolchain').toString().toInteger()
}
return 17
}

project.afterEvaluate {
subprojects { subproject ->
afterEvaluate {
if (subproject.plugins.hasPlugin(JavaPlugin.class)) {
java {
toolchain {
languageVersion = JavaLanguageVersion.of(toolchainVersion())
}
}
tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
options.compilerArgs.add("-parameters")
options.compilerArgs.addAll(["-Werror", "-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:rawtypes", "-Xlint:varargs"]);
options.release.set(17)
}
}
}
}
}

0 comments on commit 19957ac

Please sign in to comment.