4

I have a gant script A with two targets

t1 - default target t2 - another target

Even when I run

grails A t2

the default target is run? How can I run the non-default target? I have tried grails A --target='t2' etc. but doesn't work.

Paras
  • 804
  • 1
  • 10
  • 25
  • you ever figure this out? I am trying to have one script file and be able to run multiple different targets – chrislovecnm Oct 10 '11 at 18:49
  • Sorry I couldn't figure that out. It was long time back so I don't remember exactly what I did. But as far as I remember I followed Burt's advice of creating another script. – Paras Oct 18 '11 at 17:42

3 Answers3

3

I'm not sure if there's a proper way to do it, but you can write a second script ("T2.groovy") that loads this one and sets that target as its default, e.g.

includeTargets << new File("path/to/YourScript")

setDefaultTarget("t2")
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • Thanks Burt. That's what I did for now. I have created two scripts T1.groovy and T2.groovy – Paras Jun 28 '10 at 02:51
3

A tweak to argsParsing approach is to run through elements from the argsMap and iteratively depend on them. So you could call your script something like:

grails myScript do-this do-that do-the-other

scriptName = 'myScriptName'    
includeTargets << grailsScript("_GrailsArgParsing")

snip

target(main: "Default Target") {
  depends(parseArguments)
  if(argsMap?.size() == 0) {
    depends(scriptError)
  }
  argsMap.each() {
    if (it.value) {
       println "${scriptName} building: ${it.value}"
       depends(it.value)
    }
    else {
       depends(scriptError)
    }
  }
}

snip

target(help: "Print a help message") {
   println "${scriptName}: possible targets are..."
   println "\thelp - print this help message" 
}   

target(scriptError: "Print an error and die") {
   println "${scriptName}: Please specify at least one target name"
   depends(help) 
   exit 1   
}
Leo O'Donnell
  • 203
  • 3
  • 8
1

This is another approach that I took

includeTargets << grailsScript("_GrailsArgParsing")

snip

target(main: "a script") { 
    if(!argsMap.target)
        throw new IllegalArgumentException("please specify target name with --target option")

   depends(argsMap.target)
}

setDefaultTarget(main)

You run the script with a parameter. That parameter is the name of the method to run :) That method then get's executed.

chrislovecnm
  • 2,549
  • 3
  • 20
  • 36