Can you call an Ant target from a JAVA class?

H

hust6

I need help in calling an Ant target from within a JAVA class.

If anyone knows how to do this, I would appreciate the help.

Thanks!

hust6
 
R

Roland

I need help in calling an Ant target from within a JAVA class.

If anyone knows how to do this, I would appreciate the help.

Thanks!

hust6

Use the static method 'start' in org.apache.tools.ant.Main


String[] target = {"compile"};
Properties extraUserProps = ... ; // can be null
ClassLoader loader = ... ; // can be null
org.apache.tools.ant.Main.start(target, extraUserProps, loader);


See Ant's API doc. This should be available in your Ant installation
directory, e.g. apache-ant-1.6.5/docs/manual/api/index.html
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
H

hust6

I didn't mention that I am trying to run an ant task in a build.xml
file. Does this change it? I can't figure out exactly how to
implement the code you offered. Say I am trying to run an ant target
from C:\test\build.xml, and the target is as follows:

<target name="test1">
<echo> ${arg1}</echo>
</target>

How would I make a call to this target (including specifying "arg1")?

Again, thank you very much for the help, I appreciate it.

hust6
 
R

Roland

I didn't mention that I am trying to run an ant task in a build.xml
file. Does this change it? I can't figure out exactly how to
implement the code you offered. Say I am trying to run an ant target
from C:\test\build.xml, and the target is as follows:

<target name="test1">
<echo> ${arg1}</echo>
</target>

How would I make a call to this target (including specifying "arg1")?

Again, thank you very much for the help, I appreciate it.

hust6

Use the Properties parameter of the start method:

String[] args = {"test1"};
Properties userProps = new Properties();
userProps.setProperty("arg1", "WhatEver");
userProps.setProperty("arg2", "Something");
ClassLoader loader = null; // or a specific classloader
org.apache.tools.ant.Main.start(args, userProps, loader);


And if you need to specify a specific build file, you can use the following:

File buildFile = new File("path/to/some/build.xml");
String[] args = {"-buildfile", buildFile.toString(), "test1"};

I.e. the args passed to start method are like those you would pass on
the commandline:
ant -buildfile path/to/some/build.xml test1

--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top