net.sf.fikin.ant
Class EmbeddedAntProject

java.lang.Object
  extended by org.apache.tools.ant.Project
      extended by net.sf.fikin.ant.EmbeddedAntProject

public class EmbeddedAntProject
extends org.apache.tools.ant.Project

Embedded Ant Project into custom java applciation

Some times one would like to have an Ant Project (backed by some build.xml file) available into a custom java application. Embedding Ant is not strainght formward if one would like to take into account logging and build.xml initialization. For all such cases this class was made for.

This class provides:

Example:

      ClassLoader cl = this.getClass().getClassLoader();
      InputStream in = cl.getResourceAsStream( "some build.xml in classpath" );
      EmbeddedAntProject prj = new EmbeddedAntProject( binFile, "build-1.xml", in );
      
      prj.init();
      
      prj.executeTarget( prj.getDefaultTarget() );
      
      prj.setMessageLevel( Project.MSG_DEBUG );
      prj.executeTarget( "new-target" );
      
      String[][] params = new String[][] {
              { "property1", "value1" },
              { "property2", "value2" }
      };
      prj.executeTarget( "target2", params );
      
      ...
 
created on Aug 9, 2006

Since:
Version:
$Revision: 1.5 $
Author:
fiykov

Nested Class Summary
 class EmbeddedAntProject.NonClosingPS
          this is a print stream but non-closing handy when playing with tasks which happen to close stdout.
 
Field Summary
 
Fields inherited from class org.apache.tools.ant.Project
JAVA_1_0, JAVA_1_1, JAVA_1_2, JAVA_1_3, JAVA_1_4, MSG_DEBUG, MSG_ERR, MSG_INFO, MSG_VERBOSE, MSG_WARN, TOKEN_END, TOKEN_START
 
Constructor Summary
EmbeddedAntProject(java.io.File buildFile)
          Instantiate a new Ant project with given build file
EmbeddedAntProject(java.io.File baseDir, java.lang.String buildFileName, java.io.InputStream buildFileContent)
          Instantiate a new Ant project with given file name and content
 
Method Summary
 void executeTarget(java.lang.String target)
          execute given target
 void executeTarget(java.lang.String target, java.lang.String[][] params)
          new method execute given target with supplied properties
 void init()
          Initialize Ant project and make it ready for execution of targets.
protected  java.io.File makeItARealFile(java.io.InputStream in)
          copy the stream to a temp file this is a hack until more time avail for a diff solution
 void setMessageLevel(int level)
          set logging level for the default logger
 
Methods inherited from class org.apache.tools.ant.Project
addBuildListener, addDataTypeDefinition, addFilter, addOrReplaceTarget, addOrReplaceTarget, addReference, addTarget, addTarget, addTaskDefinition, checkTaskClass, copyFile, copyFile, copyFile, copyFile, copyFile, copyFile, copyFile, copyFile, copyInheritedProperties, copyUserProperties, createClassLoader, createDataType, createTask, defaultInput, demuxFlush, demuxInput, demuxOutput, executeSortedTargets, executeTargets, fireBuildFinished, fireBuildStarted, fireMessageLogged, fireMessageLogged, fireMessageLogged, fireSubBuildFinished, fireSubBuildStarted, fireTargetFinished, fireTargetStarted, fireTaskFinished, fireTaskStarted, getBaseDir, getBuildListeners, getCoreLoader, getDataTypeDefinitions, getDefaultInputStream, getDefaultTarget, getDescription, getElementName, getExecutor, getFilters, getGlobalFilterSet, getInputHandler, getJavaVersion, getName, getProperties, getProperty, getReference, getReferences, getTargets, getTaskDefinitions, getThreadTask, getUserProperties, getUserProperty, initSubProject, isKeepGoingMode, log, log, log, log, registerThreadTask, removeBuildListener, replaceProperties, resolveFile, resolveFile, setBaseDir, setBasedir, setCoreLoader, setDefault, setDefaultInputStream, setDefaultTarget, setDescription, setExecutor, setFileLastModified, setInheritedProperty, setInputHandler, setJavaVersionProperty, setKeepGoingMode, setName, setNewProperty, setProjectReference, setProperty, setSystemProperties, setUserProperty, toBoolean, topoSort, topoSort, topoSort, translatePath
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EmbeddedAntProject

public EmbeddedAntProject(java.io.File buildFile)
                   throws org.apache.tools.ant.BuildException
Instantiate a new Ant project with given build file

Parameters:
buildFile -
Throws:
org.apache.tools.ant.BuildException

EmbeddedAntProject

public EmbeddedAntProject(java.io.File baseDir,
                          java.lang.String buildFileName,
                          java.io.InputStream buildFileContent)
                   throws org.apache.tools.ant.BuildException
Instantiate a new Ant project with given file name and content

Parameters:
baseDir -
buildFileName -
buildFileContent -
Throws:
org.apache.tools.ant.BuildException
Method Detail

init

public void init()
          throws org.apache.tools.ant.BuildException
Initialize Ant project and make it ready for execution of targets.

Overrides:
init in class org.apache.tools.ant.Project
Throws:
org.apache.tools.ant.BuildException

makeItARealFile

protected java.io.File makeItARealFile(java.io.InputStream in)
                                throws org.apache.tools.ant.BuildException
copy the stream to a temp file this is a hack until more time avail for a diff solution

Parameters:
in -
Returns:
a temporary file with the content of that stream
Throws:
java.io.IOException
org.apache.tools.ant.BuildException

setMessageLevel

public void setMessageLevel(int level)
set logging level for the default logger

Parameters:
level - as defined by Project.MSG_? constants

executeTarget

public void executeTarget(java.lang.String target)
                   throws org.apache.tools.ant.BuildException
execute given target

Overrides:
executeTarget in class org.apache.tools.ant.Project
Throws:
java.io.FileNotFoundException
org.apache.tools.ant.BuildException

executeTarget

public void executeTarget(java.lang.String target,
                          java.lang.String[][] params)
                   throws org.apache.tools.ant.BuildException
new method execute given target with supplied properties

Throws:
java.io.FileNotFoundException
org.apache.tools.ant.BuildException


Copyright © 2003-2007 Nikolay Fiykov. All Rights Reserved.