-1

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>
holly.woo
  • 15
  • 1
  • 10
  • Ant and Ivy are passe. I would recommend that you move to Maven if you're making an effort anyway. – duffymo Dec 14 '20 at 10:32
  • I don't see the reason to migrate to a Maven proj just because "it's newer" if this is working. And however, integrating Ivy before migrating directly to Maven will help the process. https://stackoverflow.com/questions/17237679/migrating-complex-project-from-ant-to-maven-how-to-handle-unusual-folder-struc – holly.woo Dec 14 '20 at 10:56
  • It's not that Maven is newer. It's better at managing dependencies that Ivy. It's rather old, in fact. Nobody uses Ant or Ivy anymore. Look at every open source Java project there is. They all use Maven or Gradle. Ant has disappeared. – duffymo Dec 14 '20 at 11:34
  • In my case, that's unneeded right now. It will be maybe thought for future improvements. Right now, is out of purpose. Thanks for your suggestions, tho. – holly.woo Dec 14 '20 at 13:49

1 Answers1

0

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • in which block should I put the `ivy:resolve` in order to be taken from the eclipse compiler? – holly.woo Dec 14 '20 at 13:51
  • Doesn't matter, as long as it ran before you do something that requires dependencies to be in place. For example, before generating eclipse project files (if your ant script does that), before compiling (if your ant script does that), before testing, etcetera. – rzwitserloot Dec 14 '20 at 15:04