(转)Eclipse中使用Ant

 

Eclipse中使用Ant

分类: JAVA 工具 服务器

http://286.iteye.com/blog/1909223

 Eclipse中已经集成了Ant,我们可以直接在Eclipse中运行Ant,这里我要做的不是直接运行已编写好的build.xml文件,而是利用Ant插件来生成一个构建文件。

        首先打开Eclipse,点击导航栏的"Window"-->"Preferences"-->"Ant"


        其中有Ant的代码模板,格式化,classpath等属性设置,可以根据具体情况自行设置,都比较简单。

        我们要构建一个有依赖jar包项目的构建文件,这里我拿log4j为例,将log4j添加到HelloAnt项目的build path当中,如图所示:



 

        修改HelloAnt.java的源代码为:

Java代码  收藏代码
  1. package com.ant.hello;  
  2.   
  3. import org.apache.log4j.Logger;  
  4.   
  5. public class HelloAnt {  
  6.       
  7.     private static Logger log=Logger.getLogger(HelloAnt.class);  
  8.       
  9.     public static void main(String[] args){  
  10.         log.info("hello Ant!");  
  11.   
  12.     }  
  13.       
  14. }  

        修改build path中的class输出路径为"HelloAnt/classes"。

        我们在项目名称上右键-->选择Export(导出)-->Ant Buildfile-->下一步-->修改相应属性-->Finish




        完成之后我们会发现项目目录里多了一个"build.xml"文件:



        打开之后是如下代码:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
  2.   
  3. <project basedir="." default="build" name="HelloAnt">  
  4.     <property environment="env"/>  
  5.     <property name="debuglevel" value="source,lines,vars"/>  
  6.     <property name="target" value="1.6"/>  
  7.     <property name="source" value="1.6"/>  
  8.     <path id="HelloAnt.classpath">  
  9.         <pathelement location="classes"/>  
  10.         <pathelement location="../log4j.jar"/>  
  11.     </path>  
  12.     <target name="init">  
  13.         <mkdir dir="classes"/>  
  14.         <copy includeemptydirs="false" todir="classes">  
  15.             <fileset dir="src">  
  16.                 <exclude name="**/*.launch"/>  
  17.                 <exclude name="**/*.java"/>  
  18.             </fileset>  
  19.         </copy>  
  20.     </target>  
  21.     <target name="clean">  
  22.         <delete dir="classes"/>  
  23.     </target>  
  24.     <target depends="clean" name="cleanall"/>  
  25.     <target depends="build-subprojects,build-project" name="build"/>  
  26.     <target name="build-subprojects"/>  
  27.     <target depends="init" name="build-project">  
  28.         <echo message="${ant.project.name}: ${ant.file}"/>  
  29.         <javac debug="true" debuglevel="${debuglevel}" destdir="classes" includeantruntime="false" source="${source}" target="${target}">  
  30.             <src path="src"/>  
  31.             <classpath refid="HelloAnt.classpath"/>  
  32.         </javac>  
  33.     </target>  
  34.     <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>  
  35.     <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">  
  36.         <copy todir="${ant.library.dir}">  
  37.             <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>  
  38.         </copy>  
  39.         <unzip dest="${ant.library.dir}">  
  40.             <patternset includes="jdtCompilerAdapter.jar"/>  
  41.             <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>  
  42.         </unzip>  
  43.     </target>  
  44.     <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">  
  45.         <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>  
  46.         <antcall target="build"/>  
  47.     </target>  
  48.     <target name="HelloAnt">  
  49.         <java classname="com.ant.hello.HelloAnt" failonerror="true" fork="yes">  
  50.             <classpath refid="HelloAnt.classpath"/>  
  51.         </java>  
  52.     </target>  
  53. </project>  

      我们可以根据情况修改其中的<path>标签下的命名及路径,当然这种事比较傻瓜式的生成方法,下面我们换一种可以详细设置的生成方法。

        点击导航栏"Project"-->"Properties"-->"Builder"-->"New"-->"Ant Builder":



        根据给出的界面我们可以详细设置。

  • 大小: 6.4 KB
  • 大小: 8.2 KB
  • 大小: 10 KB
  • 大小: 10 KB
  • 大小: 11.3 KB
  • 大小: 9 KB
  • 大小: 41 KB
原文地址:https://www.cnblogs.com/luolizhi/p/4959977.html