ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Blaine Kendall" <blainenewslett...@yahoo.com>
Subject Re: Calling Ant from Java: An example please ?
Date Thu, 14 Feb 2002 21:16:27 GMT
I ended up solving my problem. Hopefully this will help others as well.

I added  <property environment="env" /> into my build.xml and then I added
another line pass that environment variable into my target. (I had to pass
in my Path since it could never find vb6.program.dir if it couldn't access
the system path). Also, note that there is a difference between "Path" and
"PATH"

 <target name="build">
  <echo message="Building file" />
  <exec dir="${vb6.program.dir}" executable="vb6.exe" failonerror="yes">
   <env key="Path" value="${env.Path}"/>
   <arg line="/m ${src.dir}\HelloWorld.vbp /outdir ${build.dir}"/>
  </exec>
 </target>

try to harness your properties like this.

I couldn't figure out how to grab the classpath. If anyone can figure that
one out, let me know.

Thanks. Blaine.

----- Original Message -----
From: "Blaine Kendall" <blainenewsletters@yahoo.com>
To: "Ant Users List" <ant-user@jakarta.apache.org>
Cc: <blainenewsletters@yahoo.com>
Sent: Wednesday, February 13, 2002 11:46 PM
Subject: Re: Calling Ant from Java: An example please ?


> I'm having the same problem. I'm building a web front end to start our
> builds. I have existing Ant scripts which run fine from the command line
and
> the path settings include everything needed.
>
> My problem is when I try to run the same ant scripts by calling ant from
> java, the path & classpath environment variables are lost. When I try to
> compile java, the classpath is incomplete. When I try to build VB, the
path
> to VB6.exe cannot be found. It works on other targets within the script
such
> as moving or deleting files, but anything involving environment variables
> doesn't work.
>
> Here's what I'm doing. I have a jsp page which, when initialized, creates
> ControlBean, reads the path to the ant build file out of web.xml, sets
that
> path to the ControlBean (setBuildXmlFile). On submitting the page,
> ControlBean.startBuild() is called.
>
> Someone please show me where in my java code that I can somehow add to
both
> my PATH and CLASSPATH variables. I can sympathize with Jay... the API docs
> are terrible for describing what each method is for. I've seen about a
half
> dozen methods regarding *property* and tried many combinations trying to
get
> them to work, but nothing does.
>
> Has anyone ever gotten Ant to compile anything from Java?
>
> -----------------index.jsp---------------------
> <%@ page language="java" contentType="text/html" %>
> <%@ page import="antweb.*" %>
> <html>
>   <head>
>     <title>Build Starter</title>
>   </head>
>   <body>
>     <jsp:useBean id="cb" scope="session" class="antweb.ControlBean"/>
>     <br>
>   <form action="kickoff.jsp" method="post">
>   <%
>     String buildFileName = application.getInitParameter("buildfile");
>     cb.setBuildXmlFile(buildFileName);
>     %>
>   <input type="hidden" name="buildXmlFile" value="<%= cb.getBuildXmlFile()
> %>" />
>   <input type="submit" value="Start build">
>   </form>
>   </body>
> </html>
> -----------------ControlBean.java---------------------
> package antweb;
>
> import java.io.Serializable;
> import javax.servlet.ServletConfig;
> import javax.servlet.ServletContext;
> import java.io.File;
> import org.apache.tools.ant.Project;
> import org.apache.tools.ant.ProjectHelper;
> import org.apache.tools.ant.taskdefs.Property;
> import org.apache.tools.ant.BuildException;
> import org.apache.log4j.PropertyConfigurator;
>
> public class ControlBean implements Serializable{
>
>    // Public constants
>   private String buildXmlFile;
>   private String buildTarget = "all";
>   private boolean buildRunning = false;
>
>   // Public Methods
>   public void startBuild() {
>     System.out.println("creating a new project");
>     Project pj = new Project();
>     System.out.println("init the project");
>     pj.init();
>     System.out.println("setting the properties");
>     try {
>       System.out.println("trying to configure project");
>       System.out.println("xmlFile is: "+ buildXmlFile );
>       File bldFile = new File (getBuildXmlFile());
>       System.out.println("bldFile has been created");
>       ProjectHelper.configureProject(pj, bldFile);
>       System.out.println("project has been configured");
>     } catch (BuildException be) {
>     System.out.println("Build EXCEPTION!");
>     }
>     System.out.println("about to executeTarget :_" +
this.getBuildTarget());
>     pj.executeTarget(this.getBuildTarget());
>     System.out.println("target done...build starting...");
>     setBuildRunning(true);
>   }//startBuild
>  .
>  .
>  .
>   public String getBuildXmlFile() {
>     return buildXmlFile;
>   }//getBuildXmlFile
>
>   public void setBuildXmlFile(String buildXmlFile) {
>     this.buildXmlFile = buildXmlFile;
>   }//setBuildXmlFile
>
> }//ControlBean
>
> ----- Original Message -----
> From: "Jay Riddell" <jay@theriddells.org>
> To: "Ant Users List" <ant-user@jakarta.apache.org>; "Kevin Toomey"
> <toomeykh@yahoo.com>
> Sent: Wednesday, February 13, 2002 2:57 PM
> Subject: Re: Calling Ant from Java: An example please ?
>
>
> > Yes, I am TRYING to do that.
> > But I am failing miserably.
> > There are NO EXAMPLES on how to do this; there is only Javadoc
> > on the API itself (with very little extra explanatory text).
> >
> > OK, maybe I am not being specific enough here.
> >
> > Here is the java code that I have written that attempts
> > to compile a "Hello, world" java file:
> > http://home.attbi.com/~theriddells/CompileIt.java
> >
> > And here are the results that I get:
> > http://home.attbi.com/~theriddells/results.txt
> >
> > (Sorry for the links, I felt they were too long to be
> > directly quoted here.)
> >
> > Thoughts ?  Help ?
> >
> > Thanks
> >
> > On 2/13/02 11:15 AM, "Kevin Toomey" <toomeykh@yahoo.com> wrote:
> >
> > > It sounds like you want to take a look at the org.apache.tools.ant
> > > package in the API. Using the Project, Target, and Task classes, you
> > > could implement Diane's example via a Java program.
> > >
> > > --- Jay Riddell <jay@theriddells.org> wrote:
> > >> Thanks for the quick response  . . . but no ;-)
> > >>
> > >> I want to accomplish this in Java by calling the Ant API.
> > >>
> > >> I need to dynamically:
> > >>   - generate source code
> > >>   - compile what was generated
> > >>   - classload it
> > >>   - execute it
> > >>
> > >> And I need to do all this as part of a larger application.
> > >>
> > >> How's THAT for fun ?
> > >>
> > >> On 2/13/02 10:47 AM, "Diane Holt" <holtdl@yahoo.com> wrote:
> > >>
> > >>> --- Jay Riddell <jay@theriddells.org> wrote:
> > >>>> I have an application where I need to dynamically generate Java
> > >> source
> > >>>> code, compile it, load it and then execute that code.
> > >>> [snip]
> > >>>> However, I'm sure that I'm not doing anything really
> > >> difficult...I'm
> > >>>> just doing it wrong ;-).  A "compile Hello World"-type example
> > >> would
> > >>>> save me BUNCHES of work and would be MUCH appreciated.
> > >>>
> > >>> If I understand right what you want to do (but given the past two
> > >> days,
> > >>> that's anybody's guess :), here's an example:
> > >>>
> > >>> <target name="runHello" depends="gensrc,compile">
> > >>>   <java classname="HelloWorld" >
> > >>>     <classpath>
> > >>>       <pathelement location="."/>
> > >>>     </classpath>
> > >>>   </java>
> > >>> </target>
> > >>>
> > >>> <target name="compile">
> > >>>   <javac srcdir="." destdir="." includes="HelloWorld.java"/>
> > >>> </target>
> > >>>
> > >>> <target name="gensrc">
> > >>>   <echo file="HelloWorld.java">public class HelloWorld
> > >>> {
> > >>> public static void main (String [] args)
> > >>> {
> > >>>     System.out.println("Hello, world") ;
> > >>> }
> > >>> }
> > >>>   </echo>
> > >>> </target>
> > >>>
> > >>> Diane
> > >>>
> > >>> =====
> > >>> (holtdl@yahoo.com)
> > >>>
> > >>>
> > >>>
> > >>> __________________________________________________
> > >>> Do You Yahoo!?
> > >>> Send FREE Valentine eCards with Yahoo! Greetings!
> > >>> http://greetings.yahoo.com
> > >>>
> > >>> --
> > >>> To unsubscribe, e-mail:
> > >> <mailto:ant-user-unsubscribe@jakarta.apache.org>
> > >>> For additional commands, e-mail:
> > >> <mailto:ant-user-help@jakarta.apache.org>
> > >>>
> > >>
> > >>
> > >> --
> > >> To unsubscribe, e-mail:
> > >> <mailto:ant-user-unsubscribe@jakarta.apache.org>
> > >> For additional commands, e-mail:
> > >> <mailto:ant-user-help@jakarta.apache.org>
> > >>
> > >
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Send FREE Valentine eCards with Yahoo! Greetings!
> > > http://greetings.yahoo.com
> > >
> > > --
> > > To unsubscribe, e-mail:
> <mailto:ant-user-unsubscribe@jakarta.apache.org>
> > > For additional commands, e-mail:
> <mailto:ant-user-help@jakarta.apache.org>
> > >
> >
> >
> > --
> > To unsubscribe, e-mail:
<mailto:ant-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
<mailto:ant-user-help@jakarta.apache.org>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


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


Mime
View raw message