Ant学习小记

###目录结构 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...

转载于:https://my.oschina.net/linuxfelix/blog/203408

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值