Java - Apache Ivy 没有正确解析依赖关系

Java - Apache Ivy not resolving dependencies correctly

我无法通过终端使用 Ant 编译源代码,因为尽管我发出了 'ant resolve or ant retrieve'?

,但似乎依赖关系没有正确解析

我的build.xml和下面的ivy.xml

build.xml

<!-- ANT HOME ENVIRONMENT VARIABLE -->
<property name="ant.home" value="${env.ANT_HOME}" />

<!-- IVY HOME DIRECTORY -->
<property name="ivy.home" value="${ant.home}" />

<!-- IVY2 JAR DIRECTORY (REPOSITORY) -->
<property name="ivy.default.ivy.user.dir" value="${user.home}/.ivy2"/>

<!-- DOWNLOAD IVY -->
<target name="setup" description="Install ivy">
    <mkdir dir="${user.home}/.ivy2" />
    <get dest="${ivy.home}/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
</target>

<!-- RESOLVE CLASSPATHS -->
<target name="resolve" description="Use ivy to resolve classpaths">
    <ivy:resolve file="ivy.xml" />
    <ivy:report todir='target/ivy-reports' graph='false' xml='false'/>
    <ivy:cachepath pathid="ivy.path" conf="compile" />
</target>

<!-- RETRIEVE DEPENDANCIES AFTER RESOLVING-->
<target name="retrieve" depends="resolve" description="Use ivy to retrieve dependencies">
    <ivy:retrieve sync="true" type="jar" />
</target>

<!-- COMPILE PROJECT -->
<target name="compile" depends="clean, retrieve">
    <!-- Create build directory -->
    <mkdir dir="target/${ant.project.name}" />

    <!-- Compile source code -->
    <javac includeantruntime="false" srcdir="src" debug="true" destdir="target/${ant.project.name}" >
        <classpath>
            <path refid="ivy.path" />
        </classpath>
    </javac>
</target>

<!-- CLEAN TARGET DIRECTORY -->
<target name="clean">
    <delete dir="target/orderlycalls" />
    <delete dir="target/classes" />
    <delete dir="target/ivy-reports" />     
</target>

<!-- CLEAN TARGET AND IVY CATCHE -->
<target name="clean-all" depends="clean" description="Additionally purge ivy cache">
    <ivy:cleancache/>
</target>

ivy.xml

<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">

    <configurations>
        <conf name="default" visibility="public" description="The single built artifact. Nothing else"/>
        <conf name="compile" visibility="public" description="The master module and transitive dependencies"/>
        <conf name="provided" visibility="public" description="Needed for compile. Will be provided outside or war"/>
        <conf name="runtime" visibility="public" description="Not required for compile, but for runtime" extends="compile"/>
        <conf name="default" visibility="public" description="The default configuration" extends="runtime"/>
        <conf name="test" visibility="private" description="Required for testing" extends="runtime"/>
    </configurations>

    <dependencies>
        <dependency org="net.sf.trove4j" name="trove4j" rev="3.0.3" conf="provided"/>
        <dependency org="org.apache.tomcat.embed" name="tomcat-embed-core" rev="7.0.53" conf="provided"/>
    </dependencies>

</ivy-module>

当我 运行 'ant compile' 编译蚂蚁抱怨它找不到 'Servlet Context' 时,它是 tomcat.jar 或 'TObject' 的一部分,'THashMap' 是 trove.jar 的一部分,还有更多尽管我是 retrieving/resolving build.xml 中的罐子。

我注意到的另一件事是,在我的 .ivy2/cache// 中没有实际的 jar 文件。只有 xml 个文件

知道我做错了什么或根本没做吗?

谢谢

您已使用 cachepath 任务创建 Ant Path,使用与 "compile" 配置关联的依赖项。

<ivy:cachepath pathid="ivy.path" conf="compile" />

你的问题出在你的 ivy 文件上,你没有指定任何依赖映射到 "compile" 配置。这可以解释为什么您的 javac 任务看不到任何 jars。

   <configurations>
        ...
        <conf name="compile" .../>
        <conf name="provided" .../>
        ...
    </configurations>

    <dependencies>
        <dependency ... conf="provided"/>
        <dependency ... conf="provided"/>
    </dependencies>

我建议为您的每个配置创建一个显式映射,例如:

<!-- Compile dependencies --> 
<dependency org="net.sf.trove4j" name="trove4j" rev="3.0.3" conf="compile->default"/>

<!-- provided dependencies -->
<dependency org="org.apache.tomcat.embed" name="tomcat-embed-core" rev="7.0.53" conf="provided->master"/>

您唯一需要的映射是以下远程配置:

  • 默认 远程 jar 及其传递依赖项
  • master 仅远程 jar

有关 ivy 如何解释远程 maven 模块的更多信息,请阅读以下内容:

  • How are maven scopes mapped to ivy configurations by ivy