【发布时间】:2015-03-27 09:05:42
【问题描述】:
我是一个非常新的 Gradle。几天前我才开始看它,我真的很喜欢它,所以我正在尝试将我的项目转换为 Gradle。
我刚刚遇到了我的第一个难题。我不知道如何防止“战争”插件复制很多已经在我的 EAR 中找到的瞬态依赖项。例如,假设我想在我的 WAR 中使用 org.springframework:spring-web。这个 jar 有很多临时依赖项,它们都被打包在 WEB-INF/lib 中。这是一个问题,b/c 所有这些依赖项都已经在我的EAR 的APP-INF/lib 中,b/c 还有其他模块在使用它们。
我想做的是告诉 Gradle,我希望像往常一样编译带有所有依赖项的 WAR 代码,但是在打包 WAR 时,我想对发生的事情进行一些控制进入WEB-INF/lib。据我所知,这个功能不存在。还是我错过了什么?
我现在已经通过 hack 解决了这个问题,但我觉得必须有一个更简单的方法来做到这一点。我希望有更多经验的人能指出我在这里出错的地方。
这是我目前所拥有的:
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
}
【问题讨论】:
-
您好,您解决了吗?我正在寻找解决方案。谢谢
-
@patb23:检查我的答案 - 它有解决方案。
标签: gradle dependencies war ear