现在的位置: 首页 > 综合 > 正文

喜欢Ant不喜欢Maven的有福了

2013年04月26日 ⁄ 综合 ⁄ 共 5055字 ⁄ 字号 评论关闭

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
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<ivysettings>
    <ivy:configure>
        <credentialshost="192.168.1.234"realm="Sonatype
Nexus Repository Manager"
username="deployment"passwd="deployment123"/>
    </ivy:configure>
    <settingsdefaultResolver="defaultChain"defaultConflictManager="latest-revision"/>
    <cachesdefaultCacheDir="d:/.ivy2"/>
    <propertyname="nexus-public"value="http://192.168.1.234:8081/nexus/content/groups/public"/>
    <propertyname="nexus-releases"value="http://192.168.1.234:8081/nexus/content/repositories/releases"/>
    <propertyname="nexus-snapshots"value="http://192.168.1.234:8081/nexus/content/repositories/snapshots"/>
    <resolvers>
        <chainname="defaultChain"checkmodified="true"changingPattern=".*SNAPSHOT">
            <!-- 从公网下载依赖的jar -->
            <ibiblioname="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-moduleversion="1.0">
    <infoorganisation="com.abc"module="project17"/>
    <configurations>
        <confname="default"visibility="public"extends="runtime,master"/>
        <confname="master"visibility="public"/>
        <confname="compile"visibility="public"/>
        <confname="provided"visibility="public"/>
        <confname="runtime"visibility="public"extends="compile"/>
        <confname="test"visibility="private"extends="runtime"/>
    </configurations>
    <dependenciesdefaultconfmapping="compile->compile(*),master(*);runtime->master(*),compile(*),runtime(*)">
        <!-- 日志相关lib,会传递依赖log4j jar -->
        <dependencyorg="org.slf4j"name="slf4j-api"rev="1.7.3"conf="compile;runtime"/>
        <dependencyorg="org.slf4j"name="slf4j-log4j12"rev="1.7.3"conf="compile;runtime"/>
 
        <dependencyorg="javax.servlet"name="servlet-api"rev="2.5"conf="provided->default"
/>
        <!-- Test libs -->
        <dependencyorg="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 -->
<propertyname="publish.version"value="0.01"/>
<propertyname="ivy.report.todir"value="build"/>
<!-- 初始化ivy:   -->
<ivy:settingsfile="ivysettings.xml"/> 
<!-- 添加resolve target,用于下载依赖包并创建3个classpath变量用于其它target:   -->
<targetname="resolve"description="--
parse ivy.xml"
>
    <ivy:resolvefile="ivy.xml"conf="*"useCacheOnly="true"/>
    <ivy:cachepathpathid="ivy.libs.compile"type="jar,bundle"conf="compile,provided"/>
    <ivy:cachepathpathid="ivy.libs.test"type="jar,bundle"conf="test,provided"/>
    <ivy:cachepathpathid="ivy.libs.runtime"type="jar,bundle"conf="runtime"/>
</target>
<!-- 改造compile,依赖于resolve并使用ivy.libs.compile和ivy.libs.test变量,其它target如运行unit test也类似:   -->
<targetname="compile"depends="resolve">
        <mkdirdir="${classes.dir}"/>
        <javactarget="1.6"encoding="utf-8"srcdir="src"destdir="${classes.dir}"debug="${build.debug}"includeantruntime="false">
            <compilerargvalue="-Xlint:unchecked"/>
            <classpath>
                <pathrefid="ivy.libs.compile"/>
                <pathrefid="ivy.libs.test"/>
            </classpath>
        </javac>
</target>
<!-- 添加report target用于生产漂亮的依赖报告,这个也不是必须的:   -->
<targetname="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里面声明了一下,你的项目就可以编译通过了,如下图:

抱歉!评论已关闭.