I'm trying to write a script that uses CliBuilder. When I wrote the initial
skeleton, I just had the following:
------------
#! env groovy
import groovy.util.CliBuilder
def cli = new CliBuilder(usage: usage())
cli.help(usage())
def options = cli.parse(args)
def usage() {
"blahblah ..."
}
------------
When I execute this with "groovy ./scriptname.groovy --help", it just
immediately returned to the shell prompt. I also did "chmod +x
scriptname.groovy" and then ran "./scriptname.groovy --help". Curiously, this
didn't immediately return to the prompt, and it didn't fail, it just sat there
seemingly forever, perhaps waiting for input. I eventually ^Ced it. I tried
this many times, with the same result.
Note that I'm running this on Cygwin, with groovy 2.4.7.
When I ran this in Eclipse Neon, with the latest groovy-eclipse snapshot, it
immediately gave me this:
------------------
Caught: java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at codecloudUserValidation.run(codecloudUserValidation.groovy:8)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.cli.ParseException
... 1 more
------------------
This might be a problem with groovy-eclipse, hard to tell.
Despite the difference, it does appear that I need commons-cli, so I changed
the file like this:
---------------
#! env groovy
@Grab("commons-cli:commons-cli:1.3.1")
------------
This presented some challenges, as I needed to set the proxy properties, but as
far as I can tell, the only way to set those properties is by doing either of
these two things:
* Running "groovy -Dhttp.... scriptname args"
* Setting JAVA_OPTS to include "-Dhttp...."
I don't want to do either of those things.
I tried changing the shebang line, adding "-Dhttp..." after "env groovy", which
I guess would only be used if I could get the "./scriptname.groovy ..." to
work, as opposed to "groovy ./scriptname.groovy ...".
In any case, I did end up manually running it with "groovy -Dhttp...
./scriptname.groovy" (along with setting some grab/ivy verbose properties), and
watched it download commons-cli. I verified the jar ended up in
"$HOME/.groovy/grapes/commons-cli/commons-cli/jars/commons-cli-1.3.1.jar".
Unfortunately, this seems to have no effect. Running it in Eclipse still fails
with the same error (do I need some sort of "refresh" in Eclipse to reload that
tree?), and running it from the shell prompt still behaves the same as before.
I also tried adding a println right after the last import and also right before
the end of the "straight-line" block, before method definitions. When I run it
from the shell with "groovy ./scriptname.groovy ...", it prints both messages,
along with my "usage()" return value. When I run it with "./scriptname.groovy
...", it hangs as before, without printing anything. When I run it from
Eclipse, it prints the "start" message, prints the exception and exits.