简体   繁体   中英

Adding Ivy in old Ant project

I've already read some articles but I'm not able to understand how I should setup an Ivy file for dependencies. The project I'm working on is old and I don't know it much, I have this half generated - half edited build.xml file which works with Eclipse compiler. It's the first time I deal with build configuration, I want to move the dependencies to the ivysettings.xml file and improve what can be improved. My target is to create a war to deploy in Tomcat.

What could be the best way to do it?

build.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="Teamwork"
    xmlns:ivy="antlib:org.apache.ivy.ant">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../../../../Program Files/eclipse/"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.7"/>
    <property name="source" value="1.7"/>
    <path id="Web App Libraries.libraryclasspath">
        <pathelement location="WebContent/WEB-INF/lib/Tidy.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/antlr-2.7.7.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/asm-attrs.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/asm.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/avro-1.5.1.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/aws-java-sdk-1.5.5.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/bsh-2.0b4.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/c3p0-0.9.2.1.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/commons-beanutils.jar"/>
        [... other jars]
    </path>
    <path id="EAR Libraries.libraryclasspath"/>
    <path id="Teamwork.classpath">
        <pathelement location="bin"/>
        <path refid="Web App Libraries.libraryclasspath"/>
        <path refid="EAR Libraries.libraryclasspath"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="Teamwork.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
</project>

Step 1 is to get rid of that <path id> block; that's what you're refactoring into an ivy-based solution. You need an ivy.xml file that lists your dependencies. An example can be found here - that is about as complex as it'll get, yours can presumably be simpler. I suggest test , runtime and build configurations, you don't need more unless you have a pressing need. Then, for each dependency, figure out in which contexts you need it (if you need it on the cp when compiling, 'build'. If you need it on the cp when running, 'runtime'. Only put in the 'test' configuration when it's for testing but nothing else (so, usually, junit and that's it).

Then in your ant file you're looking to do something like:

<ivy:resolve file="buildScripts/ivy.xml" refresh="true" conf="build, runtime, test" />  
<ivy:retrieve/>

which will then make those jars appear in lib/test , lib/runtime and lib/build . Use start ant tricks to make a object (do not explicitly list out each jar). This does play havoc when you remove a dependency, as that won't make the jar file disappear, but it's probably simpler to then just ant clean (which should now also delete the lib dir in addition to build ). There are ways to make an ant <path> object off of a configuration but that's more complicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM