/
ConfigureRuntimeClasspathNormalizationIntegrationTest.groovy
863 lines (740 loc) · 32.4 KB
/
ConfigureRuntimeClasspathNormalizationIntegrationTest.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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
* Copyright 2017 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.normalization
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.ToBeFixedForConfigurationCache
import org.gradle.integtests.fixtures.UnsupportedWithConfigurationCache
import org.gradle.test.fixtures.file.TestFile
import org.gradle.util.TextUtil
import spock.lang.Issue
import spock.lang.Unroll
import java.util.jar.Attributes
import java.util.jar.Manifest
@Unroll
class ConfigureRuntimeClasspathNormalizationIntegrationTest extends AbstractIntegrationSpec {
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can ignore files on runtime classpath in #tree (using runtime API: #api)"() {
def project = new ProjectWithRuntimeClasspathNormalization(api).withFilesIgnored()
def ignoredResource = project[ignoredResourceName]
def notIgnoredResource = project[notIgnoredResourceName]
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
ignoredResource.changeContents()
succeeds project.customTask
then:
skipped(project.customTask)
when:
notIgnoredResource.changeContents()
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
ignoredResource.remove()
succeeds project.customTask
then:
skipped(project.customTask)
when:
ignoredResource.add()
succeeds project.customTask
then:
skipped(project.customTask)
where:
tree | ignoredResourceName | notIgnoredResourceName | api
'directories' | 'ignoredResourceInDirectory' | 'notIgnoredResourceInDirectory' | Api.RUNTIME
'jars' | 'ignoredResourceInJar' | 'notIgnoredResourceInJar' | Api.RUNTIME
'nested jars' | 'ignoredResourceInNestedJar' | 'notIgnoredResourceInNestedJar' | Api.RUNTIME
'nested in dir jars' | 'ignoredResourceInNestedInDirJar' | 'notIgnoredResourceInNestedInDirJar' | Api.RUNTIME
'directories' | 'ignoredResourceInDirectory' | 'notIgnoredResourceInDirectory' | Api.ANNOTATION
'jars' | 'ignoredResourceInJar' | 'notIgnoredResourceInJar' | Api.ANNOTATION
'nested jars' | 'ignoredResourceInNestedJar' | 'notIgnoredResourceInNestedJar' | Api.ANNOTATION
'nested in dir jars' | 'ignoredResourceInNestedInDirJar' | 'notIgnoredResourceInNestedInDirJar' | Api.ANNOTATION
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
@Unroll
def "can ignore manifest attributes in #tree on runtime classpath"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME).withManifestAttributesIgnored()
def manifestResource = project[resourceName]
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
manifestResource.changeAttributes((IMPLEMENTATION_VERSION): "1.0.1")
succeeds project.customTask
then:
skipped(project.customTask)
where:
tree | resourceName
'jar' | 'jarManifest'
'directory' | 'manifestInDirectory'
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
@Unroll
def "can ignore entire manifest in #tree on runtime classpath"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME).withManifestIgnored()
def manifestResource = project[resourceName]
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
manifestResource.changeAttributes((IMPLEMENTATION_VERSION): "1.0.1")
succeeds project.customTask
then:
skipped(project.customTask)
where:
tree | resourceName
'jar' | 'jarManifest'
'directory' | 'manifestInDirectory'
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
@Unroll
def "can ignore all meta-inf files in #tree on runtime classpath"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME).withAllMetaInfIgnored()
def manifestResource = project[resourceName]
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
manifestResource.changeAttributes((IMPLEMENTATION_VERSION): "1.0.1")
project.jarManifestProperties.replaceContents("implementation-version=1.0.1")
succeeds project.customTask
then:
skipped(project.customTask)
where:
tree | resourceName
'jar' | 'jarManifest'
'directory' | 'manifestInDirectory'
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can ignore manifest properties on runtime classpath"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME).withManifestPropertiesIgnored()
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.jarManifestProperties.replaceContents("implementation-version=1.0.1")
succeeds project.customTask
then:
skipped(project.customTask)
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can configure ignore rules per project (using runtime API: #api)"() {
def projectWithIgnores = new ProjectWithRuntimeClasspathNormalization('a', api).withFilesIgnored()
def projectWithoutIgnores = new ProjectWithRuntimeClasspathNormalization('b', api)
def allProjects = [projectWithoutIgnores, projectWithIgnores]
settingsFile << "include 'a', 'b'"
when:
succeeds(*allProjects*.customTask)
then:
executedAndNotSkipped(*allProjects*.customTask)
when:
projectWithIgnores.ignoredResourceInJar.changeContents()
projectWithoutIgnores.ignoredResourceInJar.changeContents()
succeeds(*allProjects*.customTask)
then:
skipped(projectWithIgnores.customTask)
executedAndNotSkipped(projectWithoutIgnores.customTask)
where:
api << [Api.RUNTIME, Api.ANNOTATION]
}
@UnsupportedWithConfigurationCache(because = "Task.getProject() during execution")
def "runtime classpath normalization to #change cannot be changed after first usage (using runtime API: #useRuntimeApi)"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
project.buildFile << """
task configureNormalization() {
dependsOn '${project.customTask}'
doLast {
project.normalization {
runtimeClasspath {
${config}
}
}
}
}
""".stripIndent()
when:
fails 'configureNormalization'
then:
failureHasCause 'Cannot configure runtime classpath normalization after execution started.'
where:
change | config | api
'ignore file' | "ignore '**/some-other-file.txt'" | Api.RUNTIME
'ignore file' | "ignore '**/some-other-file.txt'" | Api.ANNOTATION
'ignore manifest attribute' | "metaInf { ignoreAttribute '${IMPLEMENTATION_VERSION}' }" | Api.RUNTIME
'ignore manifest attribute' | "metaInf { ignoreAttribute '${IMPLEMENTATION_VERSION}' }" | Api.ANNOTATION
'ignore property' | "properties { ignoreProperty 'timestamp' }" | Api.RUNTIME
'ignore property' | "properties { ignoreProperty 'timestamp' }" | Api.ANNOTATION
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can ignore properties on runtime classpath in #tree (using runtime API: #api)"() {
def project = new ProjectWithRuntimeClasspathNormalization(api).withPropertiesIgnored()
def ignoredResource = project[ignoredResourceName]
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
ignoredResource.changeProperty(IGNORE_ME, 'please ignore me')
succeeds project.customTask
then:
skipped(project.customTask)
when:
ignoredResource.changeProperty(DONT_IGNORE_ME, 'please dont ignore me')
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
where:
tree | ignoredResourceName | api
'directories' | 'propertiesFileInDir' | Api.RUNTIME
'jars' | 'propertiesFileInJar' | Api.RUNTIME
'nested jars' | 'propertiesFileInNestedJar' | Api.RUNTIME
'nested in dir jars' | 'propertiesFileInNestedInDirJar' | Api.RUNTIME
'directories' | 'propertiesFileInDir' | Api.ANNOTATION
'jars' | 'propertiesFileInJar' | Api.ANNOTATION
'nested jars' | 'propertiesFileInNestedJar' | Api.ANNOTATION
'nested in dir jars' | 'propertiesFileInNestedInDirJar' | Api.ANNOTATION
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can ignore properties in selected files"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
def notIgnoredPropertiesFile = new PropertiesResource(project.root.file('classpath/dirEntry/bar.properties'), [(IGNORE_ME_TOO): 'this should not actually be ignored'])
project.buildFile << """
normalization {
runtimeClasspath {
properties('**/foo.properties') {
ignoreProperty '${IGNORE_ME}'
}
}
}
"""
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.propertiesFileInDir.changeProperty(IGNORE_ME, 'please ignore me')
succeeds project.customTask
then:
skipped(project.customTask)
when:
notIgnoredPropertiesFile.changeProperty(IGNORE_ME, 'please dont ignore me')
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can ignore properties in selected files defined in multiple rules"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
def notIgnoredPropertiesFile = new PropertiesResource(project.root.file('classpath/dirEntry/bar.properties'), [(IGNORE_ME_TOO): 'this should not actually be ignored'])
project.propertiesFileInDir.changeProperty(IGNORE_ME_TOO, 'this should also be ignored')
project.buildFile << """
normalization {
runtimeClasspath {
properties('**/foo.properties') {
ignoreProperty '${IGNORE_ME}'
}
properties('some/path/to/foo.properties') {
ignoreProperty '${IGNORE_ME_TOO}'
}
}
}
"""
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.propertiesFileInDir.changeProperty(IGNORE_ME, 'please ignore me')
project.propertiesFileInDir.changeProperty(IGNORE_ME_TOO, 'please ignore me too')
succeeds project.customTask
then:
skipped(project.customTask)
when:
notIgnoredPropertiesFile.changeProperty(IGNORE_ME, 'please dont ignore me')
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
}
def "properties files are normalized against changes to whitespace, order and comments"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
project.propertiesFileInDir.setContent('''
foo=bar
bar=baz
fizz=fuzz
'''.stripIndent())
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.propertiesFileInDir
.setComment('this comment should be ignored')
.setContent('''
# Some comment
bar=baz
fizz=fuzz
# Another comment
foo=bar
'''.stripIndent())
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.propertiesFileInDir.changeProperty('foo', 'baz')
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
def "can add rules to the default properties rule"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
def notIgnoredPropertiesFile = new PropertiesResource(project.root.file('classpath/dirEntry/bar.properties'), [(IGNORE_ME): 'this should not actually be ignored'])
project.propertiesFileInDir.changeProperty(IGNORE_ME_TOO, 'this should also be ignored')
project.buildFile << """
normalization {
runtimeClasspath {
properties('**/foo.properties') {
ignoreProperty '${IGNORE_ME}'
}
properties {
ignoreProperty '${IGNORE_ME_TOO}'
}
}
}
"""
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.propertiesFileInDir.changeProperty(IGNORE_ME, 'please ignore me')
project.propertiesFileInDir.changeProperty(IGNORE_ME_TOO, 'please ignore me too')
succeeds project.customTask
then:
skipped(project.customTask)
when:
notIgnoredPropertiesFile.changeProperty(IGNORE_ME, 'please dont ignore me')
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
}
def "safely handles properties files with bad unicode escape sequences"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
project.propertiesFileInDir.backingFile.text = 'propertyWithBadValue=this is a bad unicode sequence \\uxxxx'
when:
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
succeeds project.customTask
then:
skipped(project.customTask)
when:
project.propertiesFileInDir.backingFile.text = 'propertyWithBadValue=this is also a bad unicode sequence \\uyyyy'
succeeds project.customTask
then:
executedAndNotSkipped(project.customTask)
}
@ToBeFixedForConfigurationCache(because = "classpath normalization - see https://github.com/gradle/gradle/issues/13706")
@Issue('https://github.com/gradle/gradle/issues/16144')
def "changing normalization configuration rules changes build cache key (#description)"() {
def project = new ProjectWithRuntimeClasspathNormalization(Api.RUNTIME)
project.propertiesFileInJar.changeProperty(ALSO_IGNORE_ME, 'some value')
// We implement this with a flag, rather than just changing the build script, because we don't want the change in build script to affect
// the cache hit. We want the only change to be in the normalization rules so we can be sure that's what's changing the cache key.
project.buildFile << """
normalization {
runtimeClasspath {
if (project.hasProperty('${enableFilterFlag}')) {
${normalizationRule}
}
}
}
"""
when:
args('--build-cache')
succeeds 'clean', project.customTask
then:
executedAndNotSkipped(project.customTask)
when:
args("-P${enableFilterFlag}", '--build-cache')
succeeds 'clean', project.customTask
then:
executedAndNotSkipped(project.customTask)
where:
enableFilterFlag | normalizationRule | description
PROPERTIES_FILTER_FLAG | "properties('**/foo.properties') { ignoreProperty '${ALSO_IGNORE_ME}' }" | 'properties rule'
META_INF_FILTER_FLAG | "metaInf { ignoreAttribute '${IMPLEMENTATION_VERSION}' }" | 'meta-inf rule'
FILE_FILTER_FLAG | "ignore '**/ignored.txt'" | 'ignore rule'
}
static final String IGNORE_ME = 'ignore-me'
static final String ALSO_IGNORE_ME = 'also-ignore-me'
static final String IGNORE_ME_TOO = 'ignore-me-too'
static final String DONT_IGNORE_ME = 'dont-ignore-me'
static final String IMPLEMENTATION_VERSION = Attributes.Name.IMPLEMENTATION_VERSION.toString()
static final String PROPERTIES_FILTER_FLAG = "filterProperties"
static final String META_INF_FILTER_FLAG = "filterMetaInf"
static final String FILE_FILTER_FLAG = "filterFile"
enum Api {
RUNTIME, ANNOTATION
}
class ProjectWithRuntimeClasspathNormalization {
final TestFile root
final TestFile buildCacheDir
TestResource ignoredResourceInDirectory
TestResource notIgnoredResourceInDirectory
TestResource ignoredResourceInJar
TestResource ignoredResourceInNestedJar
TestResource ignoredResourceInNestedInDirJar
TestResource notIgnoredResourceInJar
TestResource notIgnoredResourceInNestedJar
TestResource notIgnoredResourceInNestedInDirJar
ManifestResource jarManifest
TestResource jarManifestProperties
ManifestResource manifestInDirectory
PropertiesResource propertiesFileInDir
PropertiesResource propertiesFileInJar
PropertiesResource propertiesFileInNestedJar
PropertiesResource propertiesFileInNestedInDirJar
TestFile libraryJar
TestFile nestedJar
TestFile nestedInDirJar
private TestFile libraryJarContents
private TestFile nestedJarContents
private TestFile nestedInDirJarContents
private final String projectName
final TestFile buildFile
final TestFile settingsFile
ProjectWithRuntimeClasspathNormalization(String projectName = null, Api api) {
this.projectName = projectName
this.root = projectName ? file(projectName) : temporaryFolder.testDirectory
this.buildCacheDir = testDirectory.file("build-cache")
def buildCachePath = TextUtil.normaliseFileSeparators(buildCacheDir.absolutePath)
settingsFile = root.file('settings.gradle') << """
buildCache {
local {
directory = file('${buildCachePath}')
}
}
"""
buildFile = root.file('build.gradle') << """
apply plugin: 'base'
"""
buildFile << declareCustomTask(api)
nestedInDirJarContents = root.file('nestedInDirJarContents').create {
ignoredResourceInNestedInDirJar = new TestResource(file('another/package/ignored.txt') << "This should be ignored", this.&createNestedInDirJar)
notIgnoredResourceInNestedInDirJar = new TestResource(file('another/package/not-ignored.txt') << "This should not be ignored", this.&createNestedInDirJar)
propertiesFileInNestedInDirJar = new PropertiesResource(file('some/path/to/foo.properties'), [(IGNORE_ME): 'this should be ignored', (DONT_IGNORE_ME): 'this should not be ignored'], this.&createNestedInDirJar)
}
root.file('classpath/dirEntry').create {
ignoredResourceInDirectory = new TestResource(file("ignored.txt") << "This should be ignored")
notIgnoredResourceInDirectory = new TestResource(file("not-ignored.txt") << "This should not be ignored")
nestedInDirJar = file('nestedInDir.jar')
propertiesFileInDir = new PropertiesResource(file('some/path/to/foo.properties'), [(IGNORE_ME): 'this should be ignored', (DONT_IGNORE_ME): 'this should not be ignored'])
manifestInDirectory = new ManifestResource(file('META-INF/MANIFEST.MF')).withAttributes((IMPLEMENTATION_VERSION): "1.0.0")
}
nestedJarContents = root.file('libraryContents').create {
ignoredResourceInNestedJar = new TestResource(file('some/package/ignored.txt') << "This should be ignored", this.&createJar)
notIgnoredResourceInNestedJar = new TestResource(file('some/package/not-ignored.txt') << "This should not be ignored", this.&createJar)
propertiesFileInNestedJar = new PropertiesResource(file('some/path/to/foo.properties'), [(IGNORE_ME): 'this should be ignored', (DONT_IGNORE_ME): 'this should not be ignored'], this.&createJar)
}
libraryJarContents = root.file('libraryContents').create {
jarManifest = new ManifestResource(file('META-INF/MANIFEST.MF'), this.&createJar).withAttributes((IMPLEMENTATION_VERSION): "1.0.0")
jarManifestProperties = new TestResource(file('META-INF/build-info.properties') << "implementation-version=1.0.0", this.&createJar)
ignoredResourceInJar = new TestResource(file('some/package/ignored.txt') << "This should be ignored", this.&createJar)
notIgnoredResourceInJar = new TestResource(file('some/package/not-ignored.txt') << "This should not be ignored", this.&createJar)
nestedJar = file('nested.jar')
propertiesFileInJar = new PropertiesResource(file('some/path/to/foo.properties'), [(IGNORE_ME): 'this should be ignored', (DONT_IGNORE_ME): 'this should not be ignored'], this.&createJar)
}
libraryJar = root.file('library.jar')
createJar()
createNestedInDirJar()
}
String declareCustomTask(Api api) {
if (api == Api.RUNTIME) {
return """
task customTask {
def outputFile = file("\$temporaryDir/output.txt")
inputs.files("classpath/dirEntry", "library.jar")
.withPropertyName("classpath")
.withNormalizer(ClasspathNormalizer)
outputs.file(outputFile)
.withPropertyName("outputFile")
outputs.cacheIf { true }
doLast {
outputFile.text = "done"
}
}
"""
} else {
return """
@CacheableTask
class CustomTask extends DefaultTask {
@OutputFile File outputFile = new File(temporaryDir, "output.txt")
@Classpath FileCollection classpath = project.layout.files("classpath/dirEntry", "library.jar")
@TaskAction void generate() {
outputFile.text = "done"
}
}
task customTask(type: CustomTask)
"""
}
}
void createJar() {
if (nestedJar.exists()) {
nestedJar.delete()
}
nestedJarContents.zipTo(nestedJar)
if (libraryJar.exists()) {
libraryJar.delete()
}
libraryJarContents.zipTo(libraryJar)
}
void createNestedInDirJar() {
if (nestedInDirJar.exists()) {
nestedInDirJar.delete()
}
nestedInDirJarContents.zipTo(nestedInDirJar)
}
ProjectWithRuntimeClasspathNormalization withFilesIgnored() {
root.file('build.gradle') << """
normalization {
runtimeClasspath {
ignore "**/ignored.txt"
}
}
""".stripIndent()
return this
}
ProjectWithRuntimeClasspathNormalization withAllMetaInfIgnored() {
root.file('build.gradle') << """
normalization {
runtimeClasspath {
metaInf {
ignoreCompletely()
}
}
}
""".stripIndent()
return this
}
ProjectWithRuntimeClasspathNormalization withManifestIgnored() {
root.file('build.gradle') << """
normalization {
runtimeClasspath {
metaInf {
ignoreManifest()
}
}
}
""".stripIndent()
return this
}
ProjectWithRuntimeClasspathNormalization withManifestAttributesIgnored() {
root.file('build.gradle') << """
normalization {
runtimeClasspath {
metaInf {
ignoreAttribute "${IMPLEMENTATION_VERSION}"
}
}
}
""".stripIndent()
return this
}
ProjectWithRuntimeClasspathNormalization withManifestPropertiesIgnored() {
root.file('build.gradle') << """
normalization {
runtimeClasspath {
metaInf {
ignoreProperty "implementation-version"
}
}
}
""".stripIndent()
return this
}
ProjectWithRuntimeClasspathNormalization withPropertiesIgnored() {
root.file('build.gradle') << """
normalization {
runtimeClasspath {
properties {
ignoreProperty "${IGNORE_ME}"
}
}
}
""".stripIndent()
return this
}
String getCustomTask() {
return "${projectName ? ":${projectName}" : ''}:customTask"
}
}
class TestResource {
final TestFile backingFile
private final Closure onChange
TestResource(TestFile backingFile, Closure onChange = {}) {
this.backingFile = backingFile
this.onChange = onChange
}
void replaceContents(String contents) {
backingFile.withWriter { w ->
w << contents
}
changed()
}
void changeContents() {
backingFile << "More changes"
changed()
}
void remove() {
assert backingFile.delete()
changed()
}
void add() {
backingFile << "First creation of file"
changed()
}
void changed() {
onChange.call()
}
}
class ManifestResource extends TestResource {
Map<String, String> attributes
ManifestResource(TestFile backingFile, Closure onChange = {}) {
super(backingFile, onChange)
}
ManifestResource withAttributes(Map<String, String> attributes) {
this.attributes = attributes
def manifest = new Manifest()
def mainAttributes = manifest.getMainAttributes()
mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0")
attributes.each {name, value ->
mainAttributes.put(new Attributes.Name(name), value)
}
backingFile.withOutputStream {os ->
manifest.write(os)
}
return this
}
ManifestResource changeAttributes(Map<String, String> attributes) {
withAttributes(attributes)
changed()
return this
}
@Override
void changeContents() {
throw new UnsupportedOperationException()
}
@Override
void add() {
throw new UnsupportedOperationException()
}
}
class PropertiesResource extends TestResource {
String comment = ""
PropertiesResource(TestFile backingFile, Map<String, String> initialProps, Closure finalizedBy={}) {
super(backingFile, finalizedBy)
withProperties() { Properties props ->
props.putAll(initialProps)
}
}
PropertiesResource changeProperty(String key, String value) {
withProperties(true) { Properties props ->
props.setProperty(key, value)
}
changed()
return this
}
PropertiesResource setComment(String comment) {
this.comment = comment
return this
}
PropertiesResource setContent(String content) {
// Preserve the order of the properties in the map when writing the properties file
backingFile.withWriter {writer ->
writer.write("# ${comment}\n")
writer.write(content)
}
changed()
return this
}
private withProperties(boolean loadFromExisting = false, Closure action) {
Properties props = new Properties()
if (loadFromExisting) {
def inputStream = backingFile.newInputStream()
try {
props.load(inputStream)
} finally {
inputStream.close()
}
}
action.call(props)
def outputStream = backingFile.newOutputStream()
try {
props.store(outputStream, comment)
} finally {
outputStream.close()
}
}
}
}