简体   繁体   English

在旧的 Ant 项目中添加 Ivy

[英]Adding Ivy in old Ant project

I've already read some articles but I'm not able to understand how I should setup an Ivy file for dependencies.我已经阅读了一些文章,但我无法理解如何为依赖项设置 Ivy 文件。 The project I'm working on is old and I don't know it much, I have this half generated - half edited build.xml file which works with Eclipse compiler.我正在处理的项目很旧,我不太了解,我生成了一半 - 一半编辑了 build.xml 文件,该文件适用于 Eclipse 编译器。 It's the first time I deal with build configuration, I want to move the dependencies to the ivysettings.xml file and improve what can be improved.这是我第一次处理构建配置,我想将依赖项移动到 ivysettings.xml 文件并改进可以改进的地方。 My target is to create a war to deploy in Tomcat.我的目标是创建一个部署在 Tomcat 中的战争。

What could be the best way to do it?最好的方法是什么?

build.xml:构建.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>

Step 1 is to get rid of that <path id> block;第 1 步是摆脱那个<path id>块; that's what you're refactoring into an ivy-based solution.这就是您要重构为基于常春藤的解决方案的内容。 You need an ivy.xml file that lists your dependencies.您需要一个列出您的依赖项的ivy.xml文件。 An example can be found here - that is about as complex as it'll get, yours can presumably be simpler.可以在此处找到一个示例-它会尽可能复杂,您的示例可能会更简单。 I suggest test , runtime and build configurations, you don't need more unless you have a pressing need.我建议testruntimebuild配置,除非你有迫切需要,否则你不需要更多。 Then, for each dependency, figure out in which contexts you need it (if you need it on the cp when compiling, 'build'. If you need it on the cp when running, 'runtime'. Only put in the 'test' configuration when it's for testing but nothing else (so, usually, junit and that's it).然后,对于每个依赖项,找出你需要它的上下文(如果你在编译时在 cp 上需要它,'build'。如果你在运行时在 cp 上需要它,'runtime'。只放入'test'用于测试时的配置,但仅此而已(因此,通常是 junit 就是这样)。

Then in your ant file you're looking to do something like:然后在您的 ant 文件中,您希望执行以下操作:

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

which will then make those jars appear in lib/test , lib/runtime and lib/build .这将使那些 jars 出现在lib/testlib/runtimelib/build中。 Use start ant tricks to make a object (do not explicitly list out each jar).使用 start ant 技巧来制作 object(不要明确列出每个 jar)。 This does play havoc when you remove a dependency, as that won't make the jar file disappear, but it's probably simpler to then just ant clean (which should now also delete the lib dir in addition to build ).当您删除依赖项时,这确实会造成严重破坏,因为这不会使 jar 文件消失,但它可能比 ant clean 更简单(除了build之外,现在还应该删除lib目录)。 There are ways to make an ant <path> object off of a configuration but that's more complicated.有一些方法可以使 ant <path> object 脱离配置,但这更复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM