我有一个build.xml来为我的应用程序生成Jar文件。
我试图直接调用build.xml,如下所示
import java.io.IOException;
public class RunBuild {
public static void main(String args[]) throws IOException {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("/home/sai/Jan5WS/AntTest/build.xml");
}
}
它没有抛出任何异常,但它没有生成jar文件。
我目前正在使用ubuntu 12和ant 8.1版本
有人可以告诉我可能出错的地方吗?
谢谢
已编辑的部分
我试过这样运行
public static void main(String args [])抛出IOException {
File buildFile = new File("build.xml", "/home/sai/Jan5WS/AntTest");
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());
}
但它的投掷
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.tools.ant.Project.initProperties(Project.java:313)
at org.apache.tools.ant.Project.init(Project.java:300)
at RunBuild.main(RunBuild.java:15)
答案 0 :(得分:1)
假设正常的ant,则无法执行build.xml。
您需要运行ant并将build.xml作为参数传递。
(可以使用其库运行一个ant项目:请参阅此问题Is it possible to call Ant or NSIS scripts from Java code?)
答案 1 :(得分:1)
您有两种选择:
File buildFile = new File("/home/sai/Jan5WS/AntTest", "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());
在这种情况下,ant.jar和ant-launcher.jar必须在classpath。
答案 2 :(得分:0)
build.xml不是可执行文件。它只是一个普通的xml文件。您需要执行“ant”并通过代码中的“-f”标志为其提供此build.xml文件的路径。
答案 3 :(得分:0)
尝试一下为我工作....
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class CreateJar {
public static void main(String[] args) {
Process process;
try {
process = new ProcessBuilder("ant","-f" ,"/home/nitesh/Projects/ANT_DEMO/RPM_ANT/SIS/build.xml").start();
process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这里是build.xml
<project name="rpmextract" default="jar" basedir=".">
<property name="build" location="build"/>
<target name="jar">
<jar destfile="app.jar" basedir="${build}/classes"/>
</target>
</project>
答案 4 :(得分:0)