您的位置:首页 > 其它

ivy操作大全

2015-06-19 17:37 211 查看
1. 设置缓存目录

默认为:${user.home}/cache,

在运行ant时,设置如ivy.default.ivy.user.dir=f:/ivy2,所以它会缓存到f:/ivy2/cache

e.g. ant -Divy.default.ivy.user.dir=f:/ivy2

2. ivy中引入maven依赖包

在ivy.xml中<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra" xmlns:m="http://ant.apache.org/ivy/maven">
ivy tokens map to Maven 2 terminology
Ivy Maven 2
organisation group ID
module, artifact artifact ID
revision version
ext type
classifier classifier

3. default setting include 3 types of repository

local a repository which is private to the user.
shared a repository which is shared between all the members of a team
public a public repository on which most modules, and especially third party modules, can be found

默认 the local repository lies in ${ivy.default.ivy.user.dir}/local
modify . For instance:
ivy.local.default.root=/opt/ivy/repository/local

4.ivy task

ivy:resolve 解析ivy下载依赖包到本地cache
ivy:retrieve: 复制本地cache中的依赖包到工程中的指定比如:dep.lib目录
ivy:deliver:deliver an ivy file to 工程中的build/
ivy:publish: 把build/目录中的包按ivy中的conf的规则publish到目标位置

ivy 中patten的写法中 pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]"
(-[classifier])这种写法经常用到,如果classifier没有定义值,则忽略括号中的内容匹配。

5. 依赖管理

【ivysetting.xml文件】描述ivy的配置,包括:版本冲突管理,repository地址列表等

【ivy.xml文件】描述项目的直接依赖
1)解决2个同名不同版本的jar包冲突
依赖关系与jar包的不同版本密切相关,系统要求必须同时存在两个不同版本,一个是1.5.3(被hibernate依赖),一个是2.2.3(被spring依赖),如果只是使用ivysetting中的默认设置:

<settings defaultResolver="libs.local" defaultConflictManager="latest-revision" />
那么2.2.3一定取代1.5.2,如何让两者同时存在呢?
在dependecies模块中使用conflict标记:<conflict org="asm" rev="1.5.3,2.2.3" />即可,不同版本号使用逗号间隔。在ivy.xml中这样定义

<dependencies>
<dependency org="org.hibernate" name="hibernate" rev="3.2.6.ga" conf="default" />
<dependency org="org.springframework" name="spring" rev="2.5.6" conf="default" />
<!--注意:以下指定解决asm.jar包的版本冲突的方法:保留2.2.3和1.5.3两个版本的文件-->
<conflict org="asm" rev="1.5.3,2.2.3" />
</dependencies>

<conflict org="asm" rev="1.5.3" /> 则保留1.5.3的版本

2)包名字相同,不同org的jar包版本冲突解决

对于名为ant的依赖包,系统要求解决两个不同版本即:1.6.5和1.7.2之间的版本冲突,但是比较特殊的是这两个版本所属的org不同,即在公共依赖库中的目录不一致,1.6.2属于apache组织,在apache目录下。而1.7.2属于org.apache组织,在org.apache目录下(这显然是ant在开发升级过程中的一个调整的结果),如何解决两个所属org不同的依赖包之间的版本冲突呢?其实简单,两个依赖包有相同的名字:ant,还是用conflict标签解决问题:<conflict
module="ant" rev="1.7.1" /> 则使用1.7.1的版本

3)排除部分传递依赖

比如:dpc/distributed-cache depend on util and its full-api

util depend on some 3pp jars

如果不希望工程dpc/distributed-cache引入util的所有3pp

比如其中一个依赖包org.apache.tomcat会引入它的所有依赖,而dpc/distributed-cache不希望引入其中的某个传递依赖servlet-api

可以在dpc/distributed-cache/ivy.xml 如下定义

<dependencies>
<dependency org="org.apache.tomcat" name="jasper" rev="" conf="eclipse,compiletest->default">
<exclude org="org.apache.tomcat" module="servlet-api"/> or <exclude name=""servlet-api"/>
</dependency>
</dependencies>

4) 如果定义<conflict module="xws-security" rev="" /> rev为空可避免下载xws-security

<exclude name="xws-security" /> 仅仅是不把因为到工程项目中 ???

待确认

5) remove ivy transitive dependencies,有时传递依赖拿不到,可以采取这种方式避免去retrieve

dsc/util/ivy.xml

<dependency org="org.apache.tomcat" name="jasper" rev="" conf="build,eclipse->default"/>

will retrieve all dependency package ( include jasper and its transitive dependencies)

if :<dependency org="org.apache.tomcat" name="jasper" rev="" transitive="false" conf="build,eclipse->default"/>

using [ transitive="false" ] to remove all transitive dependencies, only retrieve "jasper" dependency.

6. 生成dependency tree报告

<target name="ivy-report" depends="retrieve"> <ivy:report todir="build"/> </target>

7. ivy how to get latest SNAPSHOT maven artifacts

在maven2中这个通常被称为SNAPSHOT(快照)版本,并且有一种主张认为这样可以帮助节约空间,因为只需要为开发时可能创建的大量的中间产物保留一个修订版本。
ivy使用"changing revision"的概念来支持这种方法。changing revision就是这样:一个ivy认为随着时间推移始终可能变更的修订版本,
为了处理这个,可以通过使用以来标签明确指定一个依赖为可以变更,或者在解析器上使用changingPattern 和changingMatcher 属性来知名那个修订版本或者修订版本组可以被认为是变更的。changingPattern=".*-SNAPSHOT"
但是以上方法没有解决chain resolver的问题, 当碰到war 类型的artifact 和m:classifier这些设置没有起作用,本地缓存还是没有download最新的依赖包。

本地缓存默认的artifactPattern=[organisation]/[module]/[type]s/[artifact]-[revision].[ext]
(http://ant.apache.org/ivy/history/2.0.0/settings/caches.html)

1) 对war 格式的artifact

原定义:

<dependency org="com.sew" name="fwer-war" rev="" conf=" fwer->default">
<artifact name=" fwer-war" ext="war"/>
</dependency>

本地缓存没拿到最新的fwer-war.war包,增加属性type="war"

<dependency org="com.sew" name="fwer-war" rev="" conf=" fwer->default">
<artifact name=" fwer-war" ext="war" type="war"/>
</dependency>

2) 对m:classifier类型的artifact,原定义如下

<dependency org="com.sew" name="ddwe" rev="" conf="jboss-modules->default">
<artifact name=" ddwe" ext="zip" m:classifier="jboss-module" />
</dependency>

尝试加了type="zip" 还是拿不到最新的依赖包

发现Ivy 的开发者曾经遇到不能正确找到m:classifier包从artifactory server问题,在把属性classifier放到artifact inside dependency element的情况下.他们尝试移到dependency中,解决了这个问题。事实上可以放在artifact是正确的,可以找到。

基于这个思路,我尝试把这个属性移到dependency element中,太惊奇了,真拿到了最新的依赖包。

修改如下:

<dependency org="com.sew" name="ddwe" rev="" conf="jboss-modules->default" m:classifier="jboss-module" >
<artifact name=" ddwe" ext="zip" />
</dependency>

结论:对应标准的maven包,可以在resolver中设置changingPattern=".*-SNAPSHOT" 或checkModified="true"获取到最新的依赖,但是对应war和m:classifier则需要特殊定义dependency.

8. convert ivy to pom, pom to ivy

1) ivy.xml to pom.xml (ivy:makepom target), example:
<target name="converttopom" depends="retrieve" >
<ivy:makepom ivyfile="${basedir}/build/ivy.xml" pomfile="${basedir}/build/module.pom">
<mapping conf="default" scope="compile"/>
<mapping conf="runtime" scope="runtime"/>
</ivy:makepom>
</target>
The configuration 'default' in the parsed ivy file will be mapped to the scope 'compile',
the configuration 'runtime' will be mapped to 'runtime', and other configurations will be considered optional.
Note that all Ivy features are not supported by maven poms, so the converted pom may not resolve to the exact same dependencies as the original ivy file.
2) pom.xml to ivy.xml
<ivy:convertpom pomFile="pom.xml" ivyFile="ivy.xml" />
但是还是没有完整的转换,需要人工check.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: