Ant+Ivy for building java projects

Install Ant: download the ant package

tar zvxf apache-ant-1.9.4-bin.tar.gz
#in .bashrc file
#Ant
export ANT_HOME=/home/chase/runnable/apache-ant-1.9.4
export CLASSPATH=${ANT_HOME}/lib:${CLASSPATH}:.
export PATH=${ANT_HOME}/bin:${PATH}

 Install Ivy: download the latest Ivy package "apache-ivy-2.4.0-rc1-bin.tar.gz" and unpack

tar zvxf apache-ivy-2.4.0-rc1-bin.tar.gz 

it shows:

and then copy the jar to the ant/lib classpath:

chase@pc0:~/runnable/apache-ivy-2.4.0-rc1$ cp ivy-2.4.0-rc1.jar ../apache-ant-1.9.4/lib/

then Ant and Ivy is combined!!!!!!!!!!!

How to use Ant and Ivy together

for ant create a build.xml, for Ivy create an ivy.xml file. That is

this project is for building an xx.aj file. ant is for building and ivy is for reslove dependencies:

the build file is

<!-- Tracer / AP01

     Author: Klaus Marius Hansen, DIKU
-->

<project name="template" default="help" basedir="."
     xmlns:ivy="antlib:org.apache.ivy.ant" >

  <!-- Several targets are copied from the Ivy tutorials -->
  <property name="lib.dir" value="lib" />
  <property name="build.dir" value="build" />
  <property name="src.dir" value="src" />  
  <property name="test.dir" value="test" />

  <!-- paths used for compilation and run  -->
  <path id="lib.path.id">
    <fileset dir="${lib.dir}" />
  </path>
  <path id="run.path.id">
    <path refid="lib.path.id" />
    <path location="${build.dir}" />
  </path>

  <target name="help">
    <echo>
   AP01 Tracer - simple tracer 
   Targets 
    clean:       Removes all bytecode.
    build-all:        Build and create JAR

  Author: Klaus Marius Hansen
    </echo>
  </target>
  
  <target name="clean">
    <delete dir="${build.dir}"/>
    <delete dir="${lib.dir}"/>
    <delete dir="output"/>
  </target>
  
  <target name="prepare">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${lib.dir}"/>
  </target>
  
  <!-- ================================= 
       target: resolve              
       ================================= -->
  <target name="resolve" 
      description="--> retreive dependencies with ivy">
    <ivy:retrieve/><!--retrieve任务复制解析好的依赖到你的文件系统的任何位置-->
  </target>    
  
  <!-- ================================= 
       target: report              
       ================================= -->
  <target name="report" depends="resolve" 
      description="--> generates a report of dependencies">
    <ivy:report todir="${build.dir}"/>
  </target>

  <!-- compilation tasks -->
  <!-- AspectJ definition -->
  <target name="build-src" depends="prepare,resolve">
    <taskdef resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties" 
      classpath="${lib.dir}/aspectjtools-1.8.4.jar"/>
    <iajc sourceroots="${src.dir}" source="1.6" outjar="${build.dir}/apsensor.jar" outxml="true">
      <classpath>
        <pathelement location="${lib.dir}/aspectjrt-1.8.4.jar"/>
      </classpath>
    </iajc>
  </target>

  <target name="build-test" depends="build-src">
    <javac srcdir="${test.dir}"
       destdir="${build.dir}"
       debug="on" 
       classpathref="lib.path.id" 
       includeAntRuntime="false">
    </javac>
  </target>

  <target name="copy-resource">
    <!-- empty -->
  </target>
  
  <target name="build-all" 
      depends="build-src,build-test,copy-resource"/>

</project>

The ivy.xml is

<ivy-module version="2.4">
    <info organisation="ap" module="harvest"/>
    <dependencies>
      <dependency org="org.aspectj" name="aspectjrt" rev="1.8.4"/>
      <dependency org="org.aspectj" name="aspectjtools" rev="1.8.4"/>
      <dependency org="org.aspectj" name="aspectjweaver" rev="1.8.4"/>
      <dependency org="junit" name="junit" rev="4.10"/>
    </dependencies>
</ivy-module>

The src folder is the .aj file: src/dk/diku/tracer/Tracer.aj

The test folder is empty. After run

$ant clean build-all

is becomes:

in the build folder :

in the lib folder:

Then the aspectj advices are weaved into the apsensor.jar file which can be used in other java files.

posted on 2014-11-17 15:44  chaseblack  阅读(320)  评论(0编辑  收藏  举报

导航