Exclude a specific dependency from springBoots `bootJar` gradle task

此生再无相见时 提交于 2020-12-06 02:39:51

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/51048644/exclude-a-specific-dependency-from-springboots-bootjar-gradle-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!