-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
ProjectDependencyResolveIntegrationTest.groovy
752 lines (662 loc) · 22.3 KB
/
ProjectDependencyResolveIntegrationTest.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
* Copyright 2012 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.integtests.resolve
import groovy.transform.NotYetImplemented
import org.gradle.api.JavaVersion
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.FluidDependenciesResolveRunner
import org.gradle.integtests.fixtures.resolve.ResolveTestFixture
import org.junit.runner.RunWith
import spock.lang.Issue
@RunWith(FluidDependenciesResolveRunner)
class ProjectDependencyResolveIntegrationTest extends AbstractIntegrationSpec {
def setup() {
new ResolveTestFixture(buildFile, "compile").addDefaultVariantDerivationStrategy()
}
def "project dependency includes artifacts and transitive dependencies of default configuration in target project"() {
given:
mavenRepo.module("org.other", "externalA", "1.2").publish()
mavenRepo.module("org.other", "externalB", "2.1").publish()
and:
file('settings.gradle') << "include 'a', 'b'"
and:
buildFile << """
allprojects {
repositories { maven { url '$mavenRepo.uri' } }
}
project(":a") {
configurations {
api
'default' { extendsFrom api }
}
dependencies {
api "org.other:externalA:1.2"
'default' "org.other:externalB:2.1"
}
task jar(type: Jar) {
archiveBaseName = 'a'
destinationDirectory = buildDir
}
artifacts { api jar }
}
project(":b") {
group = 'org.gradle'
version = '1.0'
configurations {
compile
}
dependencies {
compile project(':a')
}
task check(dependsOn: configurations.compile) {
doLast {
assert configurations.compile.collect { it.name } == ['a.jar', 'externalA-1.2.jar', 'externalB-2.1.jar']
def result = configurations.compile.incoming.resolutionResult
// Check root component
def rootId = result.root.id
assert rootId instanceof ProjectComponentIdentifier
def rootPublishedAs = result.root.moduleVersion
assert rootPublishedAs.group == 'org.gradle'
assert rootPublishedAs.name == 'b'
assert rootPublishedAs.version == '1.0'
// Check project components
def projectComponents = result.root.dependencies.selected.findAll { it.id instanceof ProjectComponentIdentifier }
assert projectComponents.size() == 1
def projectA = projectComponents[0]
assert projectA.id.projectPath == ':a'
assert projectA.moduleVersion.group != null
assert projectA.moduleVersion.name == 'a'
assert projectA.moduleVersion.version == 'unspecified'
// Check project dependencies
def projectDependencies = result.root.dependencies.requested.findAll { it instanceof ProjectComponentSelector }
assert projectDependencies.size() == 1
def projectDependency = projectDependencies[0]
assert projectDependency.projectPath == ':a'
// Check external module components
def externalComponents = result.allDependencies.selected.findAll { it.id instanceof ModuleComponentIdentifier }
assert externalComponents.size() == 2
def externalA = externalComponents[0]
assert externalA.id.group == 'org.other'
assert externalA.id.module == 'externalA'
assert externalA.id.version == '1.2'
assert externalA.moduleVersion.group == 'org.other'
assert externalA.moduleVersion.name == 'externalA'
assert externalA.moduleVersion.version == '1.2'
def externalB = externalComponents[1]
assert externalB.id.group == 'org.other'
assert externalB.id.module == 'externalB'
assert externalB.id.version == '2.1'
assert externalB.moduleVersion.group == 'org.other'
assert externalB.moduleVersion.name == 'externalB'
assert externalB.moduleVersion.version == '2.1'
}
}
}
"""
expect:
succeeds ":b:check"
executedAndNotSkipped ":a:jar"
}
def "project dependency that specifies a target configuration includes artifacts and transitive dependencies of selected configuration"() {
given:
mavenRepo.module("org.other", "externalA", "1.2").publish()
and:
file('settings.gradle') << """rootProject.name='test'
include 'a', 'b'"""
and:
buildFile << """
allprojects {
repositories { maven { url '$mavenRepo.uri' } }
}
project(":a") {
apply plugin: 'base'
configurations {
api
runtime { extendsFrom api }
}
dependencies {
api("org.other:externalA:1.2") {
because 'also check dependency reasons'
}
}
task jar(type: Jar) { archiveBaseName = 'a' }
artifacts { api jar }
}
project(":b") {
configurations {
compile
}
dependencies {
compile(project(path: ':a', configuration: 'runtime')) {
because 'can provide a dependency reason for project dependencies too'
}
}
task check(dependsOn: configurations.compile) {
doLast {
assert configurations.compile.collect { it.name } == ['a.jar', 'externalA-1.2.jar']
}
}
}
"""
def resolve = new ResolveTestFixture(buildFile, "compile")
when:
resolve.prepare()
then:
succeeds ":b:check"
executedAndNotSkipped ":a:jar"
when:
succeeds ':b:checkDeps'
then:
resolve.expectGraph {
root(":b", "test:b:") {
project(":a", 'test:a:') {
byReason('can provide a dependency reason for project dependencies too')
variant('runtime')
module('org.other:externalA:1.2') {
byReason('also check dependency reasons')
variant('runtime', ['org.gradle.status': 'release', 'org.gradle.category':'library', 'org.gradle.usage':'java-runtime', 'org.gradle.libraryelements': 'jar'])
}
}
}
}
}
@Issue("GRADLE-2899")
def "multiple project configurations can refer to different configurations of target project"() {
given:
file('settings.gradle') << "include 'a', 'b'"
and:
buildFile << """
project(':a') {
apply plugin: 'base'
configurations {
configA1
configA2
}
task A1jar(type: Jar) {
archiveFileName = 'A1.jar'
}
task A2jar(type: Jar) {
archiveFileName = 'A2.jar'
}
artifacts {
configA1 A1jar
configA2 A2jar
}
}
project(':b') {
configurations {
configB1
configB2
}
dependencies {
configB1 project(path:':a', configuration:'configA1')
configB2 project(path:':a', configuration:'configA2')
}
task check(dependsOn: [configurations.configB1, configurations.configB2]) {
doLast {
assert configurations.configB1.collect { it.name } == ['A1.jar']
assert configurations.configB2.collect { it.name } == ['A2.jar']
}
}
}
"""
expect:
succeeds ":b:check"
executedAndNotSkipped ":a:A1jar", ":a:A2jar"
}
def "resolved project artifacts reflect project properties changed after task graph is resolved"() {
given:
file('settings.gradle') << "include 'a', 'b'"
and:
file('a/build.gradle') << '''
apply plugin: 'base'
configurations { compile }
dependencies { compile project(path: ':b', configuration: 'compile') }
task aJar(type: Jar) { }
gradle.taskGraph.whenReady { project.version = 'late' }
artifacts { compile aJar }
'''
file('b/build.gradle') << '''
apply plugin: 'base'
version = 'early'
configurations { compile }
task bJar(type: Jar) { }
gradle.taskGraph.whenReady { project.version = 'transitive-late' }
artifacts { compile bJar }
'''
file('build.gradle') << '''
configurations {
compile
testCompile { extendsFrom compile }
}
dependencies { compile project(path: ':a', configuration: 'compile') }
task test(dependsOn: [configurations.compile, configurations.testCompile]) {
doLast {
assert configurations.compile.collect { it.name } == ['a-late.jar', 'b-transitive-late.jar']
assert configurations.testCompile.collect { it.name } == ['a-late.jar', 'b-transitive-late.jar']
}
}
'''
expect:
succeeds ":test"
executedAndNotSkipped ":a:aJar", ":b:bJar"
}
def "resolved project artifact can be changed by configuration task"() {
given:
file('settings.gradle') << "include 'a'"
and:
file('a/build.gradle') << '''
apply plugin: 'base'
configurations { compile }
task configureJar {
doLast {
tasks.aJar.archiveExtension = "txt"
tasks.aJar.archiveClassifier = "modified"
}
}
task aJar(type: Jar) {
dependsOn configureJar
}
artifacts { compile aJar }
'''
file('build.gradle') << '''
configurations {
compile
testCompile { extendsFrom compile }
}
dependencies { compile project(path: ':a', configuration: 'compile') }
task test(dependsOn: [configurations.compile, configurations.testCompile]) {
doLast {
assert configurations.compile.collect { it.name } == ['a-modified.txt']
assert configurations.testCompile.collect { it.name } == ['a-modified.txt']
}
}
'''
expect:
succeeds ":test"
executedAndNotSkipped ":a:configureJar", ":a:aJar"
}
def "project dependency that references an artifact includes the matching artifact only plus the transitive dependencies of referenced configuration"() {
given:
mavenRepo.module("group", "externalA", "1.5").publish()
and:
file('settings.gradle') << "include 'a', 'b'"
and:
buildFile << """
allprojects {
apply plugin: 'base'
repositories { maven { url '${mavenRepo.uri}' } }
}
project(":a") {
configurations { 'default' {} }
dependencies { 'default' 'group:externalA:1.5' }
task xJar(type: Jar) { archiveBaseName='x' }
task yJar(type: Jar) { archiveBaseName='y' }
artifacts { 'default' xJar, yJar }
}
project(":b") {
configurations { compile }
dependencies { compile(project(':a')) { artifact { name = 'y'; type = 'jar' } } }
task test {
inputs.files configurations.compile
doFirst {
assert configurations.compile.files.collect { it.name } == ['y.jar', 'externalA-1.5.jar']
}
}
}
"""
expect:
succeeds 'b:test'
executedAndNotSkipped ":a:yJar"
}
def "reports project dependency that refers to an unknown artifact"() {
given:
file('settings.gradle') << """
include 'a', 'b'
"""
and:
buildFile << """
allprojects { group = 'test' }
project(":a") {
configurations { 'default' {} }
}
project(":b") {
configurations { compile }
dependencies { compile(project(':a')) { artifact { name = 'b'; type = 'jar' } } }
task test {
inputs.files configurations.compile
doFirst {
configurations.compile.files.collect { it.name }
}
}
}
"""
expect:
fails ':b:test'
and:
failure.assertHasCause("Could not resolve all files for configuration ':b:compile'.")
failure.assertHasCause("Could not find b.jar (project :a).")
}
def "non-transitive project dependency includes only the artifacts of the target configuration"() {
given:
mavenRepo.module("group", "externalA", "1.5").publish()
and:
file('settings.gradle') << "include 'a', 'b'"
and:
buildFile << """
allprojects {
apply plugin: 'java'
repositories { maven { url '${mavenRepo.uri}' } }
}
project(':a') {
dependencies {
implementation 'group:externalA:1.5'
implementation files('libs/externalB.jar')
}
}
project(':b') {
dependencies {
implementation project(':a'), { transitive = false }
}
task listJars(dependsOn: configurations.runtimeClasspath) {
doLast {
assert configurations.runtimeClasspath.collect { it.name } == ['a.jar']
}
}
}
"""
expect:
succeeds ":b:listJars"
executedAndNotSkipped ":a:jar"
}
def "can have cycle in project dependencies"() {
given:
file('settings.gradle') << "include 'a', 'b', 'c'"
and:
buildFile << """
subprojects {
apply plugin: 'base'
configurations {
'default'
other
}
task jar(type: Jar)
artifacts {
'default' jar
}
}
project('a') {
dependencies {
'default' project(':b')
other project(':b')
}
task listJars {
dependsOn configurations.default
dependsOn configurations.other
doFirst {
def jars = configurations.default.collect { it.name } as Set
assert jars == ['a.jar', 'b.jar', 'c.jar'] as Set
jars = configurations.other.collect { it.name } as Set
assert jars == ['a.jar', 'b.jar', 'c.jar'] as Set
// Check type of root component
def defaultResult = configurations.default.incoming.resolutionResult
def defaultRootId = defaultResult.root.id
assert defaultRootId instanceof ProjectComponentIdentifier
def otherResult = configurations.default.incoming.resolutionResult
def otherRootId = otherResult.root.id
assert otherRootId instanceof ProjectComponentIdentifier
}
}
}
project('b') {
dependencies {
'default' project(':c')
}
}
project('c') {
dependencies {
'default' project(':a')
}
}
"""
expect:
succeeds ":a:listJars"
executedAndNotSkipped ":b:jar", ":c:jar"
}
@NotYetImplemented
@Issue('GRADLE-3280')
def "can resolve recursive copy of configuration with cyclic project dependencies"() {
given:
settingsFile << "include 'a', 'b', 'c'"
buildScript '''
subprojects {
apply plugin: 'base'
task jar(type: Jar)
artifacts {
'default' jar
}
}
project('a') {
dependencies {
'default' project(':b')
}
task assertCanResolve {
doLast {
assert !project.configurations.default.resolvedConfiguration.hasError()
}
}
task assertCanResolveRecursiveCopy {
doLast {
assert !project.configurations.default.copyRecursive().resolvedConfiguration.hasError()
}
}
}
project('b') {
dependencies {
'default' project(':c')
}
}
project('c') {
dependencies {
'default' project(':a')
}
}
'''.stripIndent()
expect:
succeeds ':a:assertCanResolve'
and:
succeeds ':a:assertCanResolveRecursiveCopy'
}
// this test is largely covered by other tests, but does ensure that there is nothing special about
// project dependencies that are “built” by built in plugins like the Java plugin's created jars
def "can use zip files as project dependencies"() {
given:
file("settings.gradle") << "include 'a'; include 'b'"
file("a/some.txt") << "foo"
file("a/build.gradle") << """
group = "g"
version = 1.0
apply plugin: 'base'
task zip(type: Zip) {
from "some.txt"
}
artifacts {
delegate.default zip
}
"""
file("b/build.gradle") << """
configurations { conf }
dependencies {
conf project(":a")
}
task copyZip(type: Copy) {
from configurations.conf
into "\$buildDir/copied"
}
"""
when:
succeeds ":b:copyZip"
then:
executedAndNotSkipped ":a:zip", ":b:copyZip"
and:
file("b/build/copied/a-1.0.zip").exists()
}
def "resolving configuration with project dependency marks dependency's configuration as observed"() {
settingsFile << "include 'api'; include 'impl'"
buildFile << """
allprojects {
configurations {
conf
}
configurations.create("default").extendsFrom(configurations.conf)
}
project(":impl") {
dependencies {
conf project(":api")
}
task check {
doLast {
assert configurations.conf.state == Configuration.State.UNRESOLVED
assert project(":api").configurations.conf.state == Configuration.State.UNRESOLVED
configurations.conf.resolve()
assert configurations.conf.state == Configuration.State.RESOLVED
assert project(":api").configurations.conf.state == Configuration.State.UNRESOLVED
// Attempt to change the configuration, to demonstrate that is has been observed
project(":api").configurations.conf.dependencies.add(null)
}
}
}
"""
when:
fails("impl:check")
then:
failure.assertHasCause "Cannot change dependencies of dependency configuration ':api:conf' after it has been included in dependency resolution"
}
@Issue(["GRADLE-3330", "GRADLE-3362"])
def "project dependency can resolve multiple artifacts from target project that are differentiated by archiveFileName only"() {
given:
file('settings.gradle') << "include 'a', 'b'"
and:
buildFile << """
project(':a') {
apply plugin: 'base'
configurations {
configOne
configTwo
}
task A1jar(type: Jar) {
archiveFileName = 'A1.jar'
}
task A2jar(type: Jar) {
archiveFileName = 'A2.jar'
}
task A3jar(type: Jar) {
archiveFileName = 'A3.jar'
}
artifacts {
configOne A1jar
configTwo A2jar
configTwo A3jar
}
}
project(':b') {
configurations {
configB
}
dependencies {
configB project(path:':a', configuration:'configOne')
configB project(path:':a', configuration:'configTwo')
}
task check(dependsOn: configurations.configB) {
doLast {
assert configurations.configB.collect { it.name } == ['A1.jar', 'A2.jar', 'A3.jar']
}
}
}
"""
expect:
succeeds ":b:check"
executedAndNotSkipped ":a:A1jar", ":a:A2jar", ":a:A3jar"
}
@Issue("https://github.com/gradle/gradle/issues/847")
def "projects with the same name should be considered different when building the graph"() {
given:
settingsFile << """
rootProject.name='duplicates'
include 'a:core'
include 'b:core'
"""
buildFile << """
allprojects {
apply plugin: 'java-library'
group 'org.test'
version '1.0'
}
project(':a:core') {
dependencies {
implementation project(':b:core')
}
}
"""
def resolve = new ResolveTestFixture(buildFile, "compileClasspath")
resolve.prepare()
when:
succeeds 'a:core:checkDeps'
then:
resolve.expectGraph {
root(":a:core", "org.test:a-core:1.0") {
project(":b:core", "org.test:b-core:1.0") {
variant("apiElements", [
'org.gradle.category': 'library',
'org.gradle.dependency.bundling': 'external',
'org.gradle.jvm.version': JavaVersion.current().majorVersion,
'org.gradle.usage': 'java-api',
'org.gradle.libraryelements': 'jar'])
artifact(name: 'main', noType: true)
}
}
}
}
@Issue("https://github.com/gradle/gradle/issues/847")
def "can opt-out detection of circular dependencies with projects of same name"() {
given:
settingsFile << """
rootProject.name='duplicates'
include 'a:core'
include 'b:core'
"""
buildFile << """
allprojects {
apply plugin: 'java-library'
group 'org.test'
version '1.0'
}
project(':a:core') {
dependencies {
implementation project(':b:core')
}
}
"""
def resolve = new ResolveTestFixture(buildFile, "compileClasspath")
resolve.prepare()
when:
fails 'a:core:checkDeps', '-Dorg.gradle.dependency.duplicate.project.detection=false'
then:
failure.assertHasDescription """Circular dependency between the following tasks:
:a:core:compileJava
\\--- :a:core:compileJava (*)"""
}
}