Skip to content

Commit d512ca5

Browse files
authored
Tidy up core's build file (#4207)
* Tidy up core's build file * post-merge fixes * Extract shading config * fix modules * fix tests * fix merge conflicts, simplify shading.gradle * cleanup * Workaround BOM dependency * Use `resolvedConfiguration`
1 parent 8712457 commit d512ca5

File tree

4 files changed

+87
-71
lines changed

4 files changed

+87
-71
lines changed

build.gradle

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ subprojects {
2121
apply plugin: 'java-library'
2222
apply plugin: 'idea'
2323
apply plugin: 'io.franzbecker.gradle-lombok'
24-
apply plugin: 'com.github.johnrengelman.shadow'
24+
apply from: "$rootDir/gradle/shading.gradle"
2525

2626
group = "org.testcontainers"
2727

@@ -101,37 +101,6 @@ subprojects {
101101
source = delombok.outputs
102102
}
103103

104-
shadowJar {
105-
configurations = []
106-
archiveClassifier.set(null)
107-
108-
doFirst {
109-
// See https://github.com/johnrengelman/shadow/blob/5.0.0/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy
110-
Set<String> packages = []
111-
// Always read from core's configurations
112-
for (configuration in tasks.getByPath(":testcontainers:shadowJar").configurations) {
113-
for (jar in configuration.files) {
114-
def jf = new java.util.jar.JarFile(jar)
115-
for (entry in jf.entries()) {
116-
def name = entry.name
117-
if (name.endsWith(".class")) {
118-
def index = name.lastIndexOf('/')
119-
if (index != -1) {
120-
packages.add(name.substring(0, index))
121-
}
122-
}
123-
}
124-
jf.close()
125-
}
126-
}
127-
for (pkg in packages) {
128-
pkg = pkg.replaceAll('/', '.')
129-
130-
tasks.shadowJar.relocate(pkg, "org.testcontainers.shaded.${pkg}")
131-
}
132-
}
133-
}
134-
135104
dependencies {
136105
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
137106
}

core/build.gradle

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ sourceSets {
88

99
idea.module.testSourceDirs += sourceSets.jarFileTest.allSource.srcDirs
1010

11-
configurations {
12-
shaded
13-
[api, compileOnly, testCompile]*.extendsFrom shaded
14-
}
15-
1611
shadowJar {
17-
configurations = [project.configurations.shaded]
18-
19-
mergeServiceFiles()
20-
2112
[
2213
'META-INF/NOTICE',
2314
'META-INF/NOTICE.txt',
@@ -77,23 +68,16 @@ dependencies {
7768

7869
shaded 'org.awaitility:awaitility:4.1.0'
7970

71+
api platform('com.github.docker-java:docker-java-bom:3.2.12')
8072
shaded platform('com.github.docker-java:docker-java-bom:3.2.12')
8173

8274
api "com.github.docker-java:docker-java-api"
8375

84-
shaded ('com.github.docker-java:docker-java-core') {
85-
exclude(group: 'com.github.docker-java', module: 'docker-java-api')
86-
exclude(group: 'com.github.docker-java', module: 'docker-java-transport')
87-
exclude(group: 'com.fasterxml.jackson.core', module: 'jackson-annotations')
88-
exclude(group: 'com.google.code.findbug')
89-
exclude(group: 'org.slf4j')
90-
exclude(group: 'org.apache.commons', module: 'commons-compress')
91-
}
76+
shaded 'com.github.docker-java:docker-java-core'
9277

93-
shaded ('com.github.docker-java:docker-java-transport-okhttp') {
94-
exclude(group: 'com.github.docker-java', module: 'docker-java-core')
95-
exclude(group: 'net.java.dev.jna')
96-
exclude(group: 'org.slf4j')
78+
shaded 'com.github.docker-java:docker-java-transport-okhttp', {
79+
// TODO remove unused `net.java.dev.jna:jna-platform` from `docker-java-transport-okhttp`
80+
exclude(group: 'net.java.dev.jna', module: 'jna-platform')
9781
}
9882

9983
api 'com.github.docker-java:docker-java-transport-zerodep'
@@ -102,9 +86,7 @@ dependencies {
10286

10387
shaded 'org.glassfish.main.external:trilead-ssh2-repackaged:4.1.2'
10488

105-
shaded 'org.zeroturnaround:zt-exec:1.12', {
106-
exclude(group: 'org.slf4j')
107-
}
89+
shaded 'org.zeroturnaround:zt-exec:1.12'
10890

10991
testImplementation 'org.apache.httpcomponents:httpclient:4.5.9'
11092
testImplementation 'redis.clients:jedis:3.7.0'

gradle/publishing.gradle

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,34 @@ publishing {
5757

5858
def dependenciesNode = rootNode.appendNode('dependencies')
5959

60-
def addDependency = { dependency, scope ->
61-
dependenciesNode.appendNode('dependency').with {
62-
appendNode('groupId', dependency.group)
63-
appendNode('artifactId', dependency.name)
64-
appendNode('version', dependency.version)
65-
appendNode('scope', scope)
60+
def addDependencies = { Configuration configuration, scope ->
61+
for (dependency in configuration.resolvedConfiguration.firstLevelModuleDependencies) {
62+
if (dependency.configuration.startsWith("platform-")) {
63+
continue
64+
}
65+
dependenciesNode.appendNode('dependency').with {
66+
if (!dependency.moduleGroup || !dependency.moduleName || !dependency.moduleVersion) {
67+
throw new IllegalStateException("Wrong dependency: $dependency")
68+
}
69+
appendNode('groupId', dependency.moduleGroup)
70+
appendNode('artifactId', dependency.moduleName)
71+
appendNode('version', dependency.moduleVersion)
72+
appendNode('scope', scope)
6673

67-
if (dependency instanceof ModuleDependency && !dependency.excludeRules.empty) {
68-
def excludesNode = appendNode('exclusions')
69-
for (rule in dependency.excludeRules) {
70-
excludesNode.appendNode('exclusion').with {
71-
appendNode('groupId', rule.group)
72-
appendNode('artifactId', rule.module)
74+
if (dependency instanceof ModuleDependency && !dependency.excludeRules.empty) {
75+
def excludesNode = appendNode('exclusions')
76+
for (rule in dependency.excludeRules) {
77+
excludesNode.appendNode('exclusion').with {
78+
appendNode('groupId', rule.group)
79+
appendNode('artifactId', rule.module)
80+
}
7381
}
7482
}
7583
}
7684
}
7785
}
78-
79-
project.configurations.api.dependencies.each { addDependency(it, 'compile') }
80-
project.configurations.provided.dependencies.each { addDependency(it, 'provided') }
86+
addDependencies(project.configurations.api, 'compile')
87+
addDependencies(project.configurations.provided, 'provided')
8188
}
8289
}
8390
}

gradle/shading.gradle

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.jar.JarFile
2+
3+
apply plugin: 'com.github.johnrengelman.shadow'
4+
5+
configurations {
6+
shaded
7+
[apiElements, implementation, compileOnly, testCompile]*.extendsFrom shaded
8+
}
9+
configurations.api.canBeResolved = true
10+
11+
shadowJar {
12+
configurations = [project.configurations.shaded]
13+
archiveClassifier.set(null)
14+
15+
mergeServiceFiles()
16+
}
17+
18+
project.afterEvaluate {
19+
Set<ModuleIdentifier> apiDependencies = project.configurations.api.resolvedConfiguration.resolvedArtifacts*.moduleVersion*.id*.module
20+
21+
// Exclude `api` dependencies, to prevent them being included into the final JAR
22+
shadowJar.dependencies {
23+
for (id in apiDependencies) {
24+
exclude(dependency("${id.group}:${id.name}"))
25+
}
26+
}
27+
28+
// Inherit dependencies' relocators
29+
shadowJar.relocators = configurations.api.dependencies.withType(ProjectDependency).collectMany {
30+
return it.dependencyProject.tasks.findByName("shadowJar")?.relocators ?: []
31+
}
32+
33+
// See https://github.com/johnrengelman/shadow/blob/5.0.0/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy
34+
Set<String> packages = []
35+
36+
for (artifact in project.configurations.shaded.resolvedConfiguration.resolvedArtifacts) {
37+
if (apiDependencies.contains(artifact.moduleVersion.id.module)) {
38+
continue
39+
}
40+
41+
try (def jf = new JarFile(artifact.file)) {
42+
for (entry in jf.entries()) {
43+
def name = entry.name
44+
if (name.endsWith(".class")) {
45+
def index = name.lastIndexOf('/')
46+
if (index != -1) {
47+
packages.add(name.substring(0, index))
48+
}
49+
}
50+
}
51+
}
52+
}
53+
for (pkg in packages) {
54+
pkg = pkg.replaceAll('/', '.')
55+
56+
tasks.shadowJar.relocate(pkg, "org.testcontainers.shaded.${pkg}")
57+
}
58+
}

0 commit comments

Comments
 (0)