ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "John Birtley" ...@buildmonkey.com>
Subject Re: Best way to run Ant without installing it?
Date Fri, 17 Sep 2004 11:14:40 GMT
If ant.jar is in your CLASSPATH, then you can invoke a build using the
following class.

We use this for very complex builds where we need a lot of iteration (or
other funkiness).

------------------------------- SNIP ----------------------------
    /*
     * AntBuild.java
     *
     * John Birtley <jb@buildmonkey.com>
     */

    import java.io.File;
    import java.io.PrintStream;
    import java.io.FileOutputStream;
    import java.util.Vector;
    import java.util.Properties;

    // Ant imports
    import org.apache.tools.ant.Project;
    import org.apache.tools.ant.ProjectHelper;
    import org.apache.tools.ant.XmlLogger;
    import org.apache.tools.ant.Main;

    /**
     * Kick off an Ant build from within a Java VM.
     *
     * @author  <a href='mailto:john@buildmonkey.com'>John Birtley</a>
     */
    public class AntBuild {

        /**
         * The Ant project
         */
        private Project project;

        /**
         * The build file
         */
        private File buildfile;

        /**
         * The targets
         */
        private Vector targets;

        /**
         * The properties
         */
        private Properties props;

        /**
         * The log file
         */
        private File logfile;


        /**
         * Creates a new instance of AntBuild
         */
        public AntBuild( String buildfile) throws BuildEngineException {
            this( new File( buildfile));
        }


        /**
         * Creates a new instance of AntBuild
         */
        public AntBuild( File buildfile) throws BuildEngineException {
            try {
                /*
                 * Get the build file
                 */
                this.buildfile = buildfile;
                if (! this.buildfile.exists()) {
                    throw new BuildEngineException( "Build file '" +
this.buildfile.getAbsolutePath() + "' does not exist");
                }

                /*
                 * Instantiate the project
                 */
                this.project = new Project();

                /*
                 * Set the base directory of the project
                 */
                project.setBaseDir( buildfile.getParentFile());

                /*
                 * Set the properties
                 */
                this.props = new Properties();

                /*
                 * Set the targets
                 */
                this.targets = new Vector();

                /*
                 * Log file for the project
                 */
                logfile = File.createTempFile( this.getClass().getName(),
".xml");
            }
            catch( Exception e) {
                throw new BuildEngineException( e);
            }
        }


        /**
         * Add a target
         */
        public void addTarget( String target) {
            this.targets.add( target);
        }


        /**
         * Set the base directory
         */
        public void setBasedir( String basedir) {
            project.setBaseDir( new File( basedir));
        }


        /**
         * Set the base directory
         */
        public void setBasedir( File basedir) {
            project.setBaseDir( basedir);
        }


        /**
         * Set a property
         */
        public void setProperty( String prop, String value) {
            props.setProperty( prop, value);
        }


        /**
         * Set a property that is specified in '-Dfoo=bar' format
         */
        public void setProperty( String property) {
            int equalsPos = property.indexOf( '=');
            if (equalsPos > 0) {
                String prop = property.substring( 0, equalsPos);
                String value = property.substring( equalsPos + 1);

                /*
                 * Remove any '-d'
                 */
                int dpos = property.toLowerCase().indexOf( "-d");
                if (dpos == 0) {
                    prop = prop.substring( 2);
                }

                props.setProperty( prop, value);
            }
        }


        /**
         * Get the log file
         */
        public File getLogfile() {
            return( this.logfile);
        }


        /**
         * Set the log file
         */
        public void setLogfile( File logfile) {
            this.logfile = logfile;
        }


        /**
         * Set the log file
         */
        public void setLogfile( String logfile) {
            this.logfile = new File( logfile);
        }


        /**
         * Perform the build
         */
        public void doBuild() throws BuildEngineException {
            try {
                /*
                 * Set the properties
                 */
                project.setUserProperty( "ant.file",
buildfile.getAbsolutePath());
                project.setUserProperty( "ant.version",
Main.getAntVersion());
                project.setUserProperty( "project.name", "Project Name");
                project.setUserProperty( "build.name", "Build Name");
                project.setUserProperty( "schedule.name", "Schedule Name");

                /*
                 * Initialise the project
                 */
                project.init();

                /*
                 * Now parse the build file
                 */
                ProjectHelper.configureProject( project, buildfile);

                /*
                 * Set all of the properties
                 */
                for (java.util.Enumeration e = props.keys();
(e.hasMoreElements()); ) {
                    String name = (String) e.nextElement();
                    project.setUserProperty( name, props.getProperty(
name));
                    project.setProperty( name, props.getProperty( name));
                    project.setNewProperty( name, props.getProperty( name));
                }

                /*
                 * Execute the default target
                 */
                project.fireBuildStarted();
                if (targets.size() == 0) {
                    /*
                     * No targets specified, execute the default
                     */
                    project.executeTarget( project.getDefaultTarget());
                }
                else {
                    project.executeTargets( targets);

                }
                project.fireBuildFinished( null);
            }
            catch( Exception e) {
                /*
                 * Something went wrong
                 */
                project.fireBuildFinished( e);
                throw new BuildEngineException( e);
            }
        }
    }



    class BuildEngineException extends Exception {

     public BuildEngineException( String msg) {
      super( msg);
     }

     public BuildEngineException( Exception cause) {
      super( cause);
     }
    }

------------------------------- SNIP ----------------------------

----- Original Message ----- 
From: <jfuller@ruminate.co.uk>
To: "Ant Users List" <user@ant.apache.org>
Sent: Friday, September 17, 2004 11:31 AM
Subject: Re: Best way to run Ant without installing it?


> Emmanouil Batsis <Emmanouil.Batsis@eurodyn.com> wrote ..
> > Hi Ivan,
> > Thanks for your reply.
> >
> > Ivan Ivanov wrote:
> >
> > >A simple scenario will be to extract ant to a
> > >directory point by user, keep the name of that
> > >directory as a (environment) variable say
> > >ANT_INSTALL_DIR and later invoke it from your script
> > >files as $ANT_INSTALL/bin/ant.
> > >
>
> w/o sending you on a wild goose chase...I think I remember doing something
like this using Jelly as the invoker of Ant tasks.....
>
> gl and post your solution if/when u get it !
>
> -Jim Fuller
>
>


----------------------------------------------------------------------------
----


> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message