Wednesday, July 11, 2012

How to add external jar libraries to your Grails 2.0

I needed to setup my Grails application with a custom library that was not provided by traditional maven public repositories. Here is how I got it working:
1. Setup a local folder structure to hold custom libraries using maven/ivy naming convention. Add a system environment variable for the path to the local folder
2. Edit grails-app/conf/BuildConfig.groovy to add the local repository.
3. Edit grails-app/conf/BuildConfig.groovy to add the new local dependency jar.
4. Test the build by running grails command dependency-report
5. Create a new build with grails command war
6. Regenerate the Spring Source Tool Suite project and classpath files using grails command integrate-with --eclipse

Listing for step 1 Local Repository
C:.
├───lib
│   └───com
│       └───fourgablesguy
│           └───myapplication
│               └───1.0.0
│                   └───myapplication-1.0.0.jar

set LOCAL_REPOSITORY=C:\lib
Listing for step 2 and 3 BuildConfig.groovy
if (System.getenv().LOCAL_REPOSITORY == null) {
 println "*"
 println "* ERROR - LOCAL_REPOSITORY environment variable is undefined."
        System.exit(1)
} else {
 println "* LOCAL_REPOSITORY=" + System.getenv().LOCAL_REPOSITORY
 localRepoDir = System.getenv().LOCAL_REPOSITORY
}
grails.project.dependency.resolution = {
    //snip 
    repositories {
        //snip 
 mavenRepo "file:${localRepoDir}"
    }
    dependencies {
        //snip 
        compile "com.fourgablesguy:myapplication:1.0.0"
    }
}
Listing for step 4-6 Grails commands
grails dependency-report 
grails package
grails integrate-with --eclipse

No comments:

About Me

My photo
Lead Java Developer Husband and Father

Tags