如何使用 ivy 构建战争而不将 jars 复制到 lib 目录 [英] How to use ivy to build a war with out copying jars to a lib directory

查看:22
本文介绍了如何使用 ivy 构建战争而不将 jars 复制到 lib 目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是让我的 ant 构建脚本构建一个 war 文件,并包含 ivy 知道该项目依赖的 jar.我目前能想到的最好的代码如下

My goal is have my ant build script build a war file and include the jars that ivy knows this project depends on. The best code I could come up with at the moment is the following

<mkdir dir="dist/lib"/>
<ivy:retrieve pattern="dist/lib/[artifact].[ext]" sync="true"/>
<war destfile="dist/${ivy.module}.war" basedir="build" includes="**/*.class"
    webxml="${war.webxml}">
    <fileset dir="${war.web}"/>
    <lib dir="dist/lib"/>
</war>

这段代码的问题是它复制了 jars 两次.一次进入我的 dist/lib 目录,并在它创建时再次进入战争.它有效,但我无法摆脱有更好方法的感觉.

The problem with this code is it copies the jars twice. Once in to my dist/lib directory and again in to the war when it's created. It works but I can't shake the feeling there is a better way.

我想做的是更像下面的事情

What I would like to do is something more like the following

<ivy:cachepath pathid="locpathref.classpath"/>
<war destfile="dist/${ivy.module}.war" basedir="build" includes="**/*.class"
    webxml="${war.webxml}">
    <fileset dir="${war.web}"/>
    <lib refid="locpathref.classpath"/>
</war>

问题是 lib 标签不接受任何类型的 refid.有什么想法还是我坚持使用一组额外的文件副本?

The problem is that the lib tag does not take in a refid of any kind. Any ideas or am I stuck with an extra set of file copies?

推荐答案

这里的问题是 lib 标签是一个自定义的 fileset,它的目标是将它的文件放入 war存档的 lib 子目录.可能可以编写自定义战争任务,但我认为不值得付出努力.

The problem here is that the lib tag is a custom fileset that targets it's files into the war archive's lib sub directory. It might be possible to write a custom war task but I don't think it's worth the effort.

如果想改进 ivy 管理战争依赖项的方式,我可以建议使用配置吗?

If want to improve the manner in which ivy manages your war's dependencies might I suggest using configurations?

创建描述运行时依赖项的配置:

Create a configuration describing the run-time dependencies:

    <ivy-module version="2.0">
    <info organisation="apache" module="hello-ivy"/>
    <configurations>
        <conf name="build" description="Libraries needed to for compilation"/>
        <conf name="war" extends="build" description="Libraries that should be included in the war file" />
    </configurations>
    <dependencies>
        <dependency org="commons-lang" name="commons-lang" rev="2.0" conf="build->*,!sources,!javadoc"/>
        <dependency org="commons-cli" name="commons-cli" rev="1.0" conf="build->*,!sources,!javadoc"/>
    </dependencies>
</ivy-module>

之后,您将它们检索到一个专用目录(使用模式),可以使用 war 任务的 lib 标签简单地包含该目录:

Afterwards you retrieve them into a dedicated directory (using a pattern) which can be simply included using the war task's lib tag:

    <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact].[ext]"/>

    <war destfile="${war.file}" webxml="${resources.dir}/web.xml">
        <fileset dir="${resources.dir}" excludes="web.xml"/>
        <lib dir="${lib.dir}/war"/>
    </war>

这种方法的优点是您可以使用每个项目依赖项的 ivy conf 属性来最终决定 jar 是否包含在 war 文件中.构建文件不再关心.

The advantage of this approach is that you use the ivy conf attribute of each project dependency to ultimately decide if the jar gets included within the war file or not. The build file no longer cares.

总而言之,我明白您的帖子的重点是关注您的 jar 文件的多个副本......使用我建议的方法将进一步增加您的副本,但我认为这不是问题,前提是您有 clean 目标以在之后删除它们.

In conclusion I understand that the point of your post was concern for multiple copies of your jar files... Using my suggested approach will further multiple your copies, but I would submit that this is not an issue provided you have a clean target to remove them afterwards.

这篇关于如何使用 ivy 构建战争而不将 jars 复制到 lib 目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆