How to use ivy to build a war with out copying jars to a lib directory
Asked Answered
G

2

7

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>

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>

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?

Gamo answered 16/1, 2010 at 16:12 Comment(1)
Use <ivy:cachefileset setid="locpathref.classpath" conf="runtime"/> then the lib tag will work as expectedChian
V
4

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.

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>

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>

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.

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.

Vyner answered 23/1, 2010 at 17:37 Comment(2)
I agree that this is a better solution then what I am doing but it still does not remove the extra file copy as I would like. The more I look at this the more I think that, as you said, the only real way to fix is by creating a custom war task or even a new custom lib task for the war task. In a perfect world this new task could would day become part of Ivy or Ant.Gamo
The solution you want is to use the ivy cachefileset task instead of cachepath. Then the refid parameter to the warfile's lib tag will work as expectedChian
A
4

If you're using Ant 1.8, you can use the technique described here: http://www.beilers.com/2010/06/ivy-dependency-management-lessons-learned-and-ant-1-8-mapped-resources/

EXAMPLE:

<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>
Appetence answered 11/3, 2011 at 23:30 Comment(1)
Very interesting ... I will try this and report!Hypodermic

© 2022 - 2024 — McMap. All rights reserved.