Accessing CLI args and java system properties from a Grails script 2

Posted by mjwall on October 23, 2009

Quick post so I can remember how to access CLI args and Java system properties inside of a grails script.

Put this following code inside of your_grails_app/scripts/ScriptTest.groovy

target ( default : 'Print args and java system properties' ) {
    //grails script-test arg1 arg2
    println "Grails CLI args"
    println Ant.project.properties."grails.cli.args" 

    //grails -Dprop1anything script-test
    println "Java system properties of prop1"
    println Ant.project.properties.prop1
}

So now you can run the following

$grails -Dprop1=anything script-test arg1 arg2
Welcome to Grails 1.1.1 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Library/Grails/Home

Base Directory: /Users/mjwall/src/sample1
Running script /Users/mjwall/src/sample1/ScriptTest.groovy
Grails CLI args
arg1
arg2
Java system properties of prop1
anything

Grails and Maven with no Maven 4

Posted by mjwall on January 10, 2009

According the Grails 1.1 Beta2 Release Notes, Grails 1.1 will have better Maven integration. I think that is great news allowing more integration between different java projects. It means this post may not relevant for very long though.

For all it’s complexity, I like maven. Sometimes it makes complex tasks easier, but not always. Here is my situation. There are several java projects already using maven. I am building a grails project that will be used by some of these projects. The easiest integration is to package up a war file and deploy it to our local maven repository. Nexus is great by the way. The other projects can include my grails project as a dependency. So what is the best way to do that? Right, I hear you. By hand. However, I need to automate this.

I looked at the grails-maven-plugin, but it is too much. I am not trying to mavenize my project, just deploy it to Nexus.

Luckily, there is as a cleaner answer. Creating scripts in grails is easy. Those scripts can use the maven-ant-tasks. Download the jar file and put it in your lib directory.

I’ll create 2 tasks, one handle the ‘maven install’ so I can test locally and one to handle ‘maven deploy’. We need a pom file, but instead of checking one in, let’s generate it from the project so it picks up the latest version etc. (Note, I studied the gant build file pretty closely). Here is an example of MavenInstall.groovy file from the scripts directory


1 includeTargets
<< grailsScript ( “War” )
2
3 final antlibXMLns = ‘antlib:org.apache.maven.artifact.ant’
4 final tempPomFile = “pom.xml”
5
6 target (preparePom : “Generate a temporary pom file”) {
7 depends(packageApp)  //so config.maven properties are loaded
8 def writer = new StringWriter()
9 def builder = new groovy.xml.MarkupBuilder(writer)
10 builder.project(xmlns:“http://maven.apache.org/POM/4.0.0″,
11 ‘xmlns:xsi’:“http://www.w3.org/2001/XMLSchema-instance”,
12 ‘xsi:schemaLocation’:“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”) {
13 ‘modelVersion’ ‘4.0.0′
14 ‘groupId’ ‘com.mjwall’
15 ‘artifactId’ grailsAppName
16 ‘packaging’ ‘war’
17 ‘version’ grailsAppVersion
18 }
19
20 File temp = new File(tempPomFile)
21 temp.write writer.toString()
22 def pom =ant.${antlibXMLns}:pom” ( file : tempPomFile , id : tempPomFile )
23 echo(“Temporary pom file written to ${tempPomFile}, don’t forget to clean up”)
24 return tempPomFile
25 }
26
27 target (deletePom : “Clean up the temporary pom file”) {
28 new File(tempPomFile).delete()
29 }
30
31 target (getWarName : “Return the war name defined by the app configs”) {
32 depends(war)
33 // bug in grails clean, doesn’t seem to delete war from a custom location defined by grails.war.destFile
34 return warName
35 }
36
37 target (default : “Install to local maven repository”) {
38 depends(war)
39 def tempPom = preparePom()
40 ant.${antlibXMLns}:install” ( file : getWarName() ) { pom ( refid : tempPom ) }
41 deletePom()
42 }
43

Not too scary, but what is going on here? The default task you will see depends on the war file being built. A temporary pom is created using the value defined in application.properties. Then the maven-ant-task for install is run and the pom is deleted. Pretty simple huh? It is once you see an example anyway.

How about a maven deploy. Basically the same thing, except the pom needs to generate a distributionManagement section. I set up a couple of properties in my Config.groovy at the end called maven.remoteReleaseUrl and maven.remoteSnapshotUrl. Change the pom generate to include them. Looks like this


6 target (preparePom :
“Generate a temporary pom file”) {
7 depends(packageApp)  //so config.maven properties are loaded
8 def writer = new StringWriter()
9 def builder = new groovy.xml.MarkupBuilder(writer)
10 builder.project(xmlns:“http://maven.apache.org/POM/4.0.0″,
11 ‘xmlns:xsi’:“http://www.w3.org/2001/XMLSchema-instance”,
12 ‘xsi:schemaLocation’:“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”) {
13 ‘modelVersion’ ‘4.0.0′
14 ‘groupId’ ‘com.mjwall’
15 ‘artifactId’ grailsAppName
16 ‘packaging’ ‘war’
17 ‘version’ grailsAppVersion
20 ‘distributionManagement’ {
21 ‘repository’ {
22 ‘id’ ‘releases’
23 ‘name’ ‘Internal Releases’
24 ‘url’ config.maven.remoteReleaseUrl //defined in Config.groovy
25 }
26 ’snapshotRepository’ {
27 ‘id’ ’snapshots’
28 ‘name’ ‘Internal Snapshots’
29 ‘url’ config.maven.remoteSnapshotUrl //defined in Config.groovy
30 ‘uniqueVersion’ ‘true’
31 }
18 }
19
20 File temp = new File(tempPomFile)
21 temp.write writer.toString()
22 def pom =ant.${antlibXMLns}:pom” ( file : tempPomFile , id : tempPomFile )
23 echo(“Temporary pom file written to ${tempPomFile}, don’t forget to clean up”)
24 return tempPomFile
25 }

Then instead of calling ant.”${antlibXMLns}:install”, call ant.”${antlibXMLns}:deploy”

Couple of notes. I did run into problems with my settings.xml file defined in ~/.m2, so I ended up creating one much like I did with the pom.xml. Only used when deploying, so it is not built for the install. Finally, because grails can only run one task per script, I DRYed up this whole thing with one file called MavenUtils that included all code for both install and deploy. Then, my MavenInstall file just loads the MavenUtils and calls install.

Wow, more explanation than I thought it would. And I guess the title is not entirely accurate. I am using maven, but not by calling the maven executable directly. Hopefully the new Maven/Grails integration will make this easier. Fingers crossed.

Groovy plugin patch for running Grails 1.1 Beta2 on Intellij 8.0.1 4

Posted by mjwall on January 01, 2009

My last attempt at running Grails 1.1 beta2 on IntelliJ 8.0.1 didn’t work out so well. I ran the EAP version for a while, but had some issues building grails itself, specifically trying to run unit tests.

So, I did some digging through the bug tracker and subversion for the plugin. I also spent some time learning about IntelliJ plugins and reading newsgroups about what others have tried. The result is a patched version of the plugin that seems to be working. Here is the file if you want to try it. Unzip and replace the contents in your INTELLIJ_HOME/plugins/Groovy directory. Again, use at your own risk and backup your existing plugins/Groovy directory.

If you are interested, here are the details.

  • Running on a Mac with java 1.5
  • Check out code from http://svn.jetbrains.org/idea/Trunk/bundled/groovy
  • Revert back to revision 21543. 21544 breaks something. Revision 21538 fixed the initial issue I saw, where the grails-1.1-beta2 library was not recognized, as reported in GRVY-1933.
  • Add in revision 21697, which fixes GRVY-1943 so the app can run.
  • Setup IntelliJ 8.0.1 build 9164 with the dev package
  • Configure IntelliJ to build the plugin. These instructions helped. Groovy module configured to use Groovy-1.6_RC1. RT module configured to use groovy-1.5.7, cause 1.6 didn’t work.
  • Build grammer with ant task, run Make and then run ‘Prepare All Plugin Modules for Deployment’.
  • Shutdown IntelliJ.
  • Remove INTELLIJ_HOME/plugins/Groovy directory. Unzip groovy.zip in INTELLIJ_HOME/plugins directory
  • Restart IntelliJ

Hope it works for you and Happy New Year.

Intellij 8.0.1 issues with Grails 1.1 beta2 3

Posted by mjwall on December 29, 2008

Grails-1.1-beta2 does not work correctly with the jetgroovy plugin in intellij. I am able to add a global library for Grails 1.1 beta2, but it not saved in the project facet. The most apparent problem for me is that the ‘Run grails target’ shortcut is not available. See this thread and this bug for more details. According to the details, it is fixed. However, I don’t see an update to the jetgroovy plugin. I am running version 8.0.1 of Intellij on Mac OSX.

So I tried the EAP 9572 version to see if it included the fix that was reported in Jira. The bundled jetgroovy plugin appears to have the update. Yippee. Glad I didn’t have to build it myself according to the wiki,

Here is the interesting part that you may care about. I zipped up the plugins/Groovy folder and replaced the old version in Intellij 8.0.1. It seems to have fixed it for the stable version as well. Here is the file until JetBrains has something better. Use at your own risk, perhaps making a backup of the old plugins/Groovy folder.

UPDATE It appears there are issues with copying the plugin back to the 8.0.1 release. Loading the file inside grails-app just hangs. However, the EAP version is working just fine so far. Let me know if you have success getting it work in the stable version.

UPDATE 2 See this for a patched version that works better.