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.

Back to Java 1

Posted by mjwall on December 23, 2008

All good things must come to an end.  After doing Ruby development professionally for almost 2 years, I decided to change jobs and went back to my Java roots.

So I would like to detail some of the things I have missed noticed in the couple of months I have been back.

Productivity, everything just seems to take longer with Java.  Maybe I am just rusty, or maybe is the a combination of some of the other things listed here

Convention, there are too many choices I have to make and too many things I have to decide that distract me from writing the code.  Before I went to Ruby, I was an ant guy.  Now I am using maven and I think I like it.  The out of the box convention just feels better.  Still not sure about the dependency management though

Not needing an IDE, Vim and I have been friends for a long time and our friendship grew during my time as a Ruby developer.  Now that I am back to Java, I keep getting pulled toward an IDE again.  I used eclipse in the past, but I thinking I will go with IntelliJ.

Libraries, I forgot how many libraries there are.  It is nice to have a choice, but it sure adds time.

BDD/TDD, takes a lot longer.  I keep preaching, but it takes so much more effort the by in is just not there.

IRB, wow do I miss you.

Class Design is certainly different(better?)  I think part if it come trying to make to make things more testable and part of it comes from more focus on the DRY principle.

I’ll post more comments as I go.  Be on the lookout for Grails posts, as I just started rewriting a rest service with it.

Cruisecontrolrb, git and rails 2.1

Posted by mjwall on August 18, 2008

I really love cruisecontrol.rb in a team environment.  The ability to run tests against every commit is great, and it is such fun being the “build police”.

Excuse me sir, can I see your license to commit please?

We can have some fun with that one.

This license expired just before the dotcom bubble

You have a languague restriction here that says you can commit only while wearing vb.net

I need to see some proof of build collision insurance.

But I digress. Sorry, back to the program.

As rails grows, I have some questions.

  1. Where the heck is code for cruise control?
  2. Can I use git?
  3. Will it run against rails 2.1 since I am security conscience and have updated after the recent warnings and threats?

Let’s do some research.  The code used to be at rubyforge. Looks like the code is still there. However, according to this post, they are moving to github. The link in that post has an extra *, but sure enough, you can find cruisecontrol.rb at http://github.com/thoughtworks/cruisecontrol.rb. Cool, now I know where to get it.

What about git support?  A google search, turns up benburkert’s github branch. Looks promising. Searching git hub for cruisecontrol.rb shows several forks, but these are all from rubyforge git.  I want it straight from thoughtworks.  Digging a little in the code shows it is already in the thoughtworks master.  There is even a mercurial adapter.  Let’s give it a try. Sure enough, it works just fine with

cruise add projectname -r file:///home/me/src/gitproject -s git

Run

cruise help add

for all the options.

Great, 2 for 2.  Now what about support for Rails 2.1?  Unfortunately, it is not there. Cruisecontrol.rb currently runs with rails 1.2.3, and a simple version change in environment.rb doesn’t fix it. (Didn’t figure it would but I had to try it right?)  For all of us who have updated to ruby 1.8.7, we are stuck for now. Ruby 1.8.7 only runs rails 2.1. My last post about multiple version of ruby doesn’t really help either.

I did find this post answered by the team about porting cruisecontrol to merb. That sounds interesting indeed.

Until then, who is working on a rails 2.1 version of cruisecontrol.rb?

Multiple versions of ruby with stow 2

Posted by mjwall on August 16, 2008

What is the best way to support multiple versions of ruby?
Some options:

  • Multiruby looks like it may do what I want. I already have it installed, because I am autotest-addicted (first coined by Dr Nic.
  • Michael Greenly posted this, which would work since I am running ubuntu.
  • A buddy recommended GNU Stow.
I chose to use stow. Call me old fashion, but I really prefer doing things be hand. I didn’t like the thought of having to type ruby1.8.6 vs ruby1.8.7 proposed by Michael Greenly, but I did pick up a few things from his article. I am interested in the testing against multiple version like multiruby provides. However, the main reason I want different ruby installs is because different clients have different environments. So here is how I did it. For reference, I am using Ubuntu 8.0.4.
  1. Remove all ubuntu ruby packages.
    Use

    dpkg --list | grep -i ruby

    to find all the packages you have installed. Then use

    sudo apt-get remove packagename

    to remove those packages. We are going to build all of these by hand.

  2. Make sure you have the ubuntu build packages
    Run the following to make sure you have packages necessary to build ruby with

    sudo apt-get build-dep ruby1.8

    This will install stuff like autoconf and automake if you don’t already have them.

  3. Setup your filesystem for stow.
    Let’s first make a directory under /usr/local named stow with

    sudo mkdir /usr/local/stow

    We will install everything here and then use stow to create symlinks in /usr/local/bin. Also, change the directory permission for /usr/local/stow so you can install stuff there as your user. This has the added benefit of alerting you if you configure wrong or try to overwrite something. We will only need to sudo to run the stow command. Run the following to see what groups you are in

    groups username

    Hopefully there will be a dev or adm or something similiar. I will use the adm group. Run the following to change the group and permissions on /usr/local/stow.

    sudo chgrp -R adm /usr/local/stow

    and

    chmod -R 775 /usr/local/stow
  4. Install stow
    Download the stow package with from ftp://mirrors.kernel.org/gnu/stow/stow-1.3.3.tar.gz. Then

    tar -zxf stow-1.3.3.tar.gz

    to extract everything. Go into the directory and then run the following.

    ./configure --prefix=/usr/local

    Then install it with make and sudo make install

    I considered installing stow under the stow directory but figured it was overkill

  5. Install ruby1.8.6
    Get the code with from ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.6-p287.tar.gz.  Unzip and cd into the directory. Run

    ./configure --prefix=/usr/local/stow/ruby-1.8.6-p287

    and then

    make && make install

    You shouldn’t need to sudo if you have the permissions correct. Run

    make install-doc

    if you want ri and rdoc, but it takes a while.

  6. Install ruby1.8.7
    Much like the 1.8.6 version, get the code with ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.7-p72.tar.gz Unzip and cd into the directory. Run

    ./configure --prefix=/usr/local/stow/ruby-1.8.7-p72

    and then

    make && make install

    Again run

    make install-doc

    if you want.

  7. Update your path
    Make sure that /usr/local/bin is on your path. Since we don’t have ruby installed anywhere else, you can put it at the end. You could put it earlier if you have other versions installed.

    echo $PATH

    to see if you already have it. If not, go to your .bashrc and add it where ever PATH is exported.

  8. Run stow
    Lets set up ruby 1.8.7. Change to the /usr/local/stow directory and run

    sudo stow ruby-1.8.7-p72

    All the symlinks are created for you.

  9. Install rubygems, gems and then stow again
    Running

    ruby -v

    should show you it is version 1.8.7. You will need to install rubygems now. Get it from http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz and then extract it into a directory. Change into that directory and run

    ruby setup.rb

    Now rubygems is installed in the ruby directory, but you will need run stow again. Change to /usr/local/stow and run

    sudo stow ruby-1.8.7-p87

    Running the command

    which gem

    should show you have it installed. You can now install gems like mongrel, rails etc. When you finish installing gems, be sure to run stow again so it will symlink the executables.

That’s it. Switching to ruby 1.8.6 is easy. Go to /usr/local/stow and run
stow -D ruby-1.8.7-p22 && stow -R ruby-1.8.6-p287

The -D removes all the symlinks.  Run the following to see all the options for stow

stow -h

Follow the instructions above to setup rubygems and all the gems again. Remember to rerun stow so the executable are symlinked. After you install the gems the first time, switching is simply a matter of stow -D and then stow -R. Pretty clean and easy.

A great thing about stow is I can use it for other packages as well, like mysql or php. I’ll note that ruby doesn’t have a

make uninstall

so it is ugly if you want to remove the packages. With stow, all you have to do is remove the directory.

Notes:
I tried to cover most commands I used. If you need more unix reference, try something like this or this. Google is your friend.

In addition to the articles linked above, I also read the following when writing this post one and two

Moving to Wordpress

Posted by mjwall on July 30, 2008

Alright, I give. Rails on shared hosting really sucks. I’ve seen reports of success with Phusion Passenger, but my host doesn’t support it yet. I wrote about my some of this issues I have had. So welcome to wordpress. Same theme, mostly, with less crashes. Maybe this will encourage me to write more.

Merb Notes

Posted by mjwall on April 02, 2008

Since my last post, I have been looking more and more at Merb. Just some random notes

  • Looks like Merb 0.9.2 got some fcgi love. That is what I want to try to replace rails on my shared host.
  • Get on IRC if you need help – irc.freenode.net#merb. Really, IRC.
  • If you can’t stay on IRC, go to http://groups.google.com/group/merb for a log
  • Use these sake tasks if you are running the latest. sudo sake merb:update merb:install will update core, more and plugins
  • merb-rspec is no longer in plugins, but included in core
  • Watch this from Confreaks. Worth the investment.
  • Most tutorials and articles are dated. This one helped though.

Looking forward to more from merb….

UPDATE: Having some trouble with typo’s caching. Going to repost this article.

New laptop

Posted by mjwall on March 05, 2008

One more quick post. Last week I purchased an inexpensive (shall I say cheap) laptop from WalMart. It is a Toshiba A215-S5808. I booted once in Vista to burn a cd and them immediately installed Ubuntu 7.10. Almost all my hardware was recognized, except the wireless and sound. This post was very helpful in resolving those issues. It’s only been a week, but I keep asking myself, “Why did I code so long in Windows”.