SyntaxHighlighter

Thursday, September 29, 2011

grails (un)install-plugin not cleaning up properly

It could be just me, but some plugins for grails don't work so well. For one of my grails projects, I had maven-publisher installed. When I saw that the release plugin is to replace maven-publisher plugin in the future, I thought I'd try it out. For the record, I had maven-publisher 0.8.1 installed. So, I did:
grails uninstall-plugin maven-publisher
grails install-plugin release
All seemed ok. It installed release plugin 1.0.0.RC3. There I tried this:
grails maven-install
It gave me this NPE bug:
java.lang.NullPointerException: Cannot get property 'file' on null object
Thankfully this bug was already logged and a patch attached too. See http://jira.grails.org/browse/GPRELEASE-23 for more details. Anyway, I decided to go back to the old maven-publisher for now. So I did:
grails uninstall-plugin release
grails install-plugin maven-publisher
Now, I was trying to maven-install again, and I get this error:
[...]
Dependencies resolved in 2207ms.
Running script /home/tlee/.grails/1.3.7/projects/myhumbleproject/plugins/maven-publisher-0.8.1/scripts/MavenInstall.groovy
Error executing script MavenInstall: No such property: releasePluginDir for class: MavenInstall
groovy.lang.MissingPropertyException: No such property: releasePluginDir for class: MavenInstall
	at MavenInstall.run(MavenInstall:17)
	at MavenInstall$run.call(Unknown Source)
	at gant.Gant.prepareTargets(Gant.groovy:606)
Error executing script MavenInstall: No such property: releasePluginDir for class: MavenInstall
I tried this:
grails clean
grails war
It asked if I wanted to uninstall svn-1.0.0.M1 which was installed by release plugin before. I said yes, and grails war script continues and builds the war file. I run:
grails maven-install
and I again get the same error as before. I tried removing entire $HOME/.ivy2/cache folder, and I still got the same error. And then, I tried removing entire $HOME/.grails/1.3.7/projects/myhumbleproject/ folder. Then:
grails maven-install
resulted in grails asking me back for clarification.
Running pre-compiled script
Script 'MavenInstall' not found, did you mean:
1) UninstallPlugin
2) InstallPlugin
3) InstallDependency
4) InstallTemplates
5) Init
Please make a selection or enter Q to quit: Q
Ha! Ok. I ran:
grails war
Then:
grails maven-install
Finally, it worked! It seems a compiled script remains even after uninstalling the release plugin.

Thursday, August 25, 2011

print text-graph in haskell!

Recently, I've been fascinated by haskell. Here's my take on the same problem in the previous post solved in haskell.
print_chars_list :: [Int] -> Char -> IO ()
print_chars_list [] _ = putStr ""
print_chars_list (x:xs) c = do
    putStrLn (take x $ repeat c)
    print_chars_list xs c

-- example
print_chars_list [1,5,4,6,2] '."

Text-graph

/**
Problem: Given a list of positive Integers and a Char,
print the Char n times per line.
Eg. if I have [1,3,2,5] and '.' I want the output of:
.
...
..
.....
*/

print_chars = { l, c ->
  l.each { n ->
    for (int i = 0; i < n; i++) {
      print c
    }
    println ""
  }
}

// try using it.
print_chars([1,3,2,5], '.')

Thursday, July 28, 2011

join and split

You can join elements in a List to form one String, and you can take a String and split it and put them into a List.


def langs = ['haskell', 'python', 'groovy', 'java', 'php', 'c']
println langs.join(' > ') // my interest level at the moment.
def scripts = "JavaScript, coffeeScript, clojureScript"
// notice again that there's no trailing colon ":" in below. Yay!
println scripts.split(',').collect { it.trim() }.join(":")


For some this is trivial, for others, not-so-trivial.
When I first encountered join() and split() in perl during my uni days, I thought this toy-like function wouldn't be really useful in real world applications. How wrong I was.
I'm not sure why this functionality never made into JDK still. Apache Common Lang's StringUtils is helpful here, but not when that function is the only thing I needed. I mean, am I the only one who wrote something like the following over and over again?


StringBuffer sb = "";
for (String s : list) {
sb.append(s).append(":");
}
String result = sb.toString().substring(0, sb.length() - 1);

Saturday, July 23, 2011

Dynamic type casting

Oh, I need to be careful with dynamic types in groovy.

If you do calculations on a BigDecimal with a Double (or double), you will get a Double back, not a BigDecimal.
Try the following in a groovyConsole to confirm.

import java.math.RoundingMode

def price = 10.25g
def tax = 1.1g
def afterTax = price * tax
println afterTax.getClass() // class java.math.BigDecimal
println afterTax.setScale(2, RoundingMode.HALF_UP) // 11.28

double tax_d = 1.1d
def afterTax_d = price * tax_d
println afterTax_d.getClass() // class java.lang.Double
println afterTax_d.setScale(2, RoundingMode.HALF_UP) // Will throw groovy.lang.MissingMethodException

Friday, July 8, 2011

Simpler if condition and Truthy/Falsy

Coming from Java land, I'm enjoying the ability to write shorter code in groovy than in Java. The condition inside an if statement is one of those. Given cool is a boolean, in Java I'd do this.
if (cool == true) {
// do something cool
} else {
// do something hot
}
In groovy, I can do this.
if (cool) {
// do something cool
} else {
// do something hot
}
But it's good to remember what actually gets counted as true or false. Although I don't like using null as a value, I had to deal with one the other day. I had a return value returned from a service call, the service call itself returned either a String or null.
def something = oldService.tellMeSomething()

if (something == null) {
// I don't like null, but I have to live with it for now.
} else {
// work on something
}
I got excited and changed the code to the following.
def something = oldService.tellMeSomething() // same service call
if (!something) {
// I don't like null, but I have to live with it for now.
} else {
// work on something
}
Then, I suddenly had a hunch that this might be wrong. The thing was that an empty String is a legitimate value returned, and I wasn't sure if an empty String would be evaluated as true or false. So I ran the following in a groovyConsole.
def e = ""
def n = null

if (e) {
println "empty string is truthy"
} else {
println "empty string is falsy"
}

// check for the opposite case too for a good measure.
if (!e) {
println '(!empty string) is truthy'
} else {
println "(!empty string) is falsy"
}

if (n) {
println "null is truthy"
} else {
println "null is falsy"
}


// check for the opposite case too for a good measure.
if (!n) {
println "(!null) is truthy"
} else {
println "(!null) is falsy"
}
If you ran that, or you know groovy better than I do, you might be relieved to know that I reverted my change back to the original (and I sighed a little too for the use of null).

Thursday, July 7, 2011

Groovy Date clearTime() method

Groovy has a nice Date class defined.
One I learned to use recently is clearTime() method.
Date today = new Date().clearTime()
This should result to today's date with no time component attached to it.

But, my code kept on failing.
So I dug around a bit, and found this: http://jira.codehaus.org/browse/GROOVY-4788

I was on groovy 1.7.3, because my project's on grails 1.3.7. As you can see on the jira page, at this version of groovy, clearTime() does not have a return value.

Watch out for it if you are stuck!

After thought: groovy 1.8 seem to have very nice improvements over previous versions of groovy. I hope grails 1.4 will get released soon!

Friday, June 10, 2011

semi-colon: ';'

I'm not going to start with the usual "Hello, world!" example.
My beginning is more trivial than that: just a semi-colon.

;

Every Java statement has to end with ;.
In Groovy, I don't have to. I could, but I don't.
So, this works:

System.out.println("Look, no semicolon!")


It's a small change, but if you get used to it, it can also deter you from trying to put multiple statements in one line as you *might* have been doing in Java.
Don't try to be too clever and try to have multiple statements in one line. It doesn't always help.

Thursday, June 9, 2011

Groovy?

Ok, first things first.

This isn't about some groovy music band, dance moves, or anything like that.

I'm a software developer.

On this blog, I intend to log (much in the sense of a logbook) my journey in learning the programming language called, Groovy.

Comments are welcome, but this is my journey into learning groovy. You shouldn't use this as a manual to the groovy language.

Ok, I'm done for now.