-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
JavaCompileToolchainIntegrationTest.groovy
583 lines (481 loc) · 22.4 KB
/
JavaCompileToolchainIntegrationTest.groovy
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.tasks.compile
import org.gradle.api.JavaVersion
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.AvailableJavaHomes
import org.gradle.integtests.fixtures.jvm.JavaToolchainFixture
import org.gradle.internal.jvm.Jvm
import org.gradle.internal.os.OperatingSystem
import org.gradle.test.fixtures.file.TestFile
import org.gradle.util.Requires
import org.gradle.util.TestPrecondition
import org.gradle.util.internal.TextUtil
import spock.lang.Issue
import static org.junit.Assume.assumeNotNull
class JavaCompileToolchainIntegrationTest extends AbstractIntegrationSpec implements JavaToolchainFixture {
def setup() {
file("src/main/java/Foo.java") << "public class Foo {}"
}
def "fails on toolchain and forkOptions mismatch when #when"() {
def currentJdk = Jvm.current()
def otherJdk = AvailableJavaHomes.differentVersion
buildFile << """
apply plugin: "java"
"""
if (tool != null) {
configureTool(tool == "current" ? currentJdk : otherJdk)
}
if (javaHome != null) {
configureForkOptionsJavaHome(javaHome == "current" ? currentJdk : otherJdk)
}
if (executable != null) {
configureForkOptionsExecutable(executable == "current" ? currentJdk : otherJdk)
}
when:
withInstallations(currentJdk, otherJdk).runAndFail(":compileJava")
then:
failureDescriptionStartsWith("Execution failed for task ':compileJava'.")
failureHasCause("Toolchain from `${errorFor}` property on `ForkOptions` does not match toolchain from `javaCompiler` property")
where:
when | tool | javaHome | executable | errorFor
"java home disagrees with executable" | null | "other" | "current" | "executable"
"tool disagrees with executable" | "other" | null | "current" | "executable"
"tool disagrees with java home" | "other" | "current" | null | "javaHome"
"tool disagrees with " | "other" | "current" | "current" | "javaHome"
}
def "fails on toolchain and forkOptions mismatch when #when (without java base plugin)"() {
def currentJdk = Jvm.current()
def otherJdk = AvailableJavaHomes.differentVersion
def compileWithVersion = [currentJdk, otherJdk].collect { it.javaVersion }.min()
buildFile << """
plugins {
id 'jvm-toolchains'
}
task compileJava(type: JavaCompile) {
classpath = project.layout.files()
source = project.layout.files("src/main/java")
destinationDirectory = project.layout.buildDirectory.dir("classes/java/main")
sourceCompatibility = "${compileWithVersion}"
targetCompatibility = "${compileWithVersion}"
}
"""
if (tool != null) {
configureTool(tool == "current" ? currentJdk : otherJdk)
}
if (javaHome != null) {
configureForkOptionsJavaHome(javaHome == "current" ? currentJdk : otherJdk)
}
if (executable != null) {
configureForkOptionsExecutable(executable == "current" ? currentJdk : otherJdk)
}
when:
withInstallations(currentJdk, otherJdk).runAndFail(":compileJava")
then:
failureDescriptionStartsWith("Execution failed for task ':compileJava'.")
failureHasCause("Toolchain from `${errorForProperty}` property on `ForkOptions` does not match toolchain from `javaCompiler` property")
where:
when | tool | javaHome | executable | errorForProperty
"java home disagrees with executable" | null | "other" | "current" | "executable"
"tool disagrees with executable" | "other" | null | "current" | "executable"
"tool disagrees with java home" | "other" | "current" | null | "javaHome"
"tool disagrees with " | "other" | "current" | "current" | "javaHome"
}
def "uses #what toolchain #when (with java plugin)"() {
def currentJdk = Jvm.current()
def otherJdk = AvailableJavaHomes.differentVersion
def selectJdk = { it == "other" ? otherJdk : it == "current" ? currentJdk : null }
buildFile << """
apply plugin: "java"
"""
if (withTool != null) {
configureTool(selectJdk(withTool))
}
if (withJavaHome != null) {
configureForkOptionsJavaHome(selectJdk(withJavaHome))
}
if (withExecutable != null) {
configureForkOptionsExecutable(selectJdk(withExecutable))
}
if (withJavaExtension != null) {
configureJavaPluginToolchainVersion(selectJdk(withJavaExtension))
}
def targetJdk = selectJdk(target)
when:
withInstallations(currentJdk, otherJdk).run(":compileJava", "--info")
then:
executedAndNotSkipped(":compileJava")
outputContains("Compiling with toolchain '${targetJdk.javaHome.absolutePath}'")
classJavaVersion(javaClassFile("Foo.class")) == targetJdk.javaVersion
where:
// Some cases are skipped, because forkOptions (when configured) must match the resulting toolchain, otherwise the build fails
what | when | withTool | withJavaHome | withExecutable | withJavaExtension | target
"current JVM" | "when nothing is configured" | null | null | null | null | "current"
"java extension" | "when configured" | null | null | null | "other" | "other"
"executable" | "when configured" | null | null | "other" | null | "other"
"java home" | "when configured" | null | "other" | null | null | "other"
"assigned tool" | "when configured" | "other" | null | null | null | "other"
"executable" | "over java extension" | null | null | "other" | "current" | "other"
"java home" | "over java extension" | null | "other" | null | "current" | "other"
"assigned tool" | "over java extension" | "other" | null | null | "current" | "other"
}
def "uses #what toolchain #when (without java base plugin)"() {
def currentJdk = Jvm.current()
def otherJdk = AvailableJavaHomes.differentVersion
def selectJdk = { it == "other" ? otherJdk : it == "current" ? currentJdk : null }
def compileWithVersion = [currentJdk, otherJdk].collect { it.javaVersion }.min()
buildFile << """
plugins {
id 'jvm-toolchains'
}
task compileJava(type: JavaCompile) {
classpath = project.layout.files()
source = project.layout.files("src/main/java")
destinationDirectory = project.layout.buildDirectory.dir("classes/java/main")
sourceCompatibility = "${compileWithVersion}"
targetCompatibility = "${compileWithVersion}"
}
"""
if (withTool != null) {
configureTool(selectJdk(withTool))
}
if (withJavaHome != null) {
configureForkOptionsJavaHome(selectJdk(withJavaHome))
}
if (withExecutable != null) {
configureForkOptionsExecutable(selectJdk(withExecutable))
}
def targetJdk = selectJdk(target)
when:
withInstallations(currentJdk, otherJdk).run(":compileJava", "--info")
then:
executedAndNotSkipped(":compileJava")
outputContains("Compiling with toolchain '${targetJdk.javaHome.absolutePath}'")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(compileWithVersion)
where:
// Some cases are skipped, because forkOptions (when configured) must match the resulting toolchain, otherwise the build fails
what | when | withTool | withJavaHome | withExecutable | target
"current JVM" | "when toolchains are not configured" | null | null | null | "current"
"executable" | "when configured" | null | null | "other" | "other"
"java home" | "when configured" | null | "other" | null | "other"
"assigned tool" | "when configured" | "other" | null | null | "other"
}
def "uses toolchain from forkOptions #forkOption when it points outside of installations"() {
def currentJdk = Jvm.current()
def otherJdk = AvailableJavaHomes.differentVersion
def path = TextUtil.normaliseFileSeparators(otherJdk.javaHome.absolutePath.toString() + appendPath)
def compatibilityVersion = [currentJdk, otherJdk].collect { it.javaVersion }.min()
buildFile << """
apply plugin: "java"
compileJava {
options.fork = true
${configure.replace("<path>", path)}
sourceCompatibility = "${compatibilityVersion}"
targetCompatibility = "${compatibilityVersion}"
}
"""
when:
// not adding the other JDK to the installations
withInstallations(currentJdk).run(":compileJava", "--info")
then:
executedAndNotSkipped(":compileJava")
outputContains("Compiling with toolchain '${otherJdk.javaHome.absolutePath}'")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(compatibilityVersion)
where:
forkOption | configure | appendPath
"java home" | 'options.forkOptions.javaHome = file("<path>")' | ''
"executable" | 'options.forkOptions.executable = "<path>"' | OperatingSystem.current().getExecutableName('/bin/javac')
}
@Issue("https://github.com/gradle/gradle/issues/22398")
def "ignore #forkOption if not forking"() {
def curJvm = Jvm.current()
def otherJvm = AvailableJavaHomes.getDifferentJdk()
def path = TextUtil.normaliseFileSeparators(otherJvm.javaHome.absolutePath + appendPath)
buildFile << """
apply plugin: "java"
compileJava {
// we do not set `options.fork = true`
${configure.replace("<path>", path)}
}
"""
when:
run(":compileJava", "--info")
then:
executedAndNotSkipped(":compileJava")
outputContains("Compiling with toolchain '${curJvm.javaHome.absolutePath}'")
outputContains("Compiling with JDK Java compiler API")
outputDoesNotContain("Compiling with Java command line compiler")
outputDoesNotContain("Started Gradle worker daemon")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(curJvm.javaVersion)
where:
forkOption | configure | appendPath
"java home" | 'options.forkOptions.javaHome = file("<path>")' | ''
"executable" | 'options.forkOptions.executable = "<path>"' | OperatingSystem.current().getExecutableName('/bin/javac')
}
def 'fails when requesting not available toolchain'() {
buildFile << """
apply plugin: 'java'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(99)
}
}
"""
when:
failure = executer
.withToolchainDetectionEnabled()
.withTasks("compileJava")
.runWithFailure()
then:
failure.assertHasDocumentedCause("No locally installed toolchains match (see https://docs.gradle.org/current/userguide/toolchains.html#sec:auto_detection) " +
"and toolchain auto-provisioning is not enabled (see https://docs.gradle.org/current/userguide/toolchains.html#sec:auto_detection).")
}
@Requires(adhoc = { AvailableJavaHomes.getJdk(JavaVersion.VERSION_1_7) != null })
def "can use toolchains to compile java 1.7 code"() {
def jdk = AvailableJavaHomes.getJdk(JavaVersion.VERSION_1_7)
buildFile << """
apply plugin: "java"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(7)
}
}
"""
when:
withInstallations(jdk).run(":compileJava", "--info")
then:
outputContains("Compiling with Java command line compiler")
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'.")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(jdk.javaVersion)
}
def "uses correct vendor when selecting a toolchain"() {
def jdk = Jvm.current()
buildFile << """
apply plugin: "java"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(${jdk.javaVersion.majorVersion})
vendor = JvmVendorSpec.matching("${System.getProperty("java.vendor").toLowerCase()}")
}
}
"""
when:
withInstallations(jdk).run(":compileJava", "--info")
then:
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'.")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(jdk.javaVersion)
}
def "fails if no toolchain has a matching vendor"() {
def version = Jvm.current().javaVersion.majorVersion
buildFile << """
apply plugin: "java"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(${version})
vendor = JvmVendorSpec.AMAZON
}
}
"""
when:
fails("compileJava")
then:
failure.assertHasDocumentedCause("No locally installed toolchains match (see https://docs.gradle.org/current/userguide/toolchains.html#sec:auto_detection) " +
"and toolchain auto-provisioning is not enabled (see https://docs.gradle.org/current/userguide/toolchains.html#sec:auto_detection).")
}
def "can use compile daemon with tools jar"() {
def jdk = AvailableJavaHomes.getJdk(JavaVersion.VERSION_1_8)
buildFile << """
apply plugin: "java"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
"""
when:
withInstallations(jdk).run(":compileJava", "--info")
then:
outputDoesNotContain("Compiling with Java command line compiler")
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'.")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(jdk.javaVersion)
}
def "can compile Java using different JDKs"() {
def jdk = AvailableJavaHomes.getJdk(javaVersion)
assumeNotNull(jdk)
buildFile << """
plugins {
id("java")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(${jdk.javaVersion.majorVersion})
}
}
"""
when:
withInstallations(jdk).run(":compileJava", "--info")
then:
outputDoesNotContain("Compiling with Java command line compiler")
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'.")
classJavaVersion(javaClassFile("Foo.class")) == JavaVersion.toVersion(jdk.javaVersion)
where:
javaVersion << JavaVersion.values().findAll { it.isJava8Compatible() }
}
/**
* This test covers the case where in Java8 the class name becomes fully qualified in the deprecation message which is
* somehow caused by invoking javacTask.getElements() in the IncrementalCompileTask of the incremental compiler plugin.
*/
@Requires(TestPrecondition.JDK9_OR_LATER)
def "Java deprecation messages with different JDKs"() {
def jdk = AvailableJavaHomes.getJdk(javaVersion)
buildFile << """
plugins {
id("java")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(${jdk.javaVersion.majorVersion})
}
}
tasks.withType(JavaCompile).configureEach {
options.compilerArgs << "-Xlint:deprecation"
}
"""
file("src/main/java/com/example/Foo.java") << """
package com.example;
public class Foo {
@Deprecated
public void foo() {}
}
"""
def fileWithDeprecation = file("src/main/java/com/example/Bar.java") << """
package com.example;
public class Bar {
public void bar() {
new Foo().foo();
}
}
"""
//noinspection GrDeprecatedAPIUsage
executer.expectDeprecationWarning("$fileWithDeprecation:5: warning: $deprecationMessage")
when:
withInstallations(jdk).run(":compileJava", "--info")
then:
outputDoesNotContain("Compiling with Java command line compiler")
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'.")
outputContains("Compiling with JDK Java compiler API.")
javaClassFile("com/example/Foo.class").exists()
javaClassFile("com/example/Bar.class").exists()
where:
javaVersion | deprecationMessage
JavaVersion.VERSION_1_8 | "[deprecation] foo() in com.example.Foo has been deprecated"
JavaVersion.current() | "[deprecation] foo() in Foo has been deprecated"
}
@Issue("https://github.com/gradle/gradle/issues/23990")
@Requires(TestPrecondition.JDK11_OR_LATER)
def "can compile with a custom compiler executable"() {
def jdk = AvailableJavaHomes.getJdk(JavaVersion.current())
def otherJdk = AvailableJavaHomes.differentVersion
buildFile << """
plugins {
id("java")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(${otherJdk.javaVersion.majorVersion})
}
}
configurations {
ecj {
canBeConsumed = false
canBeResolved = true
}
}
${mavenCentralRepository()}
dependencies {
def changed = providers.gradleProperty("changed").isPresent()
ecj(!changed ? "org.eclipse.jdt:ecj:3.31.0" : "org.eclipse.jdt:ecj:3.32.0")
}
// Make sure the provider is up-to-date only if the ECJ classpath does not change
class EcjClasspathProvider implements CommandLineArgumentProvider {
@Classpath
final FileCollection ecjClasspath
EcjClasspathProvider(FileCollection ecjClasspath) {
this.ecjClasspath = ecjClasspath
}
@Override
List<String> asArguments() {
return ["-cp", ecjClasspath.asPath, "org.eclipse.jdt.internal.compiler.batch.Main"]
}
}
compileJava {
def customJavaLauncher = javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(${jdk.javaVersion.majorVersion}))
}.get()
// ECJ does not support generating JNI headers
options.headerOutputDirectory.set(provider { null })
options.fork = true
options.forkOptions.executable = customJavaLauncher.executablePath.asFile.absolutePath
options.forkOptions.jvmArgumentProviders.add(new EcjClasspathProvider(configurations.ecj))
}
"""
when:
withInstallations(jdk, otherJdk).run(":compileJava", "--info")
then:
executedAndNotSkipped(":compileJava")
outputContains("Compiling with toolchain '${jdk.javaHome.absolutePath}'")
outputContains("Compiling with Java command line compiler '${jdk.javaExecutable.absolutePath}'")
classJavaVersion(javaClassFile("Foo.class")) == jdk.javaVersion
// Test up-to-date checks
when:
withInstallations(jdk, otherJdk).run(":compileJava")
then:
skipped(":compileJava")
when:
withInstallations(jdk, otherJdk).run(":compileJava", "-Pchanged")
then:
executedAndNotSkipped(":compileJava")
when:
withInstallations(jdk, otherJdk).run(":compileJava", "-Pchanged")
then:
skipped(":compileJava")
}
private TestFile configureForkOptionsExecutable(Jvm jdk) {
buildFile << """
compileJava {
options.fork = true
options.forkOptions.executable = "${TextUtil.normaliseFileSeparators(jdk.javacExecutable.absolutePath)}"
}
"""
}
private TestFile configureForkOptionsJavaHome(Jvm jdk) {
buildFile << """
compileJava {
options.fork = true
options.forkOptions.javaHome = file("${TextUtil.normaliseFileSeparators(jdk.javaHome.absolutePath)}")
}
"""
}
private TestFile configureTool(Jvm jdk) {
buildFile << """
compileJava {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(${jdk.javaVersion.majorVersion})
}
}
"""
}
}