[英]Using Gradle to package dependencies in a WAR, while leaving transient dependencies in the EAR
我是新來的Gradle。 我只是幾天前才開始查看它,我真的很喜歡它,所以我試圖將我的項目轉換為Gradle。
我剛遇到第一個難題。 我無法弄清楚如何防止'war'插件復制EAR中已經發現的許多瞬時依賴項。 例如,假設我要在WAR中使用org.springframework:spring-web
。 這個jar有很多瞬態依賴項,它們都打包在WEB-INF/lib
。 這是一個問題,b / c所有這些依賴項已經在我的EAR的APP-INF/lib
中,b / c還有其他使用它們的模塊。
我想告訴Gradle我希望像往常一樣使用所有依賴項來編譯WAR代碼,但是在打包WAR時,我希望對進入WEB-INF/lib
。 據我所知,此功能尚不存在。 還是我錯過了什么?
我現在已經通過黑客解決了這個問題,但是我認為必須有一種更簡單的方法來做到這一點。 我希望有更多經驗的人可以指出我在這里出錯的地方。
這是我到目前為止的內容:
apply plugin: 'war'
configurations {
// Use this config to indicate that a module should be packaged inside the WAR w/o its transitive dependencies
//
packageWithoutDependencies
// Use this one when you want to package this module and all of its dependencies in the WAR
//
packageWithDependencies
compile.extendsFrom packageWithoutDependencies
compile.extendsFrom packageWithDependencies
}
dependencies {
packageWithDependencies "org.richfaces:richfaces-bom:$richFacesVersion"
packageWithDependencies "org.richfaces.ui:richfaces-components-ui:$richFacesVersion"
packageWithDependencies "org.richfaces.core:richfaces-core-impl:$richFacesVersion"
packageWithoutDependencies "org.springframework:spring-web:$springVersion"
packageWithoutDependencies "org.springframework.security:spring-security-config:$springSecurityVersion"
packageWithoutDependencies "org.springframework.security:spring-security-taglibs:$springSecurityVersion"
packageWithoutDependencies "org.springframework.security:spring-security-web:$springSecurityVersion"
}
// The approach here is to directly modify the classpath used by the 'war' task. This is necessary
// b/c we want to compile with all the dependencies resolved as per usual, yet when we deploy,
// we only want the jars that belong in the WAR, with most transitive dependencies for those already
// deployed as 3rd party jars in the EAR.
//
task modifyWarClasspath << {
// Get the names of all non-transitive modules we are packaging
//
println " ========= WAR Modules w/o dependencies ========= "
def firstLevelDeps = configurations.packageWithoutDependencies.resolvedConfiguration.firstLevelModuleDependencies
def nonTransientDepNames = firstLevelDeps.collect { it.moduleName }
println nonTransientDepNames
// Get the names of all direct file includes we are packaging
//
println "\n ========= WAR Files w/o dependencies ========= "
def directFileIncludes = configurations.packageWithoutDependencies
.getDependencies().findAll{it.hasProperty('source')}
.source.files*.name.flatten()
println directFileIncludes
// Get the names of all transitive modules we a re packaging
//
println "\n ========= WAR Modules with dependencies ========= "
def recursiveDeps = configurations.packageWithDependencies.resolvedConfiguration
.firstLevelModuleDependencies*.allModuleArtifacts.flatten()
def transientDepNames = recursiveDeps.collect { it.name }
println transientDepNames
// Look through the WAR classpath (runtime - providedCompile), and keep only those
// files that match a module name on either of the 3 lists we made above.
//
println "\n ========= Packaging jars in the WAR ========= "
war.classpath = war.classpath.findAll {file ->
((nonTransientDepNames + transientDepNames + directFileIncludes + 'main').find {directDepName ->
file.name.startsWith(directDepName)
} != null)
}
war.classpath*.name.sort().each { println it }
println "\n\n"
}
// Enable 'packageWithDependencies' and 'packageWithoutDependencies' configs
war {
dependsOn modifyWarClasspath
}
我現在有一個很好的簡單解決方案。 關鍵是利用“提供的”范圍,然后在我的WAR依賴項中使用它來引用我的EAR依賴項。 這樣,整個WAR類路徑中的類路徑可以從EAR依賴項中減去,因此只有Web依賴項被打包在WAR中。 漂亮又簡單。
這是如何做:
configurations {
provided {
dependencies.all { dep ->
configurations.default.exclude(group: dep.group, module: dep.name)
}
}
compile.extendsFrom provided
}
然后在您的WAR項目中,您可以執行以下操作:
dependencies {
provided project(':DFIPConfig')
provided project(':DFIPSAPBatch')
provided project(':DFIPDomain')
// etc...
compile project(':DryDockProjects:DryDockWebProjects:CommonWebWCAGJar')
compile project(':DryDockProjects:DryDockWebProjects:CommonParticipantWeb')
// etc...
// Other WAR dependencies as per usual
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.