Search

Dark theme | Light theme

May 31, 2009

Use Ivy's dependency mechanisme with Grails

Use Ivy's dependency mechanisme with Grails

Sometimes we need extra dependencies for our Grails application. For example if we use MySQL as a database we need the MySQL Java connector library. With this library we can connect to MySQL in our Grails application. We can add a dependency ourselves by placing the JAR file in the lib directory of our Grails project. But we can also use Ivy to get the dependency for us from the Internet and place it in the lib directory.

First we need to install the Ivy plugin:

$ grails install-plugin ivy

Next we open the file ivy.xml in the root of our Grails project directory. We see a list of dependencies already defined in the file. We remove all dependencies and add the dependency for mysql-connector-java (if we don't remove the other dependencies some libraries will be added twice: once from the list of dependencies defined in ivy.xml and once from the GRAILS_HOME/lib directory):

<ivy-module version="2.0">
    <info organisation="org.example" module="test-1.1.1"/>
    <configurations defaultconfmapping="build->default;compile->compile(*),master(*);test,runtime->runtime(*),master(*)">
        <conf name="build"/>
        <conf name="compile"/>
        <conf name="test" extends="compile"/>
        <conf name="runtime" extends="compile"/>
    </configurations>
    <dependencies>
        <dependency org="mysql" name="mysql-connector-java" rev="5.1.6" conf="runtime"/>
    </dependencies>
</ivy-module>

Now we invoke the get-dependencies tasks from the Ivy plugin to tell Ivy to go out and get all our dependencies and install them in the lib directory:

$ grails get-dependencies

After running this target we look into the lib directory and we see the file mysql-connector-java-5.1.6.jar. We are ready to use a MySQL database and connect to it from our Grails application.