Hemeroc
Hemeroc

Reputation: 2244

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()
}

Upvotes: 5

Views: 6872

Answers (2)

Hemeroc
Hemeroc

Reputation: 2244

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

Upvotes: 6

lucniner
lucniner

Reputation: 102

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.

Upvotes: 3

Related Questions