Gradle War在WEB-INF / lib下添加了额外的jar文件

时间:2017-10-04 10:58:20

标签: gradle build.gradle war

我是Gradle的新手。使用Gradle我有多个项目构建文件。大多数项目构建文件都已正确配置并正常工作并生成正确的输出。我正在尝试将其中一个项目从ANT构建转换为Gradle构建。我能够编译Java文件并能够生成WAR存档。

我注意到生成的WAR文件有一件事是WEB-INF / lib目录中还包含其他JAR文件。当我从ANT构建系统构建项目时,只包含几个jar文件(大约10-12个),但是使用gradle它包含许多额外的jar文件(大约90-92)。我在目录结构中有jar文件,应该包括在内。

这是来自build.gradle文件的依赖块:

dependencies {
    compile fileTree(dir: 'lib', include: '*.jar')
}

我是gradle系统的新手。所以我可能错过了一些要照顾的要点,但我不知道他们是哪一个。任何人都可以指导我在多项目gradle构建系统中为依赖管理提供哪些要点?这个问题的解决方案是什么?

2 个答案:

答案 0 :(得分:0)

您可以将jar添加到WEB-INF / lib,将所需的文件夹添加到war插件配置中:

war {
 classpath fileTree('desiredFolder') 
}

摘自https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.War.html

  

classpath要包含在WAR归档中的类路径。任何JAR或ZIP   此类路径中的文件包含在WEB-INF / lib目录中。任何   此类路径中的目录包含在WEB-INF / classes中   。目录

希望这会对你有所帮助!

答案 1 :(得分:0)

gradle之所以添加额外的jar,是因为库在依赖项中声明可能还有其他依赖项。

尝试使用 providedCompile providedRuntime 避免向战争添加额外的依赖关系。

要添加库,请尝试使用 classpath (如https://docs.gradle.org/current/userguide/war_plugin.html

中所述)

自定义部分显示:

war {
from 'src/rootContent' // adds a file-set to the root of the archive
webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
classpath fileTree('additionalLibs') // adds a file-set to the WEB-INF/lib dir.
classpath configurations.moreLibs // adds a configuration to the WEB-INF/lib dir.
webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml 
}
相关问题