喜欢Ant不喜欢Maven的有福了

1. Ant+Ivy VS. Maven

Maven是一个比Ant更强大的工具,除了构建项目它还可以管理项目的依赖,甚至可以一键生成项目网站;

但是,对于一般的企业应用,Maven太过复杂难用了:

  • Maven脚本是配置式的风格,其构建过程已固化在工具内部不可改变,只能通过修改配置来改变Maven的构建行为,学习难度和使用难度都很高;
  • Maven对项目工程结构有严格要求,对遗留系统不友好;

上述两点恰好是Ant的优势,同时Ant借助Ivy也可以获得跟Maven一样的依赖管理能力(其实Ivy依赖管理比Maven做的更好,配置也更简洁)。

2. Ivy的安装

下载Ivy的最新发布包(http://apache.dataguru.cn/ant/ivy/),解压后将其中的jar包(例如ivy-2.3.0.jar)copy到ant_home/lib下即可;

3. Ant集成Ivy

如果你的工程已经在用Ant构建,那么只需以下几部操作即可为工程添加Ivy依赖管理能力:

  • 在工程根目录创建ivysettings.xml,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<? xml version = "1.0" encoding = "ISO-8859-1" ?>
< ivysettings >
     < ivy:configure >
         < credentials host = "192.168.1.234" realm = "Sonatype Nexus Repository Manager" username = "deployment" passwd = "deployment123" />
     </ ivy:configure >
     < settings defaultResolver = "defaultChain" defaultConflictManager = "latest-revision" />
     < caches defaultCacheDir = "d:/.ivy2" />
     < property name = "nexus-public" value = "http://192.168.1.234:8081/nexus/content/groups/public" />
     < property name = "nexus-releases" value = "http://192.168.1.234:8081/nexus/content/repositories/releases" />
     < property name = "nexus-snapshots" value = "http://192.168.1.234:8081/nexus/content/repositories/snapshots" />
     < resolvers >
         < chain name = "defaultChain" checkmodified = "true" changingPattern = ".*SNAPSHOT" >
             <!-- 从公网下载依赖的jar -->
             < ibiblio name = "public" m2compatible = "true" usepoms = "true" />
             <!-- 从nexus私服下载依赖的jar -->
        <!-- <ibiblio name="public" m2compatible="true" usepoms="true" root="${nexus-public}" />
             <ibiblio name="releases" m2compatible="true" usepoms="true" root="${nexus-releases}" />
             <ibiblio name="snapshots" m2compatible="true" usepoms="true" root="${nexus-snapshots}"
                 pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" /> -->
         </ chain >
     </ resolvers >
</ ivysettings >
  • 在工程根目录创建ivy.xml,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< ivy-module version = "1.0" >
     < info organisation = "com.abc" module = "project17" />
     < configurations >
         < conf name = "default" visibility = "public" extends = "runtime,master" />
         < conf name = "master" visibility = "public" />
         < conf name = "compile" visibility = "public" />
         < conf name = "provided" visibility = "public" />
         < conf name = "runtime" visibility = "public" extends = "compile" />
         < conf name = "test" visibility = "private" extends = "runtime" />
     </ configurations >
     < dependencies defaultconfmapping="compile->compile(*),master(*);runtime->master(*),compile(*),runtime(*)">
         <!-- 日志相关lib,会传递依赖log4j jar -->
         < dependency org = "org.slf4j" name = "slf4j-api" rev = "1.7.3" conf = "compile;runtime" />
         < dependency org = "org.slf4j" name = "slf4j-log4j12" rev = "1.7.3" conf = "compile;runtime" />
 
         < dependency org = "javax.servlet" name = "servlet-api" rev = "2.5" conf="provided->default" />
         <!-- Test libs -->
         < dependency org = "junit" name = "junit" rev = "4.8.2" conf="test->default" />       
     </ dependencies >
</ ivy-module >
  • 修改工程的build.xml,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!-- ivy properties -->
< property name = "publish.version" value = "0.01" />
< property name = "ivy.report.todir" value = "build" />
<!-- 初始化ivy:   -->
< ivy:settings file = "ivysettings.xml" /> 
<!-- 添加resolve target,用于下载依赖包并创建3个classpath变量用于其它target:   -->
< target name = "resolve" description = "-- parse ivy.xml" >
     < ivy:resolve file = "ivy.xml" conf = "*" useCacheOnly = "true" />
     < ivy:cachepath pathid = "ivy.libs.compile" type = "jar,bundle" conf = "compile,provided" />
     < ivy:cachepath pathid = "ivy.libs.test" type = "jar,bundle" conf = "test,provided" />
     < ivy:cachepath pathid = "ivy.libs.runtime" type = "jar,bundle" conf = "runtime" />
</ target >
<!-- 改造compile,依赖于resolve并使用ivy.libs.compile和ivy.libs.test变量,其它target如运行unit test也类似:   -->
< target name = "compile" depends = "resolve" >
         < mkdir dir = "${classes.dir}" />
         < javac target = "1.6" encoding = "utf-8" srcdir = "src" destdir = "${classes.dir}" debug = "${build.debug}" includeantruntime = "false" >
             < compilerarg value = "-Xlint:unchecked" />
             < classpath >
                 < path refid = "ivy.libs.compile" />
                 < path refid = "ivy.libs.test" />
             </ classpath >
         </ javac >
</ target >
<!-- 添加report target用于生产漂亮的依赖报告,这个也不是必须的:   -->
< target name = "report" depends = "resolve" description="--> resolve and retrieve dependencies with ivy"> 
         < ivy:report /> 
</ target >

OK!至此,你已经为蚂蚁插上了Ivy的翅膀,原来直接堆在工程lib目录下的杂乱的jar可以删掉了,

执行ant resolve target,Ivy会按照ivy.xml中声明的dependency自动为你下载所需的所有jar包,

你会发现下载的jar比你声明的dependency个数要多,那是因为ivy会自动下载间接依赖的jar;

下载的jar会被整齐有序的存放在d:/.ivy2,并且可以被多个项目复用;

4. Eclipse集成Ivy

Ant集成Ivy只是解决了在命令行工具中进行工程打包时的依赖管理问题,

而实际工作中开发人员主要在Eclipse中进行编译、调试以及调用Ant构建工程,也需要解决依赖包管理问题;

好在Ivy提供的Eclipse插件来解决这个问题,安装配置如下:

  • Window->preference->ant->RunTime->Classpath->Ant Home Entries,右边Add External Jars,添加ivy-2.3.0.jar,这样就完成了Eclipse中的Ant与Ivy集成;
  • 安装Ivy插件:Help->Install new software->add,Name: IvyDE,Location: http://www.apache.org/dist/ant/ivyde/updatesite ;安装成功后重启eclipse;
  • 重启eclipse后,Window->preference->ivy->settings;Ivy settings path设为${ivyproject_loc}/ivysettings.xml (eclipse3.7 Indigo)

至此Eclipse的ivy插件配置好了,然后就可以为你的项目classpath添加ivy依赖了:

  • 选中项目->右键 属性->Java Build Path->Libraries->Add Library...->IvyIDE Managed Dependencies->finish->OK
  • 然后神奇的事情就出现了——虽然你工程目录下一个jar包也有,只是在ivy.xml里面声明了一下,你的项目就可以编译通过了,如下图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值