如何使用常春藤来构建战争而不将jar复制到lib目录

时间:2010-01-16 16:12:29

标签: 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>

此代码的问题是它将jar复制两次。一旦进入我的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。任何想法或我是否坚持使用额外的文件副本?

2 个答案:

答案 0 :(得分:4)

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

如果想改善常春藤管理战争依赖关系的方式,我建议使用配置吗?

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

    <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>

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

总而言之,我理解您的帖子的重点是关注您的jar文件的多个副本...使用我建议的方法将进一步复制您的副本,但我会认为这不是问题,前提是您有< em> clean 目标,然后删除它们。

答案 1 :(得分:4)

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

实施例

<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>