Gradle善良:仅添加包装用于战争
我的同事Tom Wetjens 在Maven中撰寫了博客文章僅打包依賴項 。 當我們想在WAR文件中包含依賴項時,他展示了一種Maven解決方案,而在其他任何作用域中都沒有使用。 在這篇博客中,我們將看到我們如何在Gradle中解決這個問題。
假設我們在項目中使用SLF4J Logging API。 我們將API用作編譯依賴項,因為我們的代碼使用此API。 但是在我們的測試運行時中,我們想使用此API的SLF4J Simple實現。 并且在我們的WAR文件中,我們希望包括API的Logback實現。 Logback依賴關系僅需要包含在WAR文件中,并且不應存在于任何其他依賴關系配置中。
我們首先將War插件添加到我們的項目中。 war任務使用runtime依賴項配置來確定將哪些文件添加到WAR文件中的WEB-INF/lib目錄中。 我們添加了新的依賴項配置warLib ,以擴展項目中的runtime配置。
apply plugin: 'war'repositories.jcenter()configurations {// Create new dependency configuration// for dependencies to be added in // WAR file.warLib.extendsFrom runtime }dependencies {// API dependency for Slf4j.compile 'org.slf4j:slf4j-api:1.7.7'testCompile 'junit:junit:4.11'// Slf4j implementation used for tests.testRuntime 'org.slf4j:slf4j-simple:1.7.7'// Slf4j implementation to be packaged// in WAR file.warLib 'ch.qos.logback:logback-classic:1.1.2' }war {// Add warLib dependency configurationclasspath configurations.warLib// We remove all duplicate files// with this assignment.// geFiles() method return a unique// set of File objects, removing// any duplicates from configurations// added by classpath() method.classpath = classpath.files }現在,我們可以運行build任務,并獲得包含以下內容的WAR文件:
$ gradle build :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :war :assemble :compileTestJava :processTestResources UP-TO-DATE :testClasses :test :check :buildBUILD SUCCESSFULTotal time: 6.18 secs $ jar tvf build/libs/package-only-dep-example.war0 Fri Sep 19 05:59:54 CEST 2014 META-INF/25 Fri Sep 19 05:59:54 CEST 2014 META-INF/MANIFEST.MF0 Fri Sep 19 05:59:54 CEST 2014 WEB-INF/0 Fri Sep 19 05:59:54 CEST 2014 WEB-INF/lib/29257 Thu Sep 18 14:36:24 CEST 2014 WEB-INF/lib/slf4j-api-1.7.7.jar 270750 Thu Sep 18 14:36:24 CEST 2014 WEB-INF/lib/logback-classic-1.1.2.jar 427729 Thu Sep 18 14:36:26 CEST 2014 WEB-INF/lib/logback-core-1.1.2.jar115 Wed Sep 03 09:24:40 CEST 2014 WEB-INF/web.xml同樣,當我們運行dependencies任務時,我們可以看到SLF4J API的實現與依賴項配置之間的關系:
$ gradle dependencies :dependencies------------------------------------------------------------ Root project ------------------------------------------------------------archives - Configuration for archive artifacts. No dependenciescompile - Compile classpath for source set 'main'. \--- org.slf4j:slf4j-api:1.7.7default - Configuration for default artifacts. \--- org.slf4j:slf4j-api:1.7.7providedCompile - Additional compile classpath for libraries that should not be part of the WAR archive. No dependenciesprovidedRuntime - Additional runtime classpath for libraries that should not be part of the WAR archive. No dependenciesruntime - Runtime classpath for source set 'main'. \--- org.slf4j:slf4j-api:1.7.7testCompile - Compile classpath for source set 'test'. +--- org.slf4j:slf4j-api:1.7.7 \--- junit:junit:4.11\--- org.hamcrest:hamcrest-core:1.3testRuntime - Runtime classpath for source set 'test'. +--- org.slf4j:slf4j-api:1.7.7 +--- junit:junit:4.11 | \--- org.hamcrest:hamcrest-core:1.3 \--- org.slf4j:slf4j-simple:1.7.7\--- org.slf4j:slf4j-api:1.7.7warLib +--- org.slf4j:slf4j-api:1.7.7 \--- ch.qos.logback:logback-classic:1.1.2+--- ch.qos.logback:logback-core:1.1.2\--- org.slf4j:slf4j-api:1.7.6 -> 1.7.7(*) - dependencies omitted (listed previously)BUILD SUCCESSFULTotal time: 6.274 secs用Gradle 2.1編寫的代碼。
翻譯自: https://www.javacodegeeks.com/2014/09/gradle-goodness-adding-dependencies-only-for-packaging-to-war.html
總結
以上是生活随笔為你收集整理的Gradle善良:仅添加包装用于战争的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux开启的端口(linux 开启的
- 下一篇: 安卓接口回调例子(安卓接口回调)