Ant Ivy 不会设置编译类路径

Posted

技术标签:

【中文标题】Ant Ivy 不会设置编译类路径【英文标题】:Ant Ivy won't set compile classpath 【发布时间】:2020-09-04 06:25:55 【问题描述】:

我在网上阅读的所有内容都表明我这样做是正确的,但 Ant/Ivy 拒绝设置我的编译类路径。这是我的ivy.xml

<ivy-module version="2.0">
    <info organisation="com.latencyzero.missiondb" module="webapp"/>

    <configurations defaultconfmapping="runtime->*">
        <conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/>
        <conf name="provided" visibility="public" description="this is much like compile, but indicates you expect the JDK or a container to provide it. It is only available on the compilation classpath, and is not transitive."/>
        <conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/>
        <conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/>
    </configurations>

    <dependencies>
        <dependency org="org.apache.logging.log4j"              name="log4j"                        rev="2.13.3"            conf="compile->default"/>
    </dependencies>
</ivy-module>

以及build.xml的相关部分:

    <target name="resolve" description="==> retrieve dependencies with Ivy">
        <ivy:retrieve/>

        <ivy:cachepath pathid="compile.classpath" conf="compile" />
    </target>
    <target name="compile" depends="resolve" description="==> Compile Java sources">
        <mkdir  dir="$obj"/>

        <javac  destdir="$obj"
                source="$sourceVersion"
                target="$targetClassVersion"
                encoding="UTF-8"
                deprecation="$deprecation"
                nowarn="$suppressWarnings"
                debug="on"
                listfiles="false"
                includeantruntime="false">
            <classpath refid="compile.classpath"/>

            <src path="$common"/>
            <src path="$src"/>
        </javac>
    </target>

当我运行ant -v compile 时,它会下载 log4j.jar 文件,然后忽略它:

[ivy:retrieve] :: Apache Ivy 2.4.0 - 20141213170938 :: http://ant.apache.org/ivy/ ::
...
[ivy:retrieve] main: module revision found in cache: org.apache.logging.log4j#log4j;2.13.3
[ivy:retrieve]  found org.apache.logging.log4j#log4j;2.13.3 in public
...
    [javac] Compiling 94 source files to .../repo/branches/v1.0/target/object
    [javac] Using modern compiler
    [javac] Compilation arguments:
    [javac] '-deprecation'
    [javac] '-d'
    [javac] '.../repo/branches/v1.0/target/object'
    [javac] '-classpath'
    [javac] '.../repo/branches/v1.0/target/object'
    [javac] '-sourcepath'
    [javac] '.../repo/branches/v1.0/lib/common/src:.../repo/branches/v1.0/src/main/java'
    [javac] '-target'
    [javac] '1.7'
    [javac] '-encoding'
    [javac] 'UTF-8'
    [javac] '-g'
    [javac] '-source'
    [javac] '1.7'
    [javac] 
    [javac] Files to be compiled:
...
    [javac] ...dao/AbstractDAOHibernate.java:23: error: package org.apache.logging.log4j does not exist
    [javac] import org.apache.logging.log4j.Logger;
    [javac]                                ^

注意类路径只有目标目录,没有 jars。我一定遗漏了一些明显的东西,但我肯定看不到。

【问题讨论】:

【参考方案1】:

所以,原来我应该指定的依赖项是:

<dependency org="org.apache.logging.log4j" name="log4j-api" rev="2.13.3">
    <exclude module="log4j-api-java9"/>
</dependency>

我需要exclude,否则 Ivy 尝试下载不存在的模块失败。

Ivy 在传递依赖方面似乎也有问题,所以我放弃了它并切换到了 Maven。

【讨论】:

以上是关于Ant Ivy 不会设置编译类路径的主要内容,如果未能解决你的问题,请参考以下文章

Ant、Ivy 和 JUnit 找不到类 - build.xml 中的错误?

修改 ivy 类路径条目

如何使用 ivy api 以编程方式为缓存中的模块构建路径?

如何使用 ant 构建多个具有依赖关系的项目

[options] 未与 -source 1.6 一起设置引导类路径

在旧的 Ant 项目中添加 Ivy