Lab: Configuration Management with Ivy

Steven J Zeil

Last modified: Oct 12, 2015
Contents:
1 Find That Project
2 Adding Ivy to the Build
3 Adding Ivy to the Eclipse Project
4 Where is Everything?

This is a self-assessment activity to give you practice in working with the Ivy configuration manager. Feel free to share your problems, experiences, and choices in the Forum.

1 Find That Project

In a previous lab, you set up an Eclipse project with an associated Ant build.

We’ll use that as a starting point for this lab.

One of the sticking points in the previous lab came when we needed to supply the JUnit and Hamcrest libraries to the build process. Our solution at the time was to download those to some convenient directory and to hard-code that location into our build:

    <path id="test.compilation.path">
        <pathelement path="target/classes"/>
        <pathelement path="/home/zeil/src/junit4.jar"/>
        <pathelement path="/home/zeil/src/hamcrest-core.jar"/>
    </path>

There are problems with this approach.

In this lab, we’ll replace those hard-coded download locations with a more dynamic, shareable approach based on Ivy.

2 Adding Ivy to the Build

  1. The first thing to do is to make sure that our Ant build can use Ivy. I use pretty much the same steps for doing this in every project, so I have found it useful to package them into a separate build file. Download that file and save it in the same directory as your build.xml file, naming it ivy.build.xml.

  2. Now edit your build.xml file and

    before the first of the “<path” declarations.

  3. The changes in the prior step will allow Ivy to run. But we need to tell Ivy just what libraries we actually need for this project.

    Create a new file named ivy.xml (in the same directory as build.xml and, in there, indicate that we need JUnit:

        <ivy-module version="2.0">
          <info organisation="edu.odu.cs" module="myProject" />
          <dependencies defaultconfmapping="*->default">
           <dependency org="junit" name="junit" 
             rev="4.11" />
          </dependencies>
        </ivy-module>
    

    Feel free to change the “info” to something more appropriate.

    This is a very minimal ivy.xml. In particular, there’s no real use of “configurations”, which are a useful but confusing tool for managing different views of the project (e.g., differentiating between different sets of 3rd-party libraries that are needed at different stages of the build).

    Note that we tell Ivy that we need JUnit but not Hamcrest. That’s because the only reason that we need Hamcrest is because JUnit needs it, and Ivy manages those kind of indirect dependencies for us.

  4. Finally, we need to tell Ivy where to look for all these wonderful libraries that we want to download. that is done in an ivysettings.xml file. This tends to be something that is a fixed setup for all projects at a given company. This file is often not included in project source code for that reason, but is assumed to reside in the programmer’s home directory or in a shared locations accessible to the entire development team.

    We, however, are going to put it right into the same directory with our build file and other Ivy files. So download this file and store it in that same project directory.

  5. Now run your Ant build.xml from inside Eclipse (or from the command line). Run it first with the target “clean” to get rid of the compilation results form the last lab.

    Then run it again with the default target (“build”). You should see some initial messages about fetching Ivy, then locating and fetching the JUnit and Hamcrest jars, before the rest of your build proceeds as normal.

3 Adding Ivy to the Eclipse Project

Normally, our next concern would be that Eclipse needs to know about the same downloaded libraries. It’s not crucial to this project because JUnit support is already built into Eclipse, but we’ll still walk through the process.

And, surprise, once again we are going to be adjusting the project’s Build Path.

  1. If you have not already done so, follow the instructions in the Lectures to add Ivy support (IVYDE) in your Eclipse installation.

  2. Right-click on any file in the project, and select “Build path => Configure Build Path”. Go to the Libraries tab. Click “Add Library…” and select “IVYDE Managed Dependencies”. Click Next.

  3. A dialog box will pop up allowing you to configure Ivy support for this project. On the Main tab, it wants to know the name and location of your ivy.xml file. This should default to the proper location.

    On the Settings tab, you need to tell Eclipse about your ivysettings.xml file. This usually will need adjustment. Start by selecting the box “Enable project specific settings”. Then, under “Ivy settings path:”, click the “Project…” button and select your ivysettings.xml file.

    When you click “Finish”, you may see a brief message indicating that Eclipse is resolving your Ivy dependencies. It will do the same any time you edit your ivy.xml file.

4 Where is Everything?

  1. Right-click on your Project name and select “Refresh”. You sill see that you have a new folder named “ivy” and, in there, and ivy.jar file. That’s the actual Ivy system support. If you delete this, it will be reconstructed next time you run your Ant build.

  2. In your operating system, look at ~/.ivy2 if you are on a Linux or OS/X machine, or %HOME%/.ivy2 if you are on Windows. (%HOME% is generally C:\Users\_yourLoginName_ on a typical Windows system.)

    You should see local and cache directories. If you poke around inside cache, you should be able to find the jar files that Ivy has downloaded on your behalf.

    On occasion, you might find it useful to clear out this cache (e.g, if it’s holding a lot of libraries for older projects that you are no longer working on). It’s reasonably safe to do so, as Ivy will just re-download the libraries that you do need when you rebuild your current projects.