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

前端 未结 2 2109
甜味超标
甜味超标 2021-02-03 14:28

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 followi

相关标签:
2条回答
  • 2021-02-03 15:00

    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>
    
    0 讨论(0)
  • 2021-02-03 15:01

    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.

    0 讨论(0)
提交回复
热议问题