6

我的目标是让我的 ant 构建脚本构建一个 war 文件并包含 ivy 知道该项目所依赖的 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>

这段代码的问题是它复制了两次罐子。一次进入我的 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>

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

4

2 回答 2

4

这里的问题是lib标记是一个自定义文件集,它将其文件定位到战争档案的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>

之后,您将它们检索到专用目录(使用模式)中,可以使用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>

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

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

于 2010-01-23T17:37:52.823 回答
4

如果您使用的是 Ant 1.8,则可以使用此处描述的技术: http ://www.beilers.com/2010/06/ivy-dependency-management-lessons-learned-and-ant-1-8-mapped-资源/

例子:

<war destfile="${war.full.path}" webxml="WebContent/WEB-INF/web.xml" manifest="${manifest.path}">
    <fileset dir="WebContent">
     </fileset>
    <classes dir="${build.dir}"/>

    <mappedresources>
      <restrict>
        <path refid="classpath.CORE"/>
        <type type="file"/>
      </restrict>
      <chainedmapper>
        <flattenmapper/>
        <globmapper from="*" to="WEB-INF/lib/*"/>
      </chainedmapper>
    </mappedresources>

    <zipfileset dir="src" prefix="WEB-INF/classes">
         <include name="**/resources/**/*.properties" />
         <include name="**/resources/**/*.xml" />
    </zipfileset>
</war>
于 2011-03-11T23:30:42.670 回答