14

Can anyone explain me how to use filesystem resolver in Ivy with sample by considering..

  1. I have ivy.xml file where i have defined all the dependecies but i want the jars from my filesystem not from maven repository..?

  2. where do i put ivysettings.xml file.

  3. what build.xml should contain to use ivysettings.xml so that i can use jars from filesystem not from maven..

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
pallavi
  • 735
  • 2
  • 10
  • 18
  • it would be better to include these details in your other question, which is basically the same: http://stackoverflow.com/questions/10171254/how-do-i-use-a-local-folder-as-an-ivy-repository. You can use the edit link below your question to edit/change it. – oers Apr 16 '12 at 15:08

2 Answers2

23

The ivysettings.xml file is located by default in the same directory as the ivy.xml file.

Alternative locations can be specified using the ivy settings task

Project structure

3rd party dependencies located in the lib directory.

$ tree
.
|-- build.xml
|-- ivysettings.xml
|-- ivy.xml
|-- lib
|   |-- junit-4.10.jar
|   |-- slf4j-api-1.6.4.jar
|   `-- slf4j-simple-1.6.4.jar
`-- src
    |-- main
    |   `-- java
    |       `-- org
    |           `-- demo
    |               `-- App.java
    `-- test
        `-- java
            `-- org
                `-- demo
                    `-- AppTest.java

10 directories, 8 files

ivy.xml

This ivy file uses configurations to manages 3 kinds of dependencies:

  1. compile
  2. runtime
  3. test

These will correspond to the classpaths used by the ANT build.

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations defaultconfmapping="compile->default">
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.6.4"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-simple" rev="1.6.4" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
    </dependencies>

</ivy-module>

ivysettings.xml

Nothing fancy. Simple mapping to the jars located in the lib directory:

<ivysettings>
    <settings defaultResolver="local"/>
    <resolvers>
        <filesystem name="local">
            <artifact pattern="${ivy.settings.dir}/lib/[artifact]-[revision].[ext]"/>
        </filesystem>
    </resolvers>
</ivysettings>

Alternatively..... I would always include the Maven central repo for non-local jars as follows:

<ivysettings>
    <settings defaultResolver="central"/>
    <resolvers>
        <ibiblio name="central" m2compatible="true"/>
        <filesystem name="local">
            <artifact pattern="${ivy.settings.dir}/lib/[artifact]-[revision].[ext]"/>
        </filesystem>
    </resolvers>
    <modules>
        <module organisation="org.slf4j" resolver="local"/>
        <module organisation="junit" name="junit" resolver="local"/>
    </modules>
</ivysettings>

The modules section specifies which jars should be retrieved locally.

build.xml

Finally the build logic that uses ivy to manage the classpaths.

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!--
    ================
    Build properties
    ================
    -->
    <property name="src.dir" location="src/main/java"/>
    <property name="test.src.dir" location="src/test/java"/>
    <property name="build.dir" location="build"/>
    <property name="classes.dir" location="${build.dir}/classes"/>
    <property name="test.classes.dir" location="${build.dir}/test-classes"/>
    <property name="ivy.reports.dir"  location="${build.dir}/ivy-reports"/>
    <property name="test.reports.dir"  location="${build.dir}/test-reports"/>

    <!--
    ===========
    Build setup
    ===========
    -->
    <target name="init">
        <ivy:resolve/>

        <ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="runtime.path" conf="runtime"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>

        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${test.classes.dir}"/>
        <mkdir dir="${test.reports.dir}"/>
    </target>

    <!--
    ===============
    Compile targets
    ===============
    -->
    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    </target>

    <target name="compile-tests" depends="compile">
        <javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
            </classpath>
        </javac>
    </target>

    <!--
    ============
    Test targets
    ============
    -->
    <target name="test" depends="compile-tests">
        <junit printsummary="yes" haltonfailure="yes">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
                <pathelement path="${test.classes.dir}"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${test.reports.dir}">
                <fileset dir="${test.src.dir}">
                    <include name="**/*Test*.java"/>
                    <exclude name="**/AllTests.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!--
    =====================
    Build and run targets
    =====================
    -->
    <target name="build" depends="test"/>

    <target name="run" depends="build">
        <java classname="org.demo.App">
            <classpath>
                <path refid="runtime.path"/>
                <pathelement location="${classes.dir}"/>
            </classpath>
        </java>
    </target>

    <!--
    =============
    Clean targets
    =============
    -->
    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean">
        <ivy:cleancache/>
    </target>

</project>
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • thanks.. now my app is using jars from my lib folder instead of downloading from maven. :) – pallavi Apr 17 '12 at 06:12
  • but the problem is how to give the dependency line for selenium-server-standalone 2.20.0 because dont know artifactid ,groupid but this jar is needed.. – pallavi Apr 17 '12 at 13:10
  • That's a new question..... Try "".. See http://search.maven.org/#artifactdetails|org.seleniumhq.selenium|selenium-server|2.20.0|jar – Mark O'Connor Apr 17 '12 at 23:17
  • 1
    @MarkO'Connor - i keep seeing you answering ant/ivy questions... thanks! – andrew cooke Apr 18 '13 at 16:59
  • This works but only when the `default` configuration is asked from the `filesystem` resolver. I have asked a separate question here: http://stackoverflow.com/q/42096908/274677 – Marcus Junius Brutus Feb 07 '17 at 18:09
-2

If you're ONLY pulling jars locally and do not want to use Maven Central at all, Ivy is a waste of your time. Just put whatever jars you need in your /lib folder, add that to your classpath, and run your Ant file. Ivy is for interacting with Maven Central. However, if you need to do both, pull common jars from Maven and pull 3rd party jars from local, Mark's solution is a great one.

user3442998
  • 75
  • 1
  • 3