解决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下

转载于:https://my.oschina.net/heiyexue/blog/307705

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值