[英]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.jar
, kafka-clients-0.10.2.1-test.jar
和kafka_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. 而是在每个依赖项中创建配置映射。
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
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>
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.