【问题标题】:Adding Ivy in old Ant project在旧的 Ant 项目中添加 Ivy
【发布时间】:2021-03-24 23:45:41
【问题描述】:

我已经阅读了一些文章,但我无法理解如何为依赖项设置 Ivy 文件。我正在处理的项目很旧,我不太了解,我生成了这一半 - 一半编辑过的 build.xml 文件,它与 Eclipse 编译器一起工作。这是我第一次处理构建配置,我想将依赖项移动到 ivysettings.xml 文件并改进可以改进的地方。我的目标是创建一个部署在 Tomcat 中的战争。

最好的方法是什么?

build.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="Teamwork"
    xmlns:ivy="antlib:org.apache.ivy.ant">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../../../../Program Files/eclipse/"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.7"/>
    <property name="source" value="1.7"/>
    <path id="Web App Libraries.libraryclasspath">
        <pathelement location="WebContent/WEB-INF/lib/Tidy.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/antlr-2.7.7.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/asm-attrs.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/asm.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/avro-1.5.1.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/aws-java-sdk-1.5.5.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/bsh-2.0b4.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/c3p0-0.9.2.1.jar"/>
        <pathelement location="WebContent/WEB-INF/lib/commons-beanutils.jar"/>
        [... other jars]
    </path>
    <path id="EAR Libraries.libraryclasspath"/>
    <path id="Teamwork.classpath">
        <pathelement location="bin"/>
        <path refid="Web App Libraries.libraryclasspath"/>
        <path refid="EAR Libraries.libraryclasspath"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="Teamwork.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
</project>

【问题讨论】:

  • Ant 和 Ivy 过时了。如果您仍然努力的话,我建议您迁移到 Maven。
  • 我看不出迁移到 Maven 项目的原因,只是因为“它更新了”,如果这有效的话。然而,在直接迁移到 Maven 之前集成 Ivy 将有助于该过程。 stackoverflow.com/questions/17237679/…
  • 并不是说 Maven 比较新。它比 Ivy 更擅长管理依赖项。事实上,它已经相当老了。没有人再使用 Ant 或 Ivy。查看所有的开源 Java 项目。他们都使用 Maven 或 Gradle。蚂蚁消失了。
  • 就我而言,现在不需要。可能会考虑未来的改进。现在,是没有目的的。谢谢你的建议。

标签: java eclipse build ant ivy


【解决方案1】:

第 1 步是摆脱 &lt;path id&gt; 块;这就是您要重构为基于常春藤的解决方案的内容。您需要一个列出您的依赖项的ivy.xml 文件。可以在here 中找到一个示例——它会尽可能复杂,但你的可能会更简单。我建议testruntimebuild 配置,除非您有迫切需要,否则您不需要更多。然后,对于每个依赖项,找出你需要它的上下文(如果你在编译时在 cp 上需要它,'build'。如果你在运行时在 cp 上需要它,'runtime'。只放入'test'用于测试时的配置,但仅此而已(通常,junit 就是这样)。

然后在您的 ant 文件中,您希望执行以下操作:

<ivy:resolve file="buildScripts/ivy.xml" refresh="true" conf="build, runtime, test" />  
<ivy:retrieve/>

这将使这些罐子出现在lib/testlib/runtimelib/build 中。使用 start ant 技巧来制作一个对象(不要明确列出每个 jar)。当您删除依赖项时,这确实会造成严重破坏,因为这不会使 jar 文件消失,但它可能比 ant clean 更简单(现在除了 build 之外,还应该删除 lib 目录)。有一些方法可以使 ant &lt;path&gt; 对象脱离配置,但更复杂。

【讨论】:

  • 我应该把ivy:resolve放在哪个块中以便从eclipse编译器中获取?
  • 没关系,只要它在您执行需要依赖关系的操作之前运行即可。例如,在生成 eclipse 项目文件之前(如果您的 ant 脚本这样做),在编译之前(如果您的 ant 脚本这样做),在测试之前等等。
猜你喜欢
  • 2013-06-13
  • 2012-05-31
  • 2011-06-23
  • 2013-06-01
  • 2013-04-20
  • 2016-02-08
  • 1970-01-01
  • 2012-09-08
  • 2018-01-10
相关资源
最近更新 更多