Ant-Ivy使用

转至:http://blog.csdn.net/leon709/article/details/7428838

 

Apache Ivy是专门用来管理项目的jar包依赖的。我们知道Maven已经有很出色的这方面的功能,如果你已经在使用Maven,就没必要使用Ivy了。但是其实Maven除了这方面功能,还有很多强大的功能,如果你只需要管理jar包依赖而已,那么可以只用Ivy就够了,用Maven就有点大材小用。 

1.      首先,你的机器应该已经安装了Ant和JDK,最好是1.6或更高版本的Ant,并在计算机环境变量里已经配置好了正确的ANT_HOME,JAVA_HOME和path变量。

2.      接下来,在某磁盘,比如D盘,随意建一个文件夹,用来安装Ivy,比如D:/AntIvy

3.      然浏览器,访问以下链接,可以直接下载得到build.xml文件:

http://ant.apache.org/ivy/history/latest-milestone/samples/build.xml

将下载到的build.xml文件保存在D:/AntIvy中。 

4.      打开DOS命令行cd进入该目录D:/AntIvy

输入ant,回车,就会开始安装Ivy了。不过可能失败,有些时候电脑无法直接下载一些东西,需要代理,所以,允许ant命令前需要设置Ant代理,先运行:

set ANT_OPTS=-Dhttp.proxyHost=xxx.xxx.xxx -Dhttp.proxyPort=1234

扫描二维码关注公众号,回复: 764555 查看本文章

然后再运行ant命令就可以了,这样将会进行安装Ivy。 

5.      安装成功后,D:/AntIvy下会有ivy和src目录,ivy目录下有个ivy.jar,将该jar包拷贝到ANT_HOME/lib下。(其实整个说来说去,最重要的就是要获得这ivy.jar文件。其他的都是浮云。撇开这些步骤,如果你能用其他方式获得这个ivy.jar那么就等于你已经可以使用Ivy了,最简单的方式就是去maven Repo中搜ivy就可以下载了,哈哈。

 

6.      经过以上5个步骤后,实际上已经完成Ivy的安装和配置了,下面我们来看一个Example使用Ivy。访问以下路径,会有几个Ivy例子:

https://svn.apache.org/repos/asf/ant/ivy/core/trunk/src/example

可以下载到hello-ivy,将该例子保存下来,DOS命令行进入该hello-ivy工程根目录。运行命令ant如下: 

 

D:\AntIvy\src\example\hello-ivy>ant

Buildfile: build.xml

 

resolve:

No ivy:settings found for the default reference 'ivy.instance'.  A default insta

nce will be used

no settings file found, using default...

:: Ivy 2.0.0-beta1 - 20071206070608 :: http://ant.apache.org/ivy/ ::

:: loading settings :: url = jar:file:/D:/tool/Ant/lib/ivy.jar!/org/apache/ivy/c

ore/settings/ivysettings.xml

[ivy:retrieve] :: resolving dependencies :: org.apache#hello-ivy;working@SHNI132

 

[ivy:retrieve]  confs: [default]

[ivy:retrieve]  found commons-lang#commons-lang;2.0 in public

[ivy:retrieve]  found commons-cli#commons-cli;1.0 in public

[ivy:retrieve]  found commons-logging#commons-logging;1.0 in public

[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-lang/commons-la

ng/2.0/commons-lang-2.0.jar ...

[ivy:retrieve] ...........

[ivy:retrieve] .... (165kB)

[ivy:retrieve] .. (0kB)

[ivy:retrieve]  [SUCCESSFUL ] commons-lang#commons-lang;2.0!commons-lang.jar (35

55ms)

[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-cli/commons-cli

/1.0/commons-cli-1.0.jar ...

[ivy:retrieve] ..... (29kB)

[ivy:retrieve] .. (0kB)

[ivy:retrieve]  [SUCCESSFUL ] commons-cli#commons-cli;1.0!commons-cli.jar (1559m

s)

[ivy:retrieve] downloading http://repo1.maven.org/maven2/commons-logging/commons

-logging/1.0/commons-logging-1.0.jar ...

[ivy:retrieve] ... (21kB)

[ivy:retrieve] .. (0kB)

[ivy:retrieve]  [SUCCESSFUL ] commons-logging#commons-logging;1.0!commons-loggin

g.jar (1715ms)

[ivy:retrieve] :: resolution report :: resolve 6705ms :: artifacts dl 6829ms

[ivy:retrieve]  :: evicted modules:

[ivy:retrieve]  commons-lang#commons-lang;1.0 by [commons-lang#commons-lang;2.0]

 in [default]

        ---------------------------------------------------------------------

        |                  |            modules            ||   artifacts   |

        |       conf       | number| search|dwnlded|evicted|| number|dwnlded|

        ---------------------------------------------------------------------

        |      default     |   4   |   3   |   0   |   1   ||   3   |   3   |

        ---------------------------------------------------------------------

[ivy:retrieve] :: retrieving :: org.apache#hello-ivy

[ivy:retrieve]  confs: [default]

[ivy:retrieve]  3 artifacts copied, 0 already retrieved (216kB/78ms)

 

run:

    [mkdir] Created dir: D:\AntIvy\src\example\hello-ivy\build

    [javac] Compiling 1 source file to D:\AntIvy\src\example\hello-ivy\build

     [java] standard message : hello ivy !

     [java] capitalized by org.apache.commons.lang.WordUtils : Hello Ivy !

 

BUILD SUCCESSFUL

Total time: 16 seconds

D:\AntIvy\src\example\hello-ivy>

 

这只是第一次运行ant,需要下载jar包等所以慢,还打印了不少东西,但是第二次再运行ant时,就很快了。

 

7.      好了,现在我们可以看看hello-ivy实例工程中根目录中ivy.xml和build.xml,这是很简单的文件,你看看一定会有所领悟的,而且你知道你可以随便copy,然后修修改改就可以用到其他的项目中。比如现在我们在Eclipse中新建了一个JavaProject,我们只要把这两个文件copy到新项目的根目录下。Eclipse中一般有内置的Ant,所以我么运行Ant的build时,使用的是内置的Ant,这时需要配置一下Eclipse中的Ant,加上我们的ivy.jar,你懂的。 

8.      Window  --> Preferences --> Ant --> Runtime  --> 点选Ant Home Entries或GlobalEntries  -->  Add External Jar将我们安装的Ivy下的ivy/ivy.jar添加进来就可以了。 

 

其实,使用Ivy最重要的就是获得ivy.jar,这个可以上http://search.maven.org/搜索ivy就可以获得了。将获得的jar包放入Ant的lib里面就OK啦。

其次就是build.xml和ivy.xml,这里主要的是一些Ant命令的写法,在build.xml中如何编辑标签来配置ant命令,另外ivy.xml中配置的是dependency,而且两个文件可以合二为一,如果你对这些配置已经很熟悉,那么就没什么好看的啦。

 在Eclipse中安装IvyDE插件

http://www.apache.org/dist/ant/ivyde/updatesite

Help  --> Install New Software...  -->  Add

然后名称设为:ApacheIvy update site

Location即上面的url:http://www.apache.org/dist/ant/ivyde/updatesite

然后安装的时候选择这个新添加的UpdateSite,勾选ApacheIvyDE,Next,开始安装。

好了,重启Eclipse,OK。安装好插件是可以让Eclipse也可以像Maven那样管理jar包。

 

当然,还有其他的方法,比如下载个IvyDE的.zip自己copy到Eclipse的plugin和feature里也可以。

猜你喜欢

转载自anduo.iteye.com/blog/1811004
Ivy