简体   繁体   English

使用常春藤解决具有相同依赖关系的两个工件

[英]Resolve two artifacts in same dependency using ivy

I'm having trouble resolving kafka dependencies using ivy. 我在使用常春藤解决kafka依赖项时遇到问题。

I am trying to create a kafka server in a junit test. 我正在尝试在junit测试中创建一个kafka服务器。

TestUtils.createServer(kafkaConfig, Time.SYSTEM);

This requires kafka_2.11-0.10.2.1.jar , kafka-clients-0.10.2.1-test.jar , and kafka_2.11-0.2.1-test.jar to be on the test class path since kafka.utils.TestUtils is in the *-test.jar . 这要求kafka_2.11-0.10.2.1.jarkafka-clients-0.10.2.1-test.jarkafka_2.11-0.2.1-test.jar处于测试类路径上,因为kafka.utils.TestUtils为在*-test.jar The problem is how can I resolve the *-test.jar using ivy and ant? 问题是如何使用常春藤和蚂蚁解析*-test.jar In the ivy.xml I have: ivy.xml我有:

<configurations defaultconfigmapping="default>
    <conf name="compile"/>
    <conf name="test" extends="compile"/>
    <conf name="master"/>
    <conf name="sources"/>
    <conf name="javadoc"/>
    <conf name="runtime" extends="compile"/>
    <conf name="default" extends="master,runtime"/>
</configurations>
...
    <dependency org="org.apache.kafka" name="kafka-clients" rev="0.10.2.1" conf="compile->default"/>
    <dependency org="org.apache.kafka" name="kafka_2.11" rev="0.10.2.1" conf="test->default">
        <artifact name="kafka_2.11" ext="jar"/>
        <artifact name="kafka_2.11" e:classifier="test" ext="jar"/>
    </dependency>
    <dependency org="org.apache.kafka" name="kafka-clients" rev="0.10.2.1" conf="test->default">
        <artifact name="kafka-clients" ext="jar"/>
        <artifact name="kafka-clients" e:classifier="test" ext="jar"/>
    </dependency>

The build has something similar this 该版本有类似的东西

<ivy:resolve log="quiet" conf="compile" transitive="false"/>
<ivy:cachepth pathid="test-classpath" conf="test" log="quiet"/>

This results in NoClassDefFoundError org/apache/kafka/test/TestUtils . 这导致NoClassDefFoundError org/apache/kafka/test/TestUtils

I noticed that conf is set to "compile" but when I add test to the list I get other errors for missing classes. 我注意到conf设置为“编译”,但是当我将test添加到列表中时,由于缺少类,我会遇到其他错误。

Don't set the transitive dependency resolution to false. 不要将传递依赖项解析设置为false。 You'll lose one of the major benefits of using ivy. 您将失去使用常春藤的主要好处之一。

Instead create configuration mappings in each of your dependencies. 而是在每个依赖项中创建配置映射。

Example

The following contrived example saves the dependencies in separate directories to illustrate which jars are on the desired classpath. 下面的示例将依赖项保存在单独的目录中,以说明所需的类路径上有哪些jar。 As you can see the kafka client jars are downloaded along with the jars they depend on (the transitive dependencies). 如您所见,kafka客户端jar和它们依赖的jar一起下载(传递依赖项)。

├── build.xml
├── ivy.xml
└── target
    └── lib
        ├── compile
        │   ├── kafka-clients-0.10.2.1.jar
        │   ├── lz4-1.3.0.jar
        │   ├── slf4j-api-1.7.21.jar
        │   └── snappy-java-1.1.2.6.jar
        └── test
            ├── kafka-clients-0.10.2.1.jar
            ├── kafka-clients-test-0.10.2.1.jar
            ├── lz4-1.3.0.jar
            ├── slf4j-api-1.7.21.jar
            └── snappy-java-1.1.2.6.jar

build.xml build.xml文件

The retrieve task is used to save the files under the "target/lib" directory. 检索任务用于将文件保存在“ target / lib”目录下。 The ivycachepath task is used to create an ANT cache with the appropriate files. ivycachepath任务用于使用适当的文件创建ANT缓存。

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="build">
        <ivy:retrieve pattern="${build.dir}/lib/[conf]/[artifact](-[classifier])-[revision].[ext]"/>

        <ivy:cachepath pathid="compile-classpath" conf="compile"/>
        <ivy:cachepath pathid="test-classpath" conf="test"/>
    </target>

</project>

ivy.xml 的ivy.xml

The magic is in the configuration mappings "compile->default" or "test->default". 神奇之处在于配置映射“ compile-> default”或“ test-> default”。

<ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="test" description="Additional test dependencies" extends="compile"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.apache.kafka" name="kafka-clients" rev="0.10.2.1" conf="compile->default"/>

        <!-- test dependencies -->
        <dependency org="org.apache.kafka" name="kafka-clients" rev="0.10.2.1" conf="test->default">
            <artifact name="kafka-clients"/>
            <artifact name="kafka-clients" e:classifier="test"/>
        </dependency>
    </dependencies>

</ivy-module>

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

相关问题 常春藤解决动态依赖 - ivy resolve dynamic dependency 常春藤无法解决依赖关系的范围,它是传递依赖关系的依赖关系 - Ivy can not resolve the scope of a dependency which is a dependency of a transitive dependency 模块 X 的多个工件被检索到 Apache Ivy 中的同一个文件? - Multiple artifacts of the module X are retrieved to the same file in Apache Ivy? 如何使用Apache Ivy解决多个文件的依赖关系? - How to use Apache ivy to resolve dependency with multiple files? 常春藤未能解决依赖:要检查什么,该怎么办? - Ivy failed to resolve dependency : What to check, what to do? sts/eclipse 锁定版本化的 jar,因此 ivy 无法解决依赖关系 - sts/eclipse lock the versioned jar and hence ivy does not resolve dependency 使用TFS工件作为Maven依赖项 - Using TFS Artifacts as a Maven Dependency 如何解决模块X的多个工件被检索到Apache Ivy中的同一文件? - How do I solve Multiple artifacts of the module X are retrieved to the same file in Apache Ivy? 使用Ivy或Maven之类的工具,是否可以识别仅源的依赖项? - Using a tool like Ivy or Maven, is it possible to identify a dependency that is source only? 使用常春藤以编程方式解决POM依赖关系,但不解决源代码和javadoc - Programatically resolving POM dependency using ivy, but not source and javadoc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM