Logo Search packages:      
Sourcecode: gant version File versions  Download package

void org::codehaus::gant::ant::Gant::execute (  )  throws BuildException [inline]

Load the file and then execute it

Definition at line 130 of file Gant.java.

References definitions, file, org::codehaus::gant::GantBuilder::invokeMethod(), and targets.

                                                          {
    if ( ! ( new File ( file ) ).exists ( ) ) { throw new BuildException ( "Gantfile does not exist." , getLocation ( ) ) ; }
    //
    //  To address the issues raised in GANT-50, we need to ensure that the org.apache.tools.ant.Project
    //  instance used by Gant is the one that initiated this task.  This means creating a new GantBinding
    //  with a specially prepared GantBuilder in order to create a suitable gant.Gant object.
    //
    //  NB  As this class is called Gant, we have to use fully qualified name to get to the other Gant class.
    //
    //  There seem to be various incomprehensible issues in attempting to use the original Project object so
    //  create a new one and set the basedir appropriately.
    //
    final Project newProject = new Project ( ) ;
    newProject.init ( ) ;
    newProject.setBaseDir ( getOwningTarget ( ).getProject ( ).getBaseDir ( ) ) ;
    final GantBuilder ant = new GantBuilder ( newProject ) ;
    final Map<String,String> environmentParameter = new HashMap<String,String> ( ) ;
    environmentParameter.put ( "environment" , "environment" ) ;
    //  Do not allow the output to escape.  The problem here is that if the output is allowed out then
    //  Ant, Gant, Maven, Eclipse and IntelliJ IDEA all behave slightly differently.  This makes testing
    //  nigh on impossible.  Also the user doesn't need to know about these.
    final PrintStream outSave = System.out ;
    System.setOut ( new PrintStream ( new ByteArrayOutputStream ( ) ) ) ;
    ant.invokeMethod ( "property" , new Object[] { environmentParameter } ) ;
    final GantBinding binding = new GantBinding ( ) ;
    binding.setVariable ( "ant" , ant ) ;
    binding.setVariable ( "Ant" , ant ) ; // TODO: deprecate and remove this.
    for ( Definition definition : definitions ) {
      final Map<String,String> definitionParameter = new HashMap<String,String> ( ) ;
      definitionParameter.put ( "name" , definition.getName ( ) ) ;
      definitionParameter.put ( "value" , definition.getValue ( ) ) ;
      ant.invokeMethod ( "property" , new Object[] { definitionParameter } ) ;
    }
    System.setOut ( outSave ) ;
    final gant.Gant gant = new gant.Gant ( binding ) ;
    gant.loadScript ( new File ( newProject.getBaseDir ( ) , file ) ) ;
    final List<String> targetsAsStrings = new ArrayList<String> ( ) ;
    for ( GantTarget g : targets ) { targetsAsStrings.add ( g.getValue ( ) ) ; }
    final int returnCode =  gant.processTargets ( targetsAsStrings ) ;
    if ( returnCode != 0 ) { throw new BuildException ( "Gant execution failed with return code " + returnCode + '.' , getLocation ( ) ) ; }
  }


Generated by  Doxygen 1.6.0   Back to index