摇篮善良:仅添加包装用于战争
我的同事Tom Wetjens 在Maven中撰寫了博客文章僅打包依賴項(xiàng) 。 當(dāng)我們想在WAR文件中包含依賴項(xiàng)時,他展示了一種Maven解決方案,而在其他任何作用域中都沒有使用。 在這篇博客中,我們將看到我們?nèi)绾卧贕radle中解決這個問題。
假設(shè)我們在項(xiàng)目中使用SLF4J Logging API。 我們將API用作編譯依賴項(xiàng),因?yàn)槲覀兊拇a使用此API。 但是在測試運(yùn)行時中,我們想使用此API的SLF4J Simple實(shí)現(xiàn)。 并且在我們的WAR文件中,我們希望包括API的Logback實(shí)現(xiàn)。 Logback依賴關(guān)系僅需要包含在WAR文件中,并且不應(yīng)存在于任何其他依賴關(guān)系配置中。
我們首先將War插件添加到我們的項(xiàng)目中。 war任務(wù)使用runtime依賴項(xiàng)配置來確定將哪些文件添加到WAR文件中的WEB-INF/lib目錄中。 我們添加了新的依賴項(xiàng)配置warLib ,以擴(kuò)展項(xiàng)目中的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 }現(xiàn)在,我們可以運(yùn)行build任務(wù),并獲得包含以下內(nèi)容的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同樣,當(dāng)我們運(yùn)行dependencies任務(wù)時,我們可以看到SLF4J API的實(shí)現(xiàn)與依賴項(xiàng)配置之間的關(guān)系:
$ 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
總結(jié)
以上是生活随笔為你收集整理的摇篮善良:仅添加包装用于战争的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕b350超频设置(华硕b350m超频
- 下一篇: 可怕的DefaultAbstractHe