-
Notifications
You must be signed in to change notification settings - Fork 407
/
Copy pathRelocationSpec.groovy
325 lines (274 loc) · 9.86 KB
/
RelocationSpec.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
package com.github.jengelman.gradle.plugins.shadow
import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import spock.lang.Ignore
import spock.lang.Issue
import java.util.jar.Attributes
import java.util.jar.JarFile
class RelocationSpec extends PluginSpecification {
@Issue('SHADOW-58')
def "relocate dependency files"() {
given:
buildFile << """
dependencies {
implementation 'junit:junit:3.8.2'
}
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
relocate 'junit.textui', 'a'
relocate 'junit.framework', 'b'
manifest {
attributes 'TEST-VALUE': 'FOO'
}
}
""".stripIndent()
when:
run('shadowJar')
then:
contains(output, [
'META-INF/MANIFEST.MF',
'a/ResultPrinter.class',
'a/TestRunner.class',
'b/Assert.class',
'b/AssertionFailedError.class',
'b/ComparisonCompactor.class',
'b/ComparisonFailure.class',
'b/Protectable.class',
'b/Test.class',
'b/TestCase.class',
'b/TestFailure.class',
'b/TestListener.class',
'b/TestResult$1.class',
'b/TestResult.class',
'b/TestSuite$1.class',
'b/TestSuite.class'
])
and:
doesNotContain(output, [
'junit/textui/ResultPrinter.class',
'junit/textui/TestRunner.class',
'junit/framework/Assert.class',
'junit/framework/AssertionFailedError.class',
'junit/framework/ComparisonCompactor.class',
'junit/framework/ComparisonFailure.class',
'junit/framework/Protectable.class',
'junit/framework/Test.class',
'junit/framework/TestCase.class',
'junit/framework/TestFailure.class',
'junit/framework/TestListener.class',
'junit/framework/TestResult$1.class',
'junit/framework/TestResult.class',
'junit/framework/TestSuite$1.class',
'junit/framework/TestSuite.class'
])
and: 'Test that manifest file exists with contents'
JarFile jar = new JarFile(output)
Attributes attributes = jar.manifest.getMainAttributes()
String val = attributes.getValue('TEST-VALUE')
assert val == 'FOO'
}
def "relocate dependency files with filtering"() {
given:
buildFile << """
dependencies {
implementation 'junit:junit:3.8.2'
}
// tag::relocateFilter[]
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
relocate('junit.textui', 'a') {
exclude 'junit.textui.TestRunner'
}
relocate('junit.framework', 'b') {
include 'junit.framework.Test*'
}
}
// end::relocateFilter[]
""".stripIndent()
when:
run('shadowJar')
then:
contains(output, [
'a/ResultPrinter.class',
'b/Test.class',
'b/TestCase.class',
'b/TestFailure.class',
'b/TestListener.class',
'b/TestResult$1.class',
'b/TestResult.class',
'b/TestSuite$1.class',
'b/TestSuite.class'
])
and:
doesNotContain(output, [
'a/TestRunner.class',
'b/Assert.class',
'b/AssertionFailedError.class',
'b/ComparisonCompactor.class',
'b/ComparisonFailure.class',
'b/Protectable.class'
])
and:
contains(output, [
'junit/textui/TestRunner.class',
'junit/framework/Assert.class',
'junit/framework/AssertionFailedError.class',
'junit/framework/ComparisonCompactor.class',
'junit/framework/ComparisonFailure.class',
'junit/framework/Protectable.class'
])
}
@Issue(['SHADOW-55', 'SHADOW-53'])
def "remap class names for relocated files in project source"() {
given:
buildFile << """
dependencies {
implementation 'junit:junit:3.8.2'
}
// tag::relocate[]
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
relocate 'junit.framework', 'shadow.junit'
}
// end::relocate[]
""".stripIndent()
file('src/main/java/shadow/ShadowTest.java') << '''
package shadow;
import junit.framework.Test;
import junit.framework.TestResult;
public class ShadowTest implements Test {
public int countTestCases() { return 0; }
public void run(TestResult result) { }
}
'''.stripIndent()
when:
run('shadowJar')
then:
contains(output, [
'shadow/ShadowTest.class',
'shadow/junit/Test.class',
'shadow/junit'
])
and:
doesNotContain(output, [
'junit/framework',
'junit/framework/Test.class'
])
and: 'check that the class can be loaded. If the file was not relocated properly, we should get a NoDefClassFound'
// Isolated class loader with only the JVM system jars and the output jar from the test project
URLClassLoader classLoader = new URLClassLoader([output.toURI().toURL()] as URL[],
ClassLoader.systemClassLoader.parent)
classLoader.loadClass('shadow.ShadowTest')
}
@Issue('SHADOW-61')
def "relocate does not drop dependency resources"() {
given: 'Core project with dependency and resource'
file('core/build.gradle') << """
apply plugin: 'java-library'
repositories { maven { url "${repo.uri}" } }
dependencies { api 'junit:junit:3.8.2' }
""".stripIndent()
file('core/src/main/resources/TEST') << 'TEST RESOURCE'
file('core/src/main/resources/test.properties') << 'name=test'
file('core/src/main/java/core/Core.java') << '''
package core;
import junit.framework.Test;
public class Core {}
'''.stripIndent()
and: 'App project with shadow, relocation, and project dependency'
file('app/build.gradle') << """
apply plugin: 'java'
apply plugin: 'com.gradleup.shadow'
repositories { maven { url "${repo.uri}" } }
dependencies { implementation project(':core') }
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
relocate 'core', 'app.core'
relocate 'junit.framework', 'app.junit.framework'
}
""".stripIndent()
file('app/src/main/resources/APP-TEST') << 'APP TEST RESOURCE'
file('app/src/main/java/app/App.java') << '''
package app;
import core.Core;
import junit.framework.Test;
public class App {}
'''.stripIndent()
and: 'Configure multi-project build'
settingsFile << '''
include 'core', 'app'
'''.stripIndent()
when:
run(':app:shadowJar')
then:
File appOutput = getFile('app/build/libs/app-all.jar')
assert appOutput.exists()
and:
contains(appOutput, [
'TEST',
'APP-TEST',
'test.properties',
'app/core/Core.class',
'app/App.class',
'app/junit/framework/Test.class'
])
}
@Issue(['SHADOW-93', 'SHADOW-114'])
def "relocate resource files"() {
given:
repo.module('shadow', 'dep', '1.0')
.insertFile('foo/dep.properties', 'c')
.publish()
file('src/main/java/foo/Foo.java') << '''
package foo;
class Foo {}
'''.stripIndent()
file('src/main/resources/foo/foo.properties') << 'name=foo'
buildFile << """
dependencies {
implementation 'shadow:dep:1.0'
}
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
relocate 'foo', 'bar'
}
""".stripIndent()
when:
run('shadowJar')
then:
contains(output, [
'bar/Foo.class',
'bar/foo.properties',
'bar/dep.properties'
])
and:
doesNotContain(output, [
'foo/Foo.class',
'foo/foo.properties',
'foo/dep.properties'
])
}
@Issue("SHADOW-294")
@Ignore
def "does not error on relocating java9 classes"() {
given:
buildFile << """
repositories {
mavenCentral()
maven {
url 'https://repository.mapr.com/nexus/content/groups/mapr-public/releases'
}
}
dependencies {
implementation 'org.slf4j:slf4j-api:1.7.21'
implementation group: 'io.netty', name: 'netty-all', version: '4.0.23.Final'
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '2.5.0'
implementation group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.6'
implementation group: 'org.hbase', name: 'asynchbase', version: '1.7.0-mapr-1603'
}
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
zip64 true
relocate 'com.google.protobuf', 'shaded.com.google.protobuf'
relocate 'io.netty', 'shaded.io.netty'
}
""".stripIndent()
when:
run('shadowJar')
then:
noExceptionThrown()
}
}