Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a specific dependency from springBoots `bootJar` gradle task

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()
}
like image 259
Hemeroc Avatar asked Jan 27 '23 20:01

Hemeroc


2 Answers

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 =)

like image 180
Hemeroc Avatar answered Feb 05 '23 15:02

Hemeroc


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.

like image 44
lucniner Avatar answered Feb 05 '23 14:02

lucniner