Maven - 测试中的不同依赖版本

Eng*_*_DJ 1 hadoop maven cloudera

我遇到了类似于Maven 2的问题- 测试和编译中的不同依赖版本但是指定的答案不起作用.

在我的项目中,我需要依赖于Hadoop的Cloudera发行版和JUnit测试的"vanilla"版本,因为前者仅适用于*nix.

当我尝试执行我的应用程序时,我得到了Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration.当我从Maven或Eclipse运行JUnit测试时,一切正常.如果我注释掉test依赖项,应用程序将成功运行.

为什么在取消注释依赖项compile时会忽略test依赖关系?

    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>0.20.2-cdh3u2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-test</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

mvn dependency:list显示以下内容,它根本不显示compile范围版本:

[INFO] The following files have been resolved:
[INFO]    ant:ant:jar:1.6.5:test
[INFO]    aopalliance:aopalliance:jar:1.0:compile
[INFO]    asm:asm:jar:3.3.1:compile
[INFO]    cglib:cglib:jar:2.2.2:compile
[INFO]    ch.qos.logback:logback-classic:jar:1.0.0:compile
[INFO]    ch.qos.logback:logback-core:jar:1.0.0:compile
[INFO]    com.google.guava:guava:jar:r08:compile
[INFO]    com.h2database:h2:jar:1.3.164:test
[INFO]    com.jolbox:bonecp:jar:0.7.1.RELEASE:compile
[INFO]    com.sun.jersey:jersey-core:jar:1.11:test
[INFO]    commons-beanutils:commons-beanutils:jar:1.7.0:test
[INFO]    commons-beanutils:commons-beanutils-core:jar:1.8.0:test
[INFO]    commons-cli:commons-cli:jar:1.2:test
[INFO]    commons-codec:commons-codec:jar:1.4:test
[INFO]    commons-collections:commons-collections:jar:3.2.1:test
[INFO]    commons-configuration:commons-configuration:jar:1.6:test
[INFO]    commons-digester:commons-digester:jar:1.8:test
[INFO]    commons-el:commons-el:jar:1.0:test
[INFO]    commons-httpclient:commons-httpclient:jar:3.0.1:test
[INFO]    commons-lang:commons-lang:jar:2.4:test
[INFO]    commons-logging:commons-logging:jar:1.1.1:compile
[INFO]    commons-net:commons-net:jar:1.4.1:test
[INFO]    hsqldb:hsqldb:jar:1.8.0.10:test
[INFO]    junit:junit:jar:4.10:test
[INFO]    mysql:mysql-connector-java:jar:5.1.18:compile
[INFO]    net.java.dev.jets3t:jets3t:jar:0.7.1:test
[INFO]    net.sf.kosmosfs:kfs:jar:0.3:test
[INFO]    org.apache.commons:commons-math:jar:2.1:test
[INFO]    org.apache.ftpserver:ftplet-api:jar:1.0.0:test
[INFO]    org.apache.ftpserver:ftpserver-core:jar:1.0.0:test
[INFO]    org.apache.ftpserver:ftpserver-deprecated:jar:1.0.0-M2:test
[INFO]    org.apache.hadoop:hadoop-core:jar:1.0.0:test
[INFO]    org.apache.hadoop:hadoop-test:jar:1.0.0:test
[INFO]    org.apache.mina:mina-core:jar:2.0.0-M5:test
[INFO]    org.codehaus.jackson:jackson-core-asl:jar:1.0.1:test
[INFO]    org.codehaus.jackson:jackson-mapper-asl:jar:1.0.1:test
[INFO]    org.eclipse.jdt:core:jar:3.1.1:test
[INFO]    org.hamcrest:hamcrest-core:jar:1.1:test
[INFO]    org.liquibase:liquibase-core:jar:2.0.3:test
[INFO]    org.liquibase.ext:liquibase-slf4j:jar:0.0.1:test
[INFO]    org.mortbay.jetty:jetty:jar:6.1.26:test
[INFO]    org.mortbay.jetty:jetty-util:jar:6.1.26:test
[INFO]    org.mortbay.jetty:jsp-2.1:jar:6.1.14:test
[INFO]    org.mortbay.jetty:jsp-api-2.1:jar:6.1.14:test
[INFO]    org.mortbay.jetty:servlet-api:jar:2.5-20081211:test
[INFO]    org.mortbay.jetty:servlet-api-2.5:jar:6.1.14:test
[INFO]    org.slf4j:jcl-over-slf4j:jar:1.6.4:compile
[INFO]    org.slf4j:log4j-over-slf4j:jar:1.6.4:compile
[INFO]    org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO]    org.springframework:spring-aop:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-asm:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-beans:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-context:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-context-support:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-core:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-expression:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-jdbc:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework:spring-test:jar:3.1.1.RELEASE:test
[INFO]    org.springframework:spring-tx:jar:3.1.1.RELEASE:compile
[INFO]    org.springframework.data:spring-data-hadoop:jar:1.0.0.BUILD-SNAPSHOT:c
ompile
[INFO]    oro:oro:jar:2.0.8:test
[INFO]    tomcat:jasper-compiler:jar:5.5.12:test
[INFO]    tomcat:jasper-runtime:jar:5.5.12:test
[INFO]    xmlenc:xmlenc:jar:0.52:test
Run Code Online (Sandbox Code Playgroud)

Vin*_*lds 6

你想要什么,不再适用于Maven 3(这曾经对Maven 2有效).Maven 3将尝试获取最近的依赖项,有效地确保编译和测试阶段中只有一个编译或测试范围依赖项用于编译和测试阶段.

在你的情况下org.apache.hadoop:hadoop-core:1.0.0:test覆盖org.apache.hadoop:hadoop-core:0.20.2-cdh3u2:compile,因此成为最接近的依赖.在运行mvn dependency:list目标时,您可能会看到Maven显示以下警告,这暗示您的项目模型存在问题:

[警告]'dependencies.dependency.(groupId:artifactId:type:classifier)'必须是唯一的:org.apache.hadoop:hadoop-core:jar - >版本0.20.2-cdh3u2 vs 1.0.0 @ line xyz,column XYZ

要"修复"这个,最好将测试拆分成一个单独的项目,其项目模型可以定义一组独立的测试依赖项.