Paras
Paras

Reputation: 824

How to invoke non default target in grails gant script

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.

Upvotes: 4

Views: 1628

Answers (3)

Leo O'Donnell
Leo O'Donnell

Reputation: 213

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   
}

Upvotes: 3

chrislovecnm
chrislovecnm

Reputation: 2621

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.

Upvotes: 1

Burt Beckwith
Burt Beckwith

Reputation: 75671

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")

Upvotes: 3

Related Questions