Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up core's build file #4207

Merged
merged 11 commits into from
Sep 23, 2021
33 changes: 1 addition & 32 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ subprojects {
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'io.franzbecker.gradle-lombok'
apply plugin: 'com.github.johnrengelman.shadow'
apply from: "$rootDir/gradle/shading.gradle"

group = "org.testcontainers"

Expand Down Expand Up @@ -99,37 +99,6 @@ subprojects {
source = delombok.outputs
}

shadowJar {
configurations = []
archiveClassifier.set(null)

doFirst {
// See https://github.com/johnrengelman/shadow/blob/5.0.0/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy
Set<String> packages = []
// Always read from core's configurations
for (configuration in tasks.getByPath(":testcontainers:shadowJar").configurations) {
for (jar in configuration.files) {
def jf = new java.util.jar.JarFile(jar)
for (entry in jf.entries()) {
def name = entry.name
if (name.endsWith(".class")) {
def index = name.lastIndexOf('/')
if (index != -1) {
packages.add(name.substring(0, index))
}
}
}
jf.close()
}
}
for (pkg in packages) {
pkg = pkg.replaceAll('/', '.')

tasks.shadowJar.relocate(pkg, "org.testcontainers.shaded.${pkg}")
}
}
}

dependencies {
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
}
Expand Down
46 changes: 11 additions & 35 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ idea.module.testSourceDirs += sourceSets.jarFileTest.allSource.srcDirs

configurations {
baseline
shaded
[api, compileOnly, testCompile]*.extendsFrom shaded
}

shadowJar {
Expand Down Expand Up @@ -72,12 +70,6 @@ task japicmp(type: me.champeau.gradle.japicmp.JapicmpTask) {
"org.testcontainers.shaded.*",
]

classExcludes = []

methodExcludes = []

fieldExcludes = []

onlyBinaryIncompatibleModified = true
htmlOutputFile = file("$buildDir/reports/japi.html")
}
Expand All @@ -99,41 +91,25 @@ dependencies {
exclude group: "*", module: "*"
}

compileOnly 'org.jetbrains:annotations:21.0.1'

api 'junit:junit:4.12'
api 'org.slf4j:slf4j-api:1.7.30'
compileOnly 'org.jetbrains:annotations:21.0.1'
testCompileClasspath 'org.jetbrains:annotations:21.0.1'
api 'org.apache.commons:commons-compress:1.20'
api ('org.rnorth.duct-tape:duct-tape:1.0.8') {
exclude(group: 'org.jetbrains', module: 'annotations')
}

api "com.github.docker-java:docker-java-api:3.2.11"

shaded ('com.github.docker-java:docker-java-core:3.2.11') {
exclude(group: 'com.github.docker-java', module: 'docker-java-api')
exclude(group: 'com.github.docker-java', module: 'docker-java-transport')
exclude(group: 'com.fasterxml.jackson.core', module: 'jackson-annotations')
exclude(group: 'com.google.code.findbug')
exclude(group: 'org.slf4j')
exclude(group: 'org.apache.commons', module: 'commons-compress')
}

shaded ('com.github.docker-java:docker-java-transport-okhttp:3.2.11') {
exclude(group: 'com.github.docker-java', module: 'docker-java-core')
exclude(group: 'net.java.dev.jna')
exclude(group: 'org.slf4j')
}

api 'org.rnorth.duct-tape:duct-tape:1.0.8'
api 'com.github.docker-java:docker-java-api:3.2.11'
api 'com.github.docker-java:docker-java-transport-zerodep:3.2.11'

shaded 'com.github.docker-java:docker-java-core:3.2.11'
shaded 'com.github.docker-java:docker-java-transport-okhttp:3.2.11', {
// TODO remove unused `net.java.dev.jna:jna-platform` from `docker-java-transport-okhttp`
exclude(group: 'net.java.dev.jna', module: 'jna-platform')
}
shaded "org.yaml:snakeyaml:1.29"

shaded 'org.glassfish.main.external:trilead-ssh2-repackaged:4.1.2'
shaded 'org.zeroturnaround:zt-exec:1.12'

shaded 'org.zeroturnaround:zt-exec:1.12', {
exclude(group: 'org.slf4j')
}
testCompileClasspath 'org.jetbrains:annotations:21.0.1'
Copy link
Contributor

@lanwen lanwen Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
testCompileClasspath 'org.jetbrains:annotations:21.0.1'
testCompileOnly 'org.jetbrains:annotations:21.0.1'

shouldn't be that with Only suffix? As *Classpath configurations are intended to be resolvable, but not consumable

https://discuss.gradle.org/t/what-is-a-configuration-which-cant-be-directly-resolved/30721

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was testCompileClasspath before, so I suggest we address it separately, as this PR is something massive already :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, didn't notice it was moved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to polish this PR and reduce the changeset while fixing the merge conflict 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


testImplementation 'org.apache.httpcomponents:httpclient:4.5.9'
testImplementation 'redis.clients:jedis:3.6.0'
Expand Down
61 changes: 61 additions & 0 deletions gradle/shading.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.util.jar.JarFile

apply plugin: 'com.github.johnrengelman.shadow'

configurations {
shaded
[apiElements, implementation, compileOnly, testCompile]*.extendsFrom shaded
}
configurations.api.canBeResolved = true

shadowJar {
configurations = [project.configurations.shaded]
archiveClassifier.set(null)

mergeServiceFiles()
}

project.afterEvaluate {
def apiDependencies = project.configurations.api.resolvedConfiguration.resolvedArtifacts*.moduleVersion*.id
def apiFiles = project.configurations.api.resolvedConfiguration.files

// Exclude `api` dependencies, to prevent them being included into the final JAR
shadowJar.dependencies {
for (id in apiDependencies) {
exclude(dependency("${id.group}:${id.name}"))
}
}

// Inherit dependencies' relocators
shadowJar.relocators = configurations.api.dependencies.withType(ProjectDependency).collectMany {
return it.dependencyProject.tasks.findByName("shadowJar")?.relocators ?: []
}

// See https://github.com/johnrengelman/shadow/blob/5.0.0/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ConfigureShadowRelocation.groovy
Set<String> packages = []
// Always read from core's configurations
for (configuration in tasks.shadowJar.configurations) {
for (jar in configuration.resolvedConfiguration.files) {
if (apiFiles.contains(jar)) {
continue
}

try (def jf = new JarFile(jar)) {
for (entry in jf.entries()) {
def name = entry.name
if (name.endsWith(".class")) {
def index = name.lastIndexOf('/')
if (index != -1) {
packages.add(name.substring(0, index))
}
}
}
}
}
}
for (pkg in packages) {
pkg = pkg.replaceAll('/', '.')

tasks.shadowJar.relocate(pkg, "org.testcontainers.shaded.${pkg}")
}
}