您的位置:首页 > 运维架构 > Tomcat

解决Gradle生成Eclipse支持后,发布到Tomcat丢失依赖jar包的问题

2013-07-31 13:30 881 查看
最近一个项目中,使用号称下一代构建工具的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下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐