java在旧ant项目中添加ivy

jc3wubiy  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(355)

我已经读了一些文章,但我不明白我应该如何设置一个常春藤依赖文件。我正在处理的项目很旧,我不太了解它,我有一个半生成半编辑的build.xml文件,它与eclipse编译器一起工作。这是我第一次处理构建配置,我想将依赖项移到ivysettings.xml文件中,并改进可以改进的内容。我的目标是创建一个在tomcat中部署的战争。
最好的办法是什么?
生成.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>
fgw7neuy

fgw7neuy1#

第一步就是摆脱这种状况 <path id> 块;这就是你正在重构的基于常春藤的解决方案。你需要一个 ivy.xml 列出依赖项的文件。一个例子可以在这里找到-这是大约复杂的,因为它会得到,你可以想当然地更简单。我建议 test , runtime 以及 build 配置,除非你有迫切的需要,否则你不需要更多。然后,对于每个依赖项,找出您需要它的上下文(如果您在编译“build”时在cp上需要它)。如果在运行“runtime”时需要它在cp上。只有在“test”配置用于测试时才放进去,其他什么都不放(所以,通常是junit,就这样)。
然后在ant文件中,您将执行以下操作:

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

然后这些jar就会出现在 lib/test , lib/runtime 以及 lib/build . 使用start ant技巧生成一个对象(不要显式列出每个jar)。当您删除依赖项时,这确实会造成严重的破坏,因为这不会使jar文件消失,但是只需ant clean(现在还应该删除 lib 除了 build ). 做蚂蚁有很多方法 <path> 对象,但这更复杂。

相关问题