如何使用常春藤来构建一个没有将jar复制到lib目录的战争

Ama*_*den 6 ant ivy

我的目标是让我的ant构建脚本构建一个war文件,并包含常春藤知道这个项目依赖的jar.我现在能想出的最佳代码如下

<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>
Run Code Online (Sandbox Code Playgroud)

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

我想做的更像是以下内容

<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>
Run Code Online (Sandbox Code Playgroud)

问题是lib标签不会引入任何类型的refid.任何想法或我是否坚持使用额外的文件副本?

Mar*_*nor 4

这里的问题是lib标签是一个自定义文件集,它将其文件定位到 war 存档的lib子目录中。也许可以编写一个自定义战争任务,但我认为这不值得付出努力。

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

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

    <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>
Run Code Online (Sandbox Code Playgroud)

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

    <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>
Run Code Online (Sandbox Code Playgroud)

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

总之,我明白你的帖子的重点是关心你的 jar 文件的多个副本...使用我建议的方法将进一步增加你的副本,但我认为这不是一个问题,只要你有一个干净的目标要删除之后的他们。

  • 您想要的解决方案是使用 ivy 缓存文件集任务而不是缓存路径。然后 warfile 的 lib 标签的 refid 参数将按预期工作 (2认同)