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

Friday, July 6, 2012

How to set Grails 2.0 DataSource to use JNDI for run-app command.

I wanted to setup my Grails 2.0 application to use a JNDI connection in the DataSource.groovy file. Here is how I got it working:

  1. Edit grails-app/conf/DataSource.groovy and configure a jndi data source:
  2. Edit grails-app/conf/Config.groovy and configure the Grails managed resource
  3. Edit the BuildConfig.groovy to insert the resource-ref in the web.xml
Here is the listing for DataSource.groovy:

environments {
    development {
dataSource {
dbCreate = "create"
jndiName = 'java:comp/env/jdbc/mydatasource'
}
              } 
}
Here is the listing for Config.groovy:

environments {
    development {
        grails.logging.jul.usebridge = true
jquery.minified = true
jqueryUi.minified = true
grails.naming.entries = ['jdbc/mydatasource': [
type: "javax.sql.DataSource",
       auth: "Container",
       description: "Data source for ...",
url: "jdbc:oracle:thin:@:",
username: "",
password: "",
driverClassName: "oracle.jdbc.driver.OracleDriver",
maxActive: "8",
              maxIdle: "4"
]
]
      }
}
Note: You will need to manually add postgres, mysql, oracle or db2 jdbc jar(s) to the application lib folder to use a datasource which references a driverClassName which is not provided by the Grails platform by default. 

Here is the listing for BuildConfig.groovy :


grails.war.resources = { stagingDir, args ->
def webxml = new File(grailsSettings.baseDir,"${stagingDir}/WEB-INF/web.xml")
def newxml = new File(grailsSettings.baseDir,"${stagingDir}/WEB-INF/new.xml")
def root = new XmlParser().parse(webxml)


// add the jdbc/mydatasource resource reference to the web.xml
def resourceRef = root.appendNode('resource-ref')
resourceRef.appendNode('description','The JNDI Database resource')
resourceRef.appendNode('res-ref-name','jdbc/mydatasource')
resourceRef.appendNode('res-type','javax.sql.DataSource')
resourceRef.appendNode('res-auth','Application')

def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(root)
newxml.withWriter { out ->
out.writeLine(writer.toString())
}
webxml.delete()
newxml.renameTo(webxml.path)
}

The code in BuildConfig is only needed to ensure the deployed war file has the required resource references. I do not believe you need that for just using the grails run-app command.




About Me

My photo
Lead Java Developer Husband and Father

Tags