Run ant from Java
Is there a tutorial on how to run Ant from Java?
Part of my answer to this question might help:
See this article
and this article:File buildFile = new File("build.xml");
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
How to run ant command through java program?
A number of things jump out at me.
- The
ant
command should beant.bat
under windows - You should separate the command from the arguments, so that each argument represents a separate
String
. This greatly reduces the complexity of arguments that contain spaces, for example... - You should really use
ProcessBuilder
, it will reduce the complexity of usingProcess
For example...
ProcessBuilder pb = new ProcessBuilder(
"D:\\installs\\apache-ant-1.9.2\\bin\\ant.bat",
"-v");
pb.redirectError();
// This should point to where your build.xml file is...
pb.directory(new File("C:/"));
try {
Process p = pb.start();
InputStream is = p.getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char) in);
}
int exitValue = p.waitFor();
System.out.println("Exited with " + exitValue);
} catch (Exception ex) {
ex.printStackTrace();
}
I should also point out that ant
is a linux/unix shell script, which Windows won't know what to do with ;)
Updated
I've tested this using Windows 7 and Java 7, works just fine
I've tested this using Windows 7 and Java 6, works just fine...
If you do continue to have issues running the ant.bat
directly, then you will need run the batch file via the command processor
ProcessBuilder pb = new ProcessBuilder(
"cmd",
"/c",
"D:\\installs\\apache-ant-1.9.2\\bin\\ant.bat",
"-v");
Ant: How to execute Ant from Java
You can execute Ant from Java and print the output to System.out
as follows (note that -buildfile
is the same as -file
or -f
):
ProcessBuilder builder = new ProcessBuilder("ant", "-f", "/path/to/build.xml", "searchDirectories");
Process process = builder.start();
try (BufferedReader out = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
out.lines().collect(Collectors.toList()).forEach(System.out::println);
}
If your searchDirectories
task produces wrong results, maybe the corresponding <target>
contains a bug.
How can I run a java class with a relative path from Ant?
There's several ways you might do this. Perhaps the simplest is to specify a relative value for the dir
attribute in your java
task, for example:
<java classname="test.XMLCreator" classpathref="compile-classpath" dir=".." fork="true">
You'll need to also use fork
for that to work, which may have other side effects.
On your specific question: many tasks support the dir
attribute, some use nested filesets, so there you would have to specify the directory on the nested element.
Another approach is to execute a target using the ant
task. The directory specified there will be used as the basedir for all tasks in the target.
How to run `ant` build-script without setting any environment variable?
You problem is not concerned with *_HOME variables, but I first answer you question.
Yes you can.
Just configure your PATH variable to (1) dir where java.exe resides (2) where ant.bat resides.
In case when *_HOME defined the path can be written:
PATH=...;%JAVA_HOME%\bin;%ANT_HOME%
Since you have no such variables you need declare:
PATH=...;C:\Program Files\Java\bin;c:\ant\bin
But in real you problem that you try use JRE while ant needs JDK. Just download from oracle site. tools.jar
is part of JDK but not JRE.
UPDATE:
You can write you own bat file that lets Windows know where to locate .exe
and .bat
files. Just create in notepad text file named my-ant.bat
And place following there:
set JAVA_HOME=C:\buildscript_required_files_v2\java\jdk64
set PATH=%PATH%;%JAVA_HOME%\bin;C:\buildscript_required_files_v2\apache-ant-1.9.0\bin
rem ** Now we invoke ant **
ant
Obviously you would like manipulate with command line arguments. That is why instead of last ant
line use following:
set my_ant_start=
:setupArgs
if ""%1""=="""" goto doneStart
set my_ant_start=%my_ant_start% %1
shift
goto setupArgs
:doneStart
rem ** Now we invoke ant **
ant %my_ant_start%
How to run ant inside java command
I think the simplest solution is to call org.apache.tools.ant.Main#main
just like the ant command line does.
For instance:
Main.main(new String[] { "-f", "path/to/the/build.xml"});
Related Topics
Java 8: Mandatory Checked Exceptions Handling in Lambda Expressions. Why Mandatory, Not Optional
Jbutton() Only Working When Mouse Hovers
How to Set Color to a Certain Row If Certain Conditions Are Met Using Java
Java Inheritance - Calling Superclass Method
"Content Is Not Allowed in Prolog" When Parsing Perfectly Valid Xml on Gae
Netbeans - Error: Could Not Find or Load Main Class
Is There a Java Equivalent of the Python Eval Function
How to Use This Boolean in an If Statement
How to Include External Jar on My Netbeans Project
How to Modify the Raw Xml Message of an Outbound Cxf Request
Changing Java Platform on Which Netbeans Runs
How to Use Hibernate @Any-Related Annotations
Stream Filter of 1 List Based on Another List
Checking If Unlimited Cryptography Is Available
Why Is It Not Possible to Shadow a Local Variable in a Loop
How to Enable Wire Logging for a Java Httpurlconnection Traffic