Exclude a specific dependency from springBoots `bootJar` gradle task
Asked Answered
W

2

5

I need to exclude a specific dependency from springBoots bootJar gradle task (similar to the provided scope in maven).

I tried a custom configuration, but the dependency-which-should-not-be-in-bootJar is still included in the resulting jar.

configurations{
    provided
    implementation.extendsFrom provided
}

dependencies {
    // ...
    provided "dependency-which-should-not-be-in-bootJar"
}

jar {
    from configurations.compile - configurations.provided
    from configurations.runtime
}

bootJar {
    from configurations.compile - configurations.provided
    from configurations.runtime
    launchScript()
}
Woodshed answered 26/6, 2018 at 17:36 Comment(2)
bootJar includes the runtime classpath by default. It inherits from implementation which you've configured to extend from provided As a result, the runtime classpath includes your provided dependency so it's include in the fat jar.Serrato
I also tried to change my provided to providedRuntime and let it extend from runtime after that I tried to configure the bootJar to override the runtime include with from configurations.runtime - configurations.providedRuntime which I thought should work but it doesn't. Can you point me to the right direction how to exclude a dependency properly from being packaged with the jar.Woodshed
P
3

You can actually use compileOnly for your dependency with gradle > 2.12

dependencies {
     // ...
     compileOnly "dependency-which-should-not-be-in-bootJar"
}

You will still have it for test + runtime, but not in the final built jar.

Polito answered 29/6, 2018 at 21:42 Comment(1)
In my case I also had to add testRuntime but otherwise it worked perfectly thx.Woodshed
W
6

I also got an answer from Andy Wilkinson in the spring boot gitter channel which works slightly different but manages to achieve similar.

configurations {
    custom
    runtime.extendsFrom custom
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    custom 'com.h2database:h2'
}

bootJar {
    exclude {
        configurations.custom.resolvedConfiguration.files.contains(it.file)
    }
}

Thank you Andy =)

Woodshed answered 2/7, 2018 at 7:8 Comment(2)
It doesn't work for classpath folders: resolvedConfiguration.files still contains the folder java.io.File itself while it.file is a child of that folderJobey
This worked for me: configurations.custom.resolvedConfiguration.files.find { customEntry -> it.file.toPath().startsWith(customEntry.toPath()) } != nullJobey
P
3

You can actually use compileOnly for your dependency with gradle > 2.12

dependencies {
     // ...
     compileOnly "dependency-which-should-not-be-in-bootJar"
}

You will still have it for test + runtime, but not in the final built jar.

Polito answered 29/6, 2018 at 21:42 Comment(1)
In my case I also had to add testRuntime but otherwise it worked perfectly thx.Woodshed

© 2022 - 2024 — McMap. All rights reserved.