您的位置:首页 > 其它

Ant、Ivy入门与集成

2015-07-14 15:33 363 查看




Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。由Apache软件基金会所提供。

Apache Ivy是一个优秀的管理(记录、跟踪、解析和报告)项目依赖的工具,可与Apache Ant紧密集成。

整理日志比敲代码还累



资源准备

本文中的演示实例是基于Eclipse插件(Ivy插件叫IvyDE,Ant插件一般都自带)来编写的。主要包括Ivy依赖管理、Ant自动化测试与打包、Ant与Ivy集成三个部分。相关资源下载地址:http://pan.baidu.com/s/1i3D2AFj

新建Java项目

为了温习Java的反射机制,特意在项目中编写了4个Java文件:Demo.java、IDemo.java、Test.java、JunitTest.java。目录结构如下:



Ivy管理依赖

为了满足后面的自动化测试,我加入了JunitTest,故需要引入Junit的相关Jar包。在项目根目录下新增ivysettings.xml、ivy.xml两个文件。

ivysettings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ivysettings>
<settings defaultResolver="default"/>
<property name="ivy.local.default.root"             value="${ivy.default.ivy.user.dir}/local" override="false"/>
<property name="ivy.local.default.ivy.pattern"      value="[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" override="false"/>
<property name="ivy.local.default.artifact.pattern" value="[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" override="false"/>

<caches useOrigin="true"/>

<resolvers>
<!--本地库-->
<filesystem name="local">
<ivy pattern="${ivy.local.default.root}/${ivy.local.default.ivy.pattern}" />
<artifact pattern="${ivy.local.default.root}/${ivy.local.default.artifact.pattern}" />
</filesystem>

<!--缓存库-->
<filesystem name="cache">
<ivy pattern="${ivy.default.ivy.user.dir}/cache/[organisation]/[module]/[artifact]-[revision].xml" />
<artifact pattern="${ivy.default.ivy.user.dir}/cache/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]" />
</filesystem>

<!--远程库 省略-->

<chain name="default" returnFirst="true" checkmodified="true">
<resolver ref="local"/>
<resolver ref="cache"/>
</chain>
</resolvers>
</ivysettings>

注意:此配置生效需要在“C:\Users\****\.ivy2\cache”路径下存在ivy的缓存Jar包。

ivy.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
<info
organisation="com.netbug"
module="reflect"
status="integration">
</info>
<configurations>
<conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime"/>
<conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/>
<conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/>
<conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/>
</configurations>
<dependencies>
<!-- 单元测试相关 -->
<dependency org="junit" name="junit" rev="4.10" conf="default" transitive="false" />
<!-- json序列化相关 -->
<dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.3.1" conf="default" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.3.1" conf="default" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.3.1" conf="default" transitive="false"/>
</dependencies>
</ivy-module>

修改项目的ivy配置信息如下:注意ivysetting.xml。



右键ivy.xml,选择“Add Ivy Library…”。此时会出现如下界面:



点击“Finish”,项目的Build Path变化如下:



此时JunitTest.java编译应该就不会报错了。


Ant自动化测试与打包

首先将资源中的junit-4.10.jar复制到Ant插件安装目录的lib下,并配置Ant运行环境如下:



Ant的自动化测试需要另外提供junitJar包,如果不提供则会抛异常。

在项目根目录下新建“build.properties、build.xml”两个文件。对应的文件内容如下:

build.properties

#Tue, 14 Jul 2015 09:46:09 +0800
custom=true
build.dir=build
build.classes.dir=build/classes
build.lib.dir=build/lib
build.report.dir=build/report


build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-test-project" default="default" xmlns:ivy="antlib:org.apache.ivy.ant">
<description>
description
</description>
<property file="build.properties">
</property>
<path id="cachepath">
<fileset dir="lib" includes="**/*.jar">
</fileset>
</path>
<target name="default" depends="jar" description="description" if="basedir">

</target>

<target name="property" description="Inside And Custom Property">
<echo message="basedir:${basedir}" />
<echo message="ant.file:${ant.file}" />
<echo message="ant.project.name:${ant.project.name}" />
<echo message="ant.version:${ant.version}" />
<echo message="ant.java.version:${ant.java.version}" />
</target>

<target name="clean">
<delete dir="${build.dir}">
</delete>
</target>

<target name="init" depends="property,clean">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${build.lib.dir}" />
<mkdir dir="${build.report.dir}" />
</target>

<target name="compile" depends="init">
<javac srcdir="src" destdir="${build.classes.dir}" includeantruntime="true" classpathref="cachepath">
</javac>
<echo message="源文件编译成功!" />
</target>
<target name="compile.test" depends="compile">
<javac srcdir="test" destdir="${build.classes.dir}" includeantruntime="true" classpathref="cachepath">
</javac>
<echo message="测试源文件编译成功!" />
</target>

<target name="test" depends="compile.test">
<echo message="Junit测试开始!" />
<junit printsummary="true" showoutput="true" fork="true">
<classpath>
<pathelement path="${build.classes.dir}" />
</classpath>
<formatter type="xml" />
<batchtest todir="${build.report.dir}">
<fileset dir="${build.classes.dir}">
<include name="**/JunitTest.*" />
</fileset>
</batchtest>
</junit>
<echo message="Junit测试结束!" />
</target>

<target name="java" depends="test">
<java classname="com.netbug.reflect.Test" classpath="${build.classes.dir}">
</java>
<echo message="组件运行成功!" />
</target>

<target name="jar" depends="java">
<jar destfile="${build.dir}/com.netbug.reflect.jar">
<fileset dir="${build.classes.dir}">
<exclude name="**/JunitTest.*" />
</fileset>
<manifest>
<attribute name="Main-class" value="com.netbug.reflect.Test" />
</manifest>
</jar>
<echo message="组件打包成功!" />
</target>
</project>


此时的项目目录结构如下:



右键“build.xml”-“Run AS Ant Build”,在根目录的build文件下会生成打包文件:com.***.reflect.jar以及自动化测试报告“report/TEST-com.netbug.reflect.JunitTest.xml”

Ant与Ivy集成

首先将资源中的ivy-2.3.0.jar复制到Ant插件安装目录的lib下,并配置Ant运行环境如下:



Ant与ivy集成需要另外提供ivy.jar包,如果不提供则会抛异常。

其后对build.xml做相应修改如下:

注释掉cachepath

<!-- <path id="cachepath">
<fileset dir="lib" includes="**/*.jar">
</fileset>
</path>  -->


新增init.ivy target

<target name="init.ivy" description="init ivy">
<ivy:settings file="ivysettings.xml" id="ivy.instance" />
<ivy:cachepath pathid="cachepath" conf="*" type="jar,bundle,war,maven-plugin" />
<ivy:retrieve conf="*" type="jar,bundle,war,maven-plugin" pattern="${build.lib.dir}/[conf]/[artifact].[ext]" />
<ivy:report todir="${build.report.dir}" />
<echo message="Ivy初始化成功" />
</target>


修改compile target基于init、init.ivy两个target

<target name="compile" depends="init,init.ivy">
<javac srcdir="src" destdir="${build.classes.dir}" includeantruntime="true" classpathref="cachepath">
</javac>
<echo message="源文件编译成功!" />
</target>


右键“build.xml”-“Run AS Ant Build”,在根目录的build文件下会生成打包文件:com.***.reflect.jar、自动化测试报告“report/TEST-com.netbug.reflect.JunitTest.xml”以及“ivy依赖报告”。

最终的项目目录结构如下:



参考资料链接

http://www.blogjava.net/aoxj/archive/2009/08/03/289522.html

http://www.blogjava.net/zhangzhong1018/articles/142411.html

http://www.cnblogs.com/cyjch/archive/2012/03/28/2420761.html

http://blog.csdn.net/lisonghua/article/details/4770260

http://my.oschina.net/u/134516/blog/195569

Best Wishes For You!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息