我的 build.xml
中有一个任务:
<target name="init" depends="init-ivy">
...
<ivy:cachepath
inline="true"
module="jersey-container-servlet"
organisation="org.glassfish.jersey.containers"
pathid="jersey.classpath"
revision="2.23.2" />
...
</target>
如果需要,此任务会下载 ivy(init-ivy
实际上会这样做),然后调用 ivy 来下载依赖项。它将 jersey.classpath
设置为结果。
现在我的 build
任务取决于 init
任务。所以每次构建时,它都会检查是否需要安装依赖项。我想避免每次都检查依赖关系,并将 build
与 init
分开。但是 init
设置了 jersey.classpath
并且 build
使用它。
有没有办法从ivy获取jersey.classpath
而不要求它检查依赖关系?在这种情况下,不检查依赖关系是否是一个好习惯?
最佳答案
正如这个答案中所解释的,ivy 不会在每次运行时下载 jar。它将它们缓存在本地“~/.ivy2/cache”下:
其次,您在内联模式下使用 ivy,大概是为了避免创建 ivy file ? Ivy cachepath被分类为post resolve任务,这意味着它会自动调用resolve任务,在后台。内联模式的作用是告诉 ivy 每次执行一次新的解析,如果您有多个类路径需要管理,这是一种浪费。
您最后考虑过使用ivy 文件吗?一次调用 resolve任务可以处理项目的所有依赖项,在本地缓存此信息,然后确定是否需要下载文件。我建议始终解决依赖关系。它的成本并不高,而且构建之间的情况可能会发生变化(例如,如果您使用动态依赖项或 Maven 快照)。
这是我的 ivy 标准 Ant 目标:
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<target name="resolve" depends="install-ivy">
<ivy:resolve/>
<ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
<ivy:cachepath pathid="test.path" conf="test"/>
</target>
<target name="install-ivy" unless="ivy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
注释:
- 名为“resolve”的单个目标,它调用 ivy 来管理我的项目类路径。它还生成有用的文档报告
- cachepath任务正在使用ivy configurations对 ivy 文件中的依赖项进行分组或分类。这确实是一种效率魔法,可以在单一解决方案上节省时间。
- 请注意install-ivy任务如何进行条件检查以确定是否安装了ivy。由于复杂性,此技巧对于项目的其余依赖项不可行。我的建议是确保 ivy 存在,然后用它来管理其他一切。 (在幕后,它会尽最大努力提高效率)。我真的不明白为什么 Apache Ant 不捆绑 ivy jar。
关于java - 如何获得 Ivy :cachepath location without checking if dependencies downloaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313778/