Skip to content

Commit 8a43251

Browse files
committed
2 parents 6f339bb + 1c315ad commit 8a43251

File tree

8 files changed

+130
-47
lines changed

8 files changed

+130
-47
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- name: Git checkout
10-
uses: actions/checkout@v2
11-
- name: Configure Gradle cache
12-
uses: actions/cache@v1
13-
with:
14-
path: ~/.gradle/caches
15-
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
16-
restore-keys: |
17-
${{ runner.os }}-gradle-
10+
uses: actions/checkout@v3
1811
- name: Set up JDK 11
19-
uses: actions/setup-java@v1
12+
uses: actions/setup-java@v2
2013
with:
14+
cache: gradle
15+
distribution: microsoft
2116
java-version: 11
2217
- name: Build with Gradle
2318
run: ./gradlew build --scan

.github/workflows/publish.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@ jobs:
1010
GRADLE_PUBLISH_SECRET: ${{ secrets.GRADLE_PUBLISH_SECRET }}
1111
steps:
1212
- name: Git checkout
13-
uses: actions/checkout@v2
14-
- name: Configure Gradle cache
15-
uses: actions/cache@v1
16-
with:
17-
path: ~/.gradle/caches
18-
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
19-
restore-keys: |
20-
${{ runner.os }}-gradle-
13+
uses: actions/checkout@v3
2114
- name: Set up JDK 11
22-
uses: actions/setup-java@v1
15+
uses: actions/setup-java@v2
2316
with:
17+
cache: gradle
18+
distribution: microsoft
2419
java-version: 11
2520
- name: Create gradle.properties
2621
run: echo -e "gradle.publish.key=$GRADLE_PUBLISH_KEY\ngradle.publish.secret=$GRADLE_PUBLISH_SECRET" > gradle.properties

src/main/java/org/javamodularity/moduleplugin/extensions/DefaultModularityExtension.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import org.gradle.api.Project;
66
import org.gradle.api.Task;
77
import org.gradle.api.plugins.JavaPlugin;
8+
import org.gradle.api.plugins.JavaPluginExtension;
89
import org.gradle.api.tasks.compile.JavaCompile;
10+
import org.gradle.jvm.toolchain.JavaToolchainSpec;
11+
import org.gradle.util.GradleVersion;
912
import org.javamodularity.moduleplugin.JavaProjectHelper;
1013
import org.javamodularity.moduleplugin.tasks.ClasspathFile;
1114

@@ -74,6 +77,17 @@ private void configureMixedJavaRelease(int mainJavaRelease, int moduleInfoJavaRe
7477
// TODO: Remove this method when Gradle supports it natively: https://github.com/gradle/gradle/issues/2510
7578
private void setJavaRelease(JavaCompile javaCompile, int javaRelease) {
7679
String currentJavaVersion = JavaVersion.current().toString();
80+
if (toolchainIsSupported()) {
81+
JavaToolchainSpec toolchain = project.getExtensions().getByType(JavaPluginExtension.class).getToolchain();
82+
if (toolchain != null) {
83+
// If toolchain is enabled, the version of java compiler is NOT same to the version of JVM running Gradle
84+
// so we need to get the version of toolchain explicitly as follows
85+
String toolchainVersion = toolchain.getLanguageVersion().map(Object::toString).getOrNull();
86+
if (toolchainVersion != null) {
87+
currentJavaVersion = toolchainVersion;
88+
}
89+
}
90+
}
7791
if (!javaCompile.getSourceCompatibility().equals(currentJavaVersion)) {
7892
throw new IllegalStateException("sourceCompatibility should not be set together with --release option");
7993
}
@@ -86,8 +100,29 @@ private void setJavaRelease(JavaCompile javaCompile, int javaRelease) {
86100
throw new IllegalStateException("--release option is already set in compiler args");
87101
}
88102

89-
compilerArgs.add("--release");
90-
compilerArgs.add(String.valueOf(javaRelease));
103+
if (releaseOptionIsSupported()) {
104+
// using the `convention(Integer)` method instead of the `set(Integer)` method, to let users set overwrite explicitly
105+
javaCompile.getOptions().getRelease().convention(javaRelease);
106+
} else {
107+
compilerArgs.add("--release");
108+
compilerArgs.add(String.valueOf(javaRelease));
109+
}
110+
}
111+
112+
/**
113+
* @see <a href="https://github.com/gradle/gradle/issues/2510#issuecomment-657436188">The comment on GitHub issue that says {@code --release} option is added in Gradle 6.6</a>
114+
* @return true if the version of Gradle is 6.6 or later
115+
*/
116+
private boolean releaseOptionIsSupported() {
117+
return GradleVersion.current().compareTo(GradleVersion.version("6.6")) >= 0;
118+
}
119+
120+
/**
121+
* @see <a href="https://docs.gradle.org/6.7/javadoc/org/gradle/api/plugins/JavaPluginExtension.html#getToolchain--">The Javadoc that says {@code JavaPluginExtension.getToolchain()} is added in Gradle 6.7</a>
122+
* @return true if the version of Gradle is 6.7 or later
123+
*/
124+
private boolean toolchainIsSupported() {
125+
return GradleVersion.current().compareTo(GradleVersion.version("6.7")) >= 0;
91126
}
92127

93128
private JavaProjectHelper helper() {

src/test/java/org/javamodularity/moduleplugin/ModulePluginSmokeTest.java

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import org.gradle.api.logging.Logging;
77
import org.gradle.testkit.runner.BuildResult;
88
import org.gradle.testkit.runner.GradleRunner;
9-
import org.gradle.util.GradleVersion;
109
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.junitpioneer.jupiter.CartesianEnumSource;
1112
import org.junitpioneer.jupiter.CartesianProductTest;
1213
import org.junitpioneer.jupiter.CartesianValueSource;
1314

@@ -22,12 +23,35 @@
2223

2324
import static org.junit.jupiter.api.Assertions.*;
2425

26+
/**
27+
* If you're locally seeing the error:
28+
* {@code Could not create an instance of type org.gradle.initialization.DefaultSettings_Decorated.}
29+
* when running these test it will because you're using a recent version of Java and running into:
30+
* https://github.com/gradle/gradle/issues/10248.
31+
*
32+
* <p>Either switch to pre JDK-14 or <b>locally</b> comment out v5_1 and v5_2 in the {@link GradleVersion} enum.
33+
*/
2534
@SuppressWarnings("ConstantConditions")
2635
class ModulePluginSmokeTest {
2736
private static final Logger LOGGER = Logging.getLogger(ModulePluginSmokeTest.class);
2837

2938
private List<File> pluginClasspath;
3039

40+
@SuppressWarnings("unused")
41+
private enum GradleVersion {
42+
// Locally comment out the 5.x versions if running JDK-14+
43+
v5_1, v5_6,
44+
v6_3, v6_4_1, v6_5_1, v6_8_3,
45+
v7_0, v7_2
46+
;
47+
48+
@Override
49+
public String toString() {
50+
return name().substring(1).replaceAll("_", ".");
51+
}
52+
}
53+
54+
@SuppressWarnings("UnstableApiUsage")
3155
@BeforeEach
3256
void before() throws IOException {
3357
pluginClasspath = Resources.readLines(Resources.getResource("plugin-classpath.txt"), Charsets.UTF_8)
@@ -38,38 +62,38 @@ void before() throws IOException {
3862

3963
@CartesianProductTest(name = "smokeTest({arguments})")
4064
@CartesianValueSource(strings = {"test-project", "test-project-kotlin-pre-1-7", "test-project-kotlin", "test-project-groovy"})
41-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
42-
void smokeTest(String projectName, String gradleVersion) {
65+
@CartesianEnumSource(GradleVersion.class)
66+
void smokeTest(String projectName, GradleVersion gradleVersion) {
4367
LOGGER.lifecycle("Executing smokeTest of {} with Gradle {}", projectName, gradleVersion);
4468
if(!checkCombination(projectName, gradleVersion)) return;
4569
var result = GradleRunner.create()
4670
.withProjectDir(new File(projectName + "/"))
4771
.withPluginClasspath(pluginClasspath)
48-
.withGradleVersion(gradleVersion)
72+
.withGradleVersion(gradleVersion.toString())
4973
.withArguments("-c", "smoke_test_settings.gradle", "clean", "build", "run", "--stacktrace")
5074
.forwardOutput()
5175
.build();
5276

5377
assertTasksSuccessful(result, "greeter.api", "build");
5478
assertTasksSuccessful(result, "greeter.provider", "build");
5579
assertTasksSuccessful(result, "greeter.provider.test", "build");
56-
if(GradleVersion.version(gradleVersion).compareTo(GradleVersion.version("5.6")) >= 0) {
80+
if(org.gradle.util.GradleVersion.version(gradleVersion.toString()).compareTo(org.gradle.util.GradleVersion.version("5.6")) >= 0) {
5781
assertTasksSuccessful(result, "greeter.provider.testfixture", "build");
5882
}
5983
assertTasksSuccessful(result, "greeter.runner", "build", "run");
6084
}
6185

6286
@CartesianProductTest(name = "smokeTestRun({arguments})")
6387
@CartesianValueSource(strings = {"test-project", "test-project-kotlin-pre-1-7", "test-project-kotlin", "test-project-groovy"})
64-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
65-
void smokeTestRun(String projectName, String gradleVersion) {
88+
@CartesianEnumSource(GradleVersion.class)
89+
void smokeTestRun(String projectName, GradleVersion gradleVersion) {
6690
LOGGER.lifecycle("Executing smokeTestRun of {} with Gradle {}", projectName, gradleVersion);
6791
if(!checkCombination(projectName, gradleVersion)) return;
6892
var writer = new StringWriter(256);
6993
var result = GradleRunner.create()
7094
.withProjectDir(new File(projectName + "/"))
7195
.withPluginClasspath(pluginClasspath)
72-
.withGradleVersion(gradleVersion)
96+
.withGradleVersion(gradleVersion.toString())
7397
.withArguments("-q", "-c", "smoke_test_settings.gradle", "clean", ":greeter.runner:run", "--args", "aaa bbb")
7498
.forwardStdOutput(writer)
7599
.forwardStdError(writer)
@@ -83,19 +107,18 @@ void smokeTestRun(String projectName, String gradleVersion) {
83107
assertEquals("welcome", lines.get(2));
84108
}
85109

86-
87110
@CartesianProductTest(name = "smokeTestJunit5({arguments})")
88111
@CartesianValueSource(strings = {"5.4.2/1.4.2", "5.5.2/1.5.2", "5.7.1/1.7.1"})
89-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
90-
void smokeTestJunit5(String junitVersionPair, String gradleVersion) {
112+
@CartesianEnumSource(GradleVersion.class)
113+
void smokeTestJunit5(String junitVersionPair, GradleVersion gradleVersion) {
91114
LOGGER.lifecycle("Executing smokeTestJunit5 with junitVersionPair {} and Gradle {}", junitVersionPair, gradleVersion);
92115
var junitVersionParts = junitVersionPair.split("/");
93116
var junitVersionProperty = String.format("-PjUnitVersion=%s", junitVersionParts[0]);
94117
var junitPlatformVersionProperty = String.format("-PjUnitPlatformVersion=%s", junitVersionParts[1]);
95118
var result = GradleRunner.create()
96119
.withProjectDir(new File("test-project/"))
97120
.withPluginClasspath(pluginClasspath)
98-
.withGradleVersion(gradleVersion)
121+
.withGradleVersion(gradleVersion.toString())
99122
.withArguments("-c", "smoke_test_settings.gradle", junitVersionProperty, junitPlatformVersionProperty, "clean", "build", "run", "--stacktrace")
100123
.forwardOutput()
101124
.build();
@@ -107,13 +130,13 @@ void smokeTestJunit5(String junitVersionPair, String gradleVersion) {
107130
}
108131

109132
@CartesianProductTest(name = "smokeTestMixed({arguments})")
110-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
111-
void smokeTestMixed(String gradleVersion) {
133+
@CartesianEnumSource(GradleVersion.class)
134+
void smokeTestMixed(GradleVersion gradleVersion) {
112135
LOGGER.lifecycle("Executing smokeTestMixed with Gradle {}", gradleVersion);
113136
var result = GradleRunner.create()
114137
.withProjectDir(new File("test-project-mixed"))
115138
.withPluginClasspath(pluginClasspath)
116-
.withGradleVersion(gradleVersion)
139+
.withGradleVersion(gradleVersion.toString())
117140
.withArguments("-c", "smoke_test_settings.gradle", "clean", "build", "--stacktrace")
118141
.forwardOutput()
119142
.build();
@@ -159,14 +182,14 @@ private static void assertExpectedClassFileFormats(
159182

160183
@CartesianProductTest(name = "smokeTestDist({arguments})")
161184
@CartesianValueSource(strings = {"test-project", "test-project-kotlin-pre-1-7", "test-project-kotlin", "test-project-groovy"})
162-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
163-
void smokeTestDist(String projectName, String gradleVersion) {
185+
@CartesianEnumSource(GradleVersion.class)
186+
void smokeTestDist(String projectName, GradleVersion gradleVersion) {
164187
LOGGER.lifecycle("Executing smokeTestDist of {} with Gradle {}", projectName, gradleVersion);
165188
if(!checkCombination(projectName, gradleVersion)) return;
166189
var result = GradleRunner.create()
167190
.withProjectDir(new File(projectName + "/"))
168191
.withPluginClasspath(pluginClasspath)
169-
.withGradleVersion(gradleVersion)
192+
.withGradleVersion(gradleVersion.toString())
170193
.withArguments("-c", "smoke_test_settings.gradle", "clean", "build", ":greeter.runner:installDist", "--stacktrace")
171194
.forwardOutput()
172195
.build();
@@ -200,14 +223,14 @@ void smokeTestDist(String projectName, String gradleVersion) {
200223

201224
@CartesianProductTest(name = "smokeTestRunDemo({arguments})")
202225
@CartesianValueSource(strings = {"test-project", "test-project-kotlin-pre-1-7", "test-project-kotlin", "test-project-groovy"})
203-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
204-
void smokeTestRunDemo(String projectName, String gradleVersion) {
226+
@CartesianEnumSource(GradleVersion.class)
227+
void smokeTestRunDemo(String projectName, GradleVersion gradleVersion) {
205228
LOGGER.lifecycle("Executing smokeTestRunDemo of {} with Gradle {}", projectName, gradleVersion);
206229
if(!checkCombination(projectName, gradleVersion)) return;
207230
var result = GradleRunner.create()
208231
.withProjectDir(new File(projectName + "/"))
209232
.withPluginClasspath(pluginClasspath)
210-
.withGradleVersion(gradleVersion)
233+
.withGradleVersion(gradleVersion.toString())
211234
.withArguments("-c", "smoke_test_settings.gradle", "clean", "build",
212235
":greeter.javaexec:runDemo1", ":greeter.javaexec:runDemo2", "--info", "--stacktrace")
213236
.forwardOutput()
@@ -219,14 +242,14 @@ void smokeTestRunDemo(String projectName, String gradleVersion) {
219242

220243
@CartesianProductTest(name = "smokeTestRunStartScripts({arguments})")
221244
@CartesianValueSource(strings = {"test-project", "test-project-kotlin-pre-1-7", "test-project-kotlin", "test-project-groovy"})
222-
@CartesianValueSource(strings = {"5.1", "5.6", "6.3", "6.4.1", "6.5.1", "6.8.3", "7.0", "7.2"})
223-
void smokeTestRunStartScripts(String projectName, String gradleVersion) {
245+
@CartesianEnumSource(GradleVersion.class)
246+
void smokeTestRunStartScripts(String projectName, GradleVersion gradleVersion) {
224247
LOGGER.lifecycle("Executing smokeTestRunScripts of {} with Gradle {}", projectName, gradleVersion);
225248
if(!checkCombination(projectName, gradleVersion)) return;
226249
var result = GradleRunner.create()
227250
.withProjectDir(new File(projectName + "/"))
228251
.withPluginClasspath(pluginClasspath)
229-
.withGradleVersion(gradleVersion)
252+
.withGradleVersion(gradleVersion.toString())
230253
.withArguments("-c", "smoke_test_settings.gradle", "clean", ":greeter.startscripts:installDist", "--info", "--stacktrace")
231254
.forwardOutput()
232255
.build();
@@ -241,15 +264,20 @@ void smokeTestRunStartScripts(String projectName, String gradleVersion) {
241264
assertEquals("Demo2: welcome home, Alice and Bob!", ctx.getAppOutput("demo2"));
242265
}
243266

267+
@Test
268+
void shouldNotCheckInWithCommentedOutVersions() {
269+
assertEquals(8, GradleVersion.values().length);
270+
}
271+
244272
private static void assertTasksSuccessful(BuildResult result, String subprojectName, String... taskNames) {
245273
for (String taskName : taskNames) {
246274
SmokeTestHelper.assertTaskSuccessful(result, subprojectName, taskName);
247275
}
248276
}
249277

250-
private static boolean checkCombination(String projectName, String gradleVersion) {
251-
final boolean kotlin_NotSupported = projectName.startsWith("test-project-kotlin") && gradleVersion.compareTo("6.4") < 0;
252-
final boolean kotlin1_7_NotSupported = projectName.equals("test-project-kotlin") && gradleVersion.compareTo("6.6") < 0;
278+
private static boolean checkCombination(String projectName, GradleVersion gradleVersion) {
279+
final boolean kotlin_NotSupported = projectName.startsWith("test-project-kotlin") && gradleVersion.toString().compareTo("6.4") < 0;
280+
final boolean kotlin1_7_NotSupported = projectName.equals("test-project-kotlin") && gradleVersion.toString().compareTo("6.6") < 0;
253281
if (kotlin_NotSupported || kotlin1_7_NotSupported) {
254282
LOGGER.lifecycle("Unsupported combination: {} / Gradle {}. Test skipped", projectName, gradleVersion);
255283
return false;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//region NO-OP (DSL testing)
2+
3+
// one of the supported Java version (prefer LTS version)
4+
def toolchainVer = 11
5+
6+
java {
7+
toolchain {
8+
languageVersion.set(JavaLanguageVersion.of(toolchainVer))
9+
}
10+
}
11+
12+
modularity {
13+
// This version should be less than `toolchainVer` because:
14+
// * if the version is same to `toolchainVer` we cannot reproduce the existing problem
15+
// * if the version is greather than `toolchainVer` javac cannot create class files
16+
standardJavaRelease(9)
17+
}
18+
//endregion
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package examples.greeter.api;
2+
3+
public interface Greeter {
4+
String hello();
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module greeter.api {
2+
exports examples.greeter.api;
3+
}

test-project/settings.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ include 'greeter.runner'
1515

1616
include 'greeter.javaexec'
1717
include 'greeter.startscripts'
18+
19+
if(GradleVersion.current().compareTo(GradleVersion.version("6.6")) >= 0) {
20+
include 'greeter.toolchain'
21+
}

0 commit comments

Comments
 (0)