解决Gradle生成Eclipse支持后,发布到Tomcat丢失依赖jar包的问题
最近一个项目中,使用号称下一代构建工具的Gradle构建项目。
使用中发现一个问题,Gradle从中央库下载的jar文件在系统的其它目录,使用gradle eclipse添加Eclipse支持时,jar文件是以外部依赖的形式导入的。Eclipse将web项目发布到Tomcat时,是不会自动发布这些依赖的。
可以通过Eclipse在项目上右击 - Propertics - Deployment Assembly,添加“Java Build Path Entries”,添加所有依赖的jar包,就可以在发布时自动发布外部依赖的jar包。
但是手动添加,是不符合自动化构建的要求的,打开.classpath文件,发现gradle自动生成的文件含有类似如下的代码
<classpathentry
sourcepath="C:/Documents and Settings/XXX/.gradle/caches/artifacts-24/filestore/commons-collections/commons-collections/3.2/source/73d0340eaecbb0ec9d3e0ace90547ef08cbfaf27/commons-collections-3.2-sources.jar"
kind="lib"
path="C:/Documents and Settings/XXX/.gradle/caches/artifacts-24/filestore/commons-collections/commons-collections/3.2/jar/f951934aa5ae5a88d7e6dfaa6d32307d834a88be/commons-collections-3.2.jar"
exported="true" />
在Eclipse中设置好Deployment Assembly后,代码变为这样
<classpathentry sourcepath="C:/Documents and Settings/XXX/.gradle/caches/artifacts-24/filestore/commons-collections/commons-collections/3.2/source/73d0340eaecbb0ec9d3e0ace90547ef08cbfaf27/commons-collections-3.2-sources.jar" kind="lib" path="C:/Documents and Settings/XXX/.gradle/caches/artifacts-24/filestore/commons-collections/commons-collections/3.2/jar/f951934aa5ae5a88d7e6dfaa6d32307d834a88be/commons-collections-3.2.jar" exported="true"> <attributes> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib" /> </attributes> </classpathentry>
这样就简单了,我们让gradle自动添加Deployment Assembly
在gradle.build中添加下面的代码
// 生成Eclipse支持时,自动生成Deployment Assembly eclipse.classpath.file.withXml { def node = it.asNode(); for (Node n : node.children()) { if ( "lib" .equals(n.attribute( "kind" ))) { def node_attributes = new Node(n, "attributes" ); def map = new HashMap(); map.put( "name" , "org.eclipse.jst.component.dependency" ); map.put( "value" , "/WEB-INF/lib" ); def node_attribute = new Node(node_attributes, "attribute" , map); } } } |
保存以后重新运行gradle eclipse,回到Eclipse刷新项目,现在发布项目,就能自动将所有外部依赖jar包发布到Tomcat下
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律