喜欢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" >
            
             < ibiblio name = "public" m2compatible = "true" usepoms = "true" />
            
       
         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(*)">
        
         < 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" />
        
         < 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
< property name = "publish.version" value = "0.01" />
< property name = "ivy.report.todir" value = "build" />
< ivy:settings file = "ivysettings.xml" /> 
< 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 >
< 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 >
< 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里面声明了一下,你的项目就可以编译通过了,如下图:
喜欢Ant不喜欢Maven的有福了_第1张图片

你可能感兴趣的:(ant,maven,eclipse,ivy)