You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Marc Farrow <ma...@gmail.com> on 2006/08/16 14:26:13 UTC

dev-list - Calling Ant within a servlet/webapp

I have some servlet and I am trying to call an Ant script within the class
to compile a "project".  All of the references in the build script are not
relative, therefore some of the classpath references are not correct.  I
know I can change the properties, etc to fix this.  However, due to the
nature of our environment we do not want to do this.  With all that being
said, I do know that all the classpath entries that I need to compile the
needed classes are within my current servlet container (in my case Tomcat).
What can I do retrieve the correct classloader so that all the classes are
visible to the Ant script.  Below is a snippet of code I am using.  In
comments, you will see the various ways I have tried to call the
setCoreLoader method.

Thank you,

File buildfile = null;
try {
     buildfile = new File(this.workingDirectory.getPath() + "/build.xml");
     Project project = new Project();
     project.setBaseDir(this.workingDirectory);
     project.setName(this.project);
     project.setUserProperty("ant.file", buildfile.getPath());
     project.init();
     ProjectHelper helper = ProjectHelper.getProjectHelper();
     project.addReference("ant.projectHelper", helper);
     helper.parse(project, buildfile);
     Properties sysProps = System.getProperties();
     for (Iterator it = sysProps.entrySet().iterator();it.hasNext();) {
  Map.Entry entry = (Map.Entry) it.next();
  project.setProperty(entry.getKey().toString(), entry.getValue
().toString());
     }
     project.setProperty("basedir", project.getBaseDir().getPath());
     project.setProperty("ant.project.name", project.getName());
     project.setProperty("ant.file", buildfile.getPath());
     project.setCoreLoader(Thread.currentThread().getContextClassLoader());
//     project.setCoreLoader(null);
//     project.setCoreLoader(this.getClass().getClassLoader());
//     project.setCoreLoader(Thread.currentThread
().getContextClassLoader().getParent());
     project.execute("dist");
} catch (Exception e) {
     throw new RuntimeException(e);
} finally {
     project = null;
     buildfile = null;
}


-- 
Marc Farrow

Re: dev-list - Calling Ant within a servlet/webapp

Posted by Marc Farrow <ma...@gmail.com>.
Because the build files are not maintained by us.  We just use them.

On 8/17/06, Kev Jackson <fo...@gmail.com> wrote:
>
>
> On 16 Aug 2006, at 19:26, Marc Farrow wrote:
>
> > I have some servlet and I am trying to call an Ant script within
> > the class
> > to compile a "project".  All of the references in the build script
> > are not
> > relative, therefore some of the classpath references are not
> > correct.  I
> > know I can change the properties, etc to fix this.  However, due to
> > the
> > nature of our environment we do not want to do this.  With all that
> > being
> > said, I do know that all the classpath entries that I need to
> > compile the
>
> Is there a reason why you don't want to use the Ant API by itself?
> Why do you need to load the build.xml file?  It seems that the real
> problem you have is that programmatically Ant doesn't have the same
> relative references as Ant running a build file, so my advice is to
> remove the problem (build file) and simply use the Ant jar as an API
> and have your rules specified in Java instead of XML.
>
> Kev
>
> --
> "That government is best which governs not at all" - Henry Thoreau
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>


-- 
Marc Farrow

Re: dev-list - Calling Ant within a servlet/webapp

Posted by Kev Jackson <fo...@gmail.com>.
On 16 Aug 2006, at 19:26, Marc Farrow wrote:

> I have some servlet and I am trying to call an Ant script within  
> the class
> to compile a "project".  All of the references in the build script  
> are not
> relative, therefore some of the classpath references are not  
> correct.  I
> know I can change the properties, etc to fix this.  However, due to  
> the
> nature of our environment we do not want to do this.  With all that  
> being
> said, I do know that all the classpath entries that I need to  
> compile the

Is there a reason why you don't want to use the Ant API by itself?   
Why do you need to load the build.xml file?  It seems that the real  
problem you have is that programmatically Ant doesn't have the same  
relative references as Ant running a build file, so my advice is to  
remove the problem (build file) and simply use the Ant jar as an API  
and have your rules specified in Java instead of XML.

Kev

--
"That government is best which governs not at all" - Henry Thoreau


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


Re: dev-list - Calling Ant within a servlet/webapp

Posted by Marc Farrow <ma...@gmail.com>.
Anyone have any clues about adding items to the general classpath?

What about even just adding classpath entries without a target (such as
<javac/>)


On 8/16/06, Marc Farrow <ma...@gmail.com> wrote:
>
>  I checked out the link.  Anthill is just "spawning" a process to run the
> Ant Executable for the specified platform.  So they are not programmatically
> using ant as I want to do.  I want my solution to be a Java solution.
>
> Anyone else have any other ideas?
>
> Thank you,
>
> Marc
>
>
>  On 8/16/06, Marc Farrow <ma...@gmail.com> wrote:
> >
> > Thanks for the tip Tom.  I will definitely check it out.
> >
> >
> > On 8/16/06, Tom Cunningham <cu...@mac.com> wrote:
> > >
> > >
> > > Have you taken a look at Anthill (http://www.urbancode.com/projects/anthill/default.jsp )?
> > > There's an open source version which you can hack up, but it seems like it
> > > does the same thing that you are trying to do here (kick off ant from the
> > > web).     If you do need to do this, you might want to check the code in
> > > their open source version for how they handle their classloaders.
> > >
> > > On Wednesday, August 16, 2006, at 08:27AM, Marc Farrow <
> > > marc.farrow@gmail.com> wrote:
> > >
> > > >I have some servlet and I am trying to call an Ant script within the
> > > class
> > > >to compile a "project".  All of the references in the build script
> > > are not
> > > >relative, therefore some of the classpath references are not
> > > correct.  I
> > > >know I can change the properties, etc to fix this.  However, due to
> > > the
> > > >nature of our environment we do not want to do this.  With all that
> > > being
> > > >said, I do know that all the classpath entries that I need to compile
> > > the
> > > >needed classes are within my current servlet container (in my case
> > > Tomcat).
> > > >What can I do retrieve the correct classloader so that all the
> > > classes are
> > > >visible to the Ant script.  Below is a snippet of code I am
> > > using.  In
> > > >comments, you will see the various ways I have tried to call the
> > > >setCoreLoader method.
> > > >
> > > >Thank you,
> > > >
> > > >File buildfile = null;
> > > >try {
> > > >     buildfile = new File(this.workingDirectory.getPath () +
> > > "/build.xml");
> > > >     Project project = new Project();
> > > >     project.setBaseDir(this.workingDirectory);
> > > >     project.setName(this.project);
> > > >     project.setUserProperty("ant.file ", buildfile.getPath());
> > > >     project.init();
> > > >     ProjectHelper helper = ProjectHelper.getProjectHelper();
> > > >     project.addReference("ant.projectHelper", helper);
> > > >     helper.parse (project, buildfile);
> > > >     Properties sysProps = System.getProperties();
> > > >     for (Iterator it = sysProps.entrySet().iterator();it.hasNext();)
> > > {
> > > >  Map.Entry entry = (Map.Entry) it.next();
> > > >  project.setProperty (entry.getKey().toString(), entry.getValue
> > > >().toString());
> > > >     }
> > > >     project.setProperty ("basedir", project.getBaseDir().getPath());
> > > >     project.setProperty(" ant.project.name", project.getName());
> > > >     project.setProperty("ant.file ", buildfile.getPath());
> > > >     project.setCoreLoader(Thread.currentThread
> > > ().getContextClassLoader());
> > > >//     project.setCoreLoader(null);
> > > >//     project.setCoreLoader(this.getClass().getClassLoader());
> > > >//     project.setCoreLoader(Thread.currentThread
> > > >().getContextClassLoader().getParent());
> > > >     project.execute ("dist");
> > > >} catch (Exception e) {
> > > >     throw new RuntimeException(e);
> > > >} finally {
> > > >     project = null;
> > > >     buildfile = null;
> > > >}
> > > >
> > > >
> > > >--
> > > >Marc Farrow
> > > >
> > > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: dev-help@ant.apache.org
> > >
> > >
> >
> >
> > --
> >
> > Marc Farrow
> >
>
>
>
> --
>
> Marc Farrow
>



-- 
Marc Farrow

Re: dev-list - Calling Ant within a servlet/webapp

Posted by Marc Farrow <ma...@gmail.com>.
I checked out the link.  Anthill is just "spawning" a process to run the Ant
Executable for the specified platform.  So they are not programmatically
using ant as I want to do.  I want my solution to be a Java solution.

Anyone else have any other ideas?

Thank you,

Marc


On 8/16/06, Marc Farrow <ma...@gmail.com> wrote:
>
> Thanks for the tip Tom.  I will definitely check it out.
>
>
> On 8/16/06, Tom Cunningham <cu...@mac.com> wrote:
> >
> >
> > Have you taken a look at Anthill (http://www.urbancode.com/projects/anthill/default.jsp
> > )?   There's an open source version which you can hack up, but it seems
> > like it does the same thing that you are trying to do here (kick off ant
> > from the web).     If you do need to do this, you might want to check the
> > code in their open source version for how they handle their classloaders.
> >
> > On Wednesday, August 16, 2006, at 08:27AM, Marc Farrow <
> > marc.farrow@gmail.com> wrote:
> >
> > >I have some servlet and I am trying to call an Ant script within the
> > class
> > >to compile a "project".  All of the references in the build script are
> > not
> > >relative, therefore some of the classpath references are not
> > correct.  I
> > >know I can change the properties, etc to fix this.  However, due to the
> >
> > >nature of our environment we do not want to do this.  With all that
> > being
> > >said, I do know that all the classpath entries that I need to compile
> > the
> > >needed classes are within my current servlet container (in my case
> > Tomcat).
> > >What can I do retrieve the correct classloader so that all the classes
> > are
> > >visible to the Ant script.  Below is a snippet of code I am using.  In
> > >comments, you will see the various ways I have tried to call the
> > >setCoreLoader method.
> > >
> > >Thank you,
> > >
> > >File buildfile = null;
> > >try {
> > >     buildfile = new File(this.workingDirectory.getPath() +
> > "/build.xml");
> > >     Project project = new Project();
> > >     project.setBaseDir(this.workingDirectory);
> > >     project.setName(this.project);
> > >     project.setUserProperty("ant.file", buildfile.getPath());
> > >     project.init();
> > >     ProjectHelper helper = ProjectHelper.getProjectHelper();
> > >     project.addReference("ant.projectHelper", helper);
> > >     helper.parse(project, buildfile);
> > >     Properties sysProps = System.getProperties();
> > >     for (Iterator it = sysProps.entrySet().iterator();it.hasNext();) {
> > >  Map.Entry entry = (Map.Entry) it.next();
> > >  project.setProperty(entry.getKey().toString(), entry.getValue
> > >().toString());
> > >     }
> > >     project.setProperty ("basedir", project.getBaseDir().getPath());
> > >     project.setProperty("ant.project.name", project.getName());
> > >     project.setProperty("ant.file ", buildfile.getPath());
> > >     project.setCoreLoader(Thread.currentThread
> > ().getContextClassLoader());
> > >//     project.setCoreLoader(null);
> > >//     project.setCoreLoader(this.getClass().getClassLoader());
> > >//     project.setCoreLoader(Thread.currentThread
> > >().getContextClassLoader().getParent());
> > >     project.execute("dist");
> > >} catch (Exception e) {
> > >     throw new RuntimeException(e);
> > >} finally {
> > >     project = null;
> > >     buildfile = null;
> > >}
> > >
> > >
> > >--
> > >Marc Farrow
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> > For additional commands, e-mail: dev-help@ant.apache.org
> >
> >
>
>
> --
>
> Marc Farrow
>



-- 
Marc Farrow

Re: dev-list - Calling Ant within a servlet/webapp

Posted by Marc Farrow <ma...@gmail.com>.
Thanks for the tip Tom.  I will definitely check it out.

On 8/16/06, Tom Cunningham <cu...@mac.com> wrote:
>
>
> Have you taken a look at Anthill (
> http://www.urbancode.com/projects/anthill/default.jsp)?   There's an open
> source version which you can hack up, but it seems like it does the same
> thing that you are trying to do here (kick off ant from the web).     If you
> do need to do this, you might want to check the code in their open source
> version for how they handle their classloaders.
>
> On Wednesday, August 16, 2006, at 08:27AM, Marc Farrow <
> marc.farrow@gmail.com> wrote:
>
> >I have some servlet and I am trying to call an Ant script within the
> class
> >to compile a "project".  All of the references in the build script are
> not
> >relative, therefore some of the classpath references are not correct.  I
> >know I can change the properties, etc to fix this.  However, due to the
> >nature of our environment we do not want to do this.  With all that being
> >said, I do know that all the classpath entries that I need to compile the
> >needed classes are within my current servlet container (in my case
> Tomcat).
> >What can I do retrieve the correct classloader so that all the classes
> are
> >visible to the Ant script.  Below is a snippet of code I am using.  In
> >comments, you will see the various ways I have tried to call the
> >setCoreLoader method.
> >
> >Thank you,
> >
> >File buildfile = null;
> >try {
> >     buildfile = new File(this.workingDirectory.getPath() +
> "/build.xml");
> >     Project project = new Project();
> >     project.setBaseDir(this.workingDirectory);
> >     project.setName(this.project);
> >     project.setUserProperty("ant.file", buildfile.getPath());
> >     project.init();
> >     ProjectHelper helper = ProjectHelper.getProjectHelper();
> >     project.addReference("ant.projectHelper", helper);
> >     helper.parse(project, buildfile);
> >     Properties sysProps = System.getProperties();
> >     for (Iterator it = sysProps.entrySet().iterator();it.hasNext();) {
> >  Map.Entry entry = (Map.Entry) it.next();
> >  project.setProperty(entry.getKey().toString(), entry.getValue
> >().toString());
> >     }
> >     project.setProperty("basedir", project.getBaseDir().getPath());
> >     project.setProperty("ant.project.name", project.getName());
> >     project.setProperty("ant.file", buildfile.getPath());
> >     project.setCoreLoader(Thread.currentThread
> ().getContextClassLoader());
> >//     project.setCoreLoader(null);
> >//     project.setCoreLoader(this.getClass().getClassLoader());
> >//     project.setCoreLoader(Thread.currentThread
> >().getContextClassLoader().getParent());
> >     project.execute("dist");
> >} catch (Exception e) {
> >     throw new RuntimeException(e);
> >} finally {
> >     project = null;
> >     buildfile = null;
> >}
> >
> >
> >--
> >Marc Farrow
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>


-- 
Marc Farrow

Re: dev-list - Calling Ant within a servlet/webapp

Posted by Tom Cunningham <cu...@mac.com>.
Have you taken a look at Anthill (http://www.urbancode.com/projects/anthill/default.jsp)?   There's an open source version which you can hack up, but it seems like it does the same thing that you are trying to do here (kick off ant from the web).     If you do need to do this, you might want to check the code in their open source version for how they handle their classloaders.

On Wednesday, August 16, 2006, at 08:27AM, Marc Farrow <ma...@gmail.com> wrote:

>I have some servlet and I am trying to call an Ant script within the class
>to compile a "project".  All of the references in the build script are not
>relative, therefore some of the classpath references are not correct.  I
>know I can change the properties, etc to fix this.  However, due to the
>nature of our environment we do not want to do this.  With all that being
>said, I do know that all the classpath entries that I need to compile the
>needed classes are within my current servlet container (in my case Tomcat).
>What can I do retrieve the correct classloader so that all the classes are
>visible to the Ant script.  Below is a snippet of code I am using.  In
>comments, you will see the various ways I have tried to call the
>setCoreLoader method.
>
>Thank you,
>
>File buildfile = null;
>try {
>     buildfile = new File(this.workingDirectory.getPath() + "/build.xml");
>     Project project = new Project();
>     project.setBaseDir(this.workingDirectory);
>     project.setName(this.project);
>     project.setUserProperty("ant.file", buildfile.getPath());
>     project.init();
>     ProjectHelper helper = ProjectHelper.getProjectHelper();
>     project.addReference("ant.projectHelper", helper);
>     helper.parse(project, buildfile);
>     Properties sysProps = System.getProperties();
>     for (Iterator it = sysProps.entrySet().iterator();it.hasNext();) {
>  Map.Entry entry = (Map.Entry) it.next();
>  project.setProperty(entry.getKey().toString(), entry.getValue
>().toString());
>     }
>     project.setProperty("basedir", project.getBaseDir().getPath());
>     project.setProperty("ant.project.name", project.getName());
>     project.setProperty("ant.file", buildfile.getPath());
>     project.setCoreLoader(Thread.currentThread().getContextClassLoader());
>//     project.setCoreLoader(null);
>//     project.setCoreLoader(this.getClass().getClassLoader());
>//     project.setCoreLoader(Thread.currentThread
>().getContextClassLoader().getParent());
>     project.execute("dist");
>} catch (Exception e) {
>     throw new RuntimeException(e);
>} finally {
>     project = null;
>     buildfile = null;
>}
>
>
>-- 
>Marc Farrow
>
>

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