Maven是一个比Ant更强大的工具,除了构建项目它还可以管理项目的依赖,甚至可以一键生成项目网站;
但是,对于一般的企业应用,Maven太过复杂难用了:
上述两点恰好是Ant的优势,同时Ant借助Ivy也可以获得跟Maven一样的依赖管理能力(其实Ivy依赖管理比Maven做的更好,配置也更简洁)。
下载Ivy的最新发布包(http://apache.dataguru.cn/ant/ivy/),解压后将其中的jar包(例如ivy-2.3.0.jar)copy到ant_home/lib下即可;
如果你的工程已经在用Ant构建,那么只需以下几部操作即可为工程添加Ivy依赖管理能力:
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 >
|
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 >
|
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,并且可以被多个项目复用;
Ant集成Ivy只是解决了在命令行工具中进行工程打包时的依赖管理问题,
而实际工作中开发人员主要在Eclipse中进行编译、调试以及调用Ant构建工程,也需要解决依赖包管理问题;
好在Ivy提供的Eclipse插件来解决这个问题,安装配置如下:
至此Eclipse的ivy插件配置好了,然后就可以为你的项目classpath添加ivy依赖了: