###目录结构 ant-test ------->工程主目录<br> ----build<br> --------classes ----------->存放编译后的class文件<br> ----src --------->存放源码<br> ----lib --------->存放jar包<br> ###使用javac和java运行java程序 felix@felix-computer:~/ant-test/src$ vim HelloWorld.java<br>
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world....");
}
}
编译:<br> felix@felix-computer:~/ant-test$ javac -sourcepath src -d build\classes src\HelloWorld.java <br> 运行:<br> felix@felix-computer:~/ant-test$ java -cp build/classes HelloWorld <br> Hello world.... <br> ###使用Ant运行 编写build.xml<br>
<!-- lang: xml -->
<project>
<target name="clean">
<delete dir="build"/>
</target>
<!--compile之前先clean-->
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<!--javac的具体用法参考 http://ant.apache.org/manual/Tasks/javac.html-->
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="run">
<!--java的具体用法参考 http://ant.apache.org/manual/Tasks/java.html-->
<java classname="HelloWorld" classpath="build/classes"/>
</target>
</project>
分别运行ant compile和ant run <br>
###使用Ant运行带有外部依赖的jar 对HelloWorld.java进行改写,利用self4j的Logger.info();输出Helloworld 改写后如下:<br> felix@felix-computer:~/ant-test$ vim src/HelloWorldWithLogger.java <br>
<!-- lang: java -->
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorldWithLogger{
public static Logger logger = LoggerFactory.getLogger(HelloWorldWithLogger.class);
public static void main(String[] args){
//System.out.println("Hello world....");
logger.info("Hello world....");
}
}
编写build.xml文件,为了与上面区分开来,对build.xml重命名 <br> felix@felix-computer:~/ant-test$ vim buildwithjar.xml <br>
<!-- lang: xml -->
<project>
<target name="clean">
<delete dir="build"/>
</target>
<!--引入lib目录下所有jar-->
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
<!--compile之前先clean-->
<!--
直接命令行编译
javac -cp lib\slf4j-api-1.7.6.jar;lib\slf4j-simple-1.7.6.jar -sourcepath src -d build\classes src\HelloWorldWithLogger.java
-->
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<!--javac的具体用法参考 http://ant.apache.org/manual/Tasks/javac.html-->
<javac srcdir="src" destdir="build/classes" classpathref="classpath"/>
</target>
<!--
直接命令行运行
java -cp lib\slf4j-api-1.7.6.jar;lib\slf4j-simple-1.7.6.jar;build\classes HelloWorldWithLogger
-->
<target name="run">
<!--java的具体用法参考 http://ant.apache.org/manual/Tasks/java.html-->
<java classname="HelloWorldWithLogger" classpath="build/classes" classpathref="classpath"/>
</target>
</project>
运行ant -buildfile buildwithjar.xml compile<br> felix@felix-computer:~/ant-test$ ant -buildfile buildwithjar.xml compile
Buildfile: /home/felix/ant-test/buildwithjar.xml
clean: [delete] Deleting directory /home/felix/ant-test/build
compile: [mkdir] Created dir: /home/felix/ant-test/build/classes [javac] /home/felix/ant-test/buildwithjar.xml:19: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 2 source files to /home/felix/ant-test/build/classes
BUILD SUCCESSFUL Total time: 0 seconds
felix@felix-computer:~/ant-test$
运行ant -buildfile buildwithjar.xml run <br> felix@felix-computer:~/ant-test$ ant -buildfile buildwithjar.xml run
Buildfile: /home/felix/ant-test/buildwithjar.xml run: [java] [main] INFO HelloWorldWithLogger - Hello world.... BUILD SUCCESSFUL Total time: 0 seconds felix@felix-computer:~/ant-test$
代码示例点击下载
over...