如何避免使用 Ivy 复制依赖项 [英] How to avoid copying dependencies with Ivy

查看:26
本文介绍了如何避免使用 Ivy 复制依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用 Ivy 来管理依赖项,但是哇 - 那东西真的很喜欢制作多个 jar 副本!它像我后院的常春藤一样蔓延,同样令人讨厌!

I'm looking into using Ivy to manage dependencies but wow - that thing really likes to make multiple copies of jars! It spreads like the ivy in my back yard and is just as undesirable!

是否可以让 Ivy 简单地定义一个引用解析依赖项的类路径(用于指定的配置文件),以便我的 javac 可以直接在 ivy 存储库(或缓存?)中引用它们.

Is it possible to have Ivy simply define a classpath (for a specified profile) that references the resolved dependencies so my javac can reference them directly in the ivy repository (or cache?).

我已经阅读了参考文档,只看到了设置到存储库缓存的符号链接的选项.我想这已经足够了,但这似乎是一种浪费.另外,我不确定战争"任务是否可以从符号链接构建战争……但我想我会在尝试时找出答案.

I've read the reference docs buy only see the option to set up symbolic links to the repository cache. I guess this will suffice, but it seems like a waste. Also, I'm not sure that a "war" task can build the war from symbolic links... but I guess I'll find out when I give it a try.

有更好的建议吗?

推荐答案

这是我的标准 Java 构建文件,它创建了一个可执行的 jar.

Here's my standard Java build file that creates an executable jar.

目标是通过 ANT 属性和用于 3rd 方依赖项的 ivy.xml 文件的组合来管理项目特定的内容.

The objective is to manage project specific stuff via a combination of ANT properties and an ivy.xml file for the 3rd-party dependencies.

<project xmlns:ivy="antlib:org.apache.ivy.ant" name="demo" default="build">

  <property name="src.dir" location="src"/>
  <property name="build.dir" location="build"/>
  <property name="dist.dir" location="dist"/>
  <property name="dist.jar" location="${dist.dir}/${ant.project.name}.jar"/>
  <property name="dist.main.class" value="HelloWorld"/>

  <target name="retrieve">
    <ivy:resolve/>
    <ivy:cachepath pathid="build.path" conf="build"/>
    <ivy:cachepath pathid="runtime.path" conf="runtime"/>
  </target>

  <target name="compile" depends="retrieve">
    <mkdir dir="${build.dir}/classes"/>
    <javac srcdir="${src.dir}" destdir="${build.dir}/classes" classpathref="build.path"/>
  </target>

  <target name="build" depends="compile">
    <ivy:retrieve pattern="${dist.dir}/lib/[artifact].[ext]"/>

    <manifestclasspath property="jar.classpath" jarfile="${dist.jar}">
      <classpath>
        <fileset dir="${dist.dir}/lib" includes="*.jar"/>
      </classpath>
    </manifestclasspath>

    <jar destfile="${dist.jar}" basedir="${build.dir}/classes">
      <manifest>
        <attribute name="Main-Class" value="${dist.main.class}"/>
        <attribute name="Class-Path" value="${jar.classpath}"/>
      </manifest>
    </jar>
  </target>

  <target name="clean">
    <delete dir="${build.dir}"/>
    <delete dir="${dist.dir}"/>
  </target>

</project>

正如您在 Ivy 文档中发现的那样,cachepath Ivy 任务用于管理两个 ANT 路径.一个用于构建依赖项,另一个用于可执行 jar 的运行时依赖项.

As you've discovered in the Ivy docu, the cachepath Ivy task is used to manage two ANT paths. One for the build dependencies the other for the run-time dependencies of the executable jar.

Ivy 的真正力量在于一种叫做configurations 的东西.我发现最初很难掌握,直到我意识到我可以为我的项目定义的 jar 的简单逻辑分组.这个例子有两个配置:

The real power of Ivy is in something called configurations. I found it difficult to grasp initially until I realised it was simple a logical grouping of jars that I can define for my project. This example has two configurations:

  • 构建
  • 运行时

这是演示如何将依赖项与 configurations 相关联的 ivy 文件:

Here's the ivy file demonstrating how dependencies can be associated with configurations:

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>
    <configurations>
        <conf name="build" description="Libraries needed to for compilation"/>
        <conf name="runtime" extends="build" description="Libraries that need to be included with project jar" />
    </configurations>
    <dependencies>
        <dependency org="commons-lang" name="commons-lang" rev="2.0" conf="build->default"/>
        <dependency org="commons-cli" name="commons-cli" rev="1.0" conf="runtime->default"/>
    </dependencies>
</ivy-module>

总而言之,我希望这个例子有助于理解 Ivy.我喜欢它只专注于一件事的方式,即管理 3rd 方依赖项.

In conclusion I hope this example helps in understanding Ivy. I like the way it concentrates on only one thing, the management of 3rd-party dependencies.

这篇关于如何避免使用 Ivy 复制依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆