Targets and Parameters

Groovy Closures have a single parameter by default even if you don't specify one.  This means that the Gant target:

target ( example : '' ) { println ( it ) }

works fine because it is the name of the implicit parameter.  When executed the above will in fact print out null since from the command line the implicit parameter is uninitialized and hence has value null.  So what use is this for Gant scripts?  Many targets are called from within Gant scripts as well as being targets.  So with the Gant script:

target ( example : '' ) { println ( it ) }
target ( callit : '' ) { example ( 'fred' ) }

Now if we execute the target callit, we get the output fred since the implicit parameter is initialized.

If you want to explicitly name the parameter instead of using it, the usual Groovy syntax suffices:

target ( example : '' ) { parameter -> println ( parameter ) }

If you want to enforce there being no parameter then again the usual Groovy idiom applies:

target ( example : '' ) { -> println ( 'no parameter in this closure.' ) }

Attempting to have more than one parameter does not work.  So the following will result in a error:

target ( example : '' ) { a , b , c -> println ( b ) }

The error reported in this situation, is somewhat arcane and deeply impenetrable.  This is due to the internals of the way Gant works.  Work is in progress to try and make the error report far more meaningful.

If you need to pass more than one value as a parameter there is an easy option, pass a list of values:

target ( example : '' ) {
  if ( it instanceof List ) { for ( item in it ) { println ( item ) } }
  else { println ( item ) }
}

The rest is really up to how the targets are programmed as to what can and cannot be achieved.  Remember though when the target is called from the command line the parameter always has the value null.  Only when a target is executed from within Gant script code is this parameter facility available.

Labels

 
(None)