Maven - 测试中的不同依赖版本 [英] Maven - Different Dependency Version in Test

查看:170
本文介绍了Maven - 测试中的不同依赖版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到类似于 Maven 2 - 在测试和编译中不同的依赖版本,但指定的答案不起作用。

在我的项目中,我需要依赖Cloudera发布的Hadoop和一个用于JUnit测试的'vanilla'版本,因为前者仅适用于* nix。



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



为什么编译依赖项在 test 依赖项被取消注释时被忽略?

 < dependency> 
< groupId> org.apache.hadoop< / groupId>
< artifactId> hadoop-core< / artifactId>
< version> 0.20.2-cdh3u2< / version>
< scope>编译< / scope>
< /依赖关系>

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

< dependency>
< groupId> org.apache.hadoop< / groupId>
< artifactId> hadoop-test< / artifactId>
< version> 1.0.0< / version>
< scope> test< / scope>
< /依赖关系>

mvn dependency:list 显示以下内容,它根本不显示编译作用域版本:

  [INFO ]下列文件已解决:
[INFO] ant:ant:jar:1.6.5:test
[INFO] aopalliance:aopalliance:jar:1.0:编译
[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 :编译
[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
[信息] 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:罐子: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:编译
[INFO] org.sl f4j:log4j-over-slf4j:jar:1.6.4:编译
[INFO] org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] org.springframework:spring- aop:jar:3.1.1.RELEASE:编译
[INFO] org.springframework:spring-asm:jar:3.1.1.RELEASE:compile
[INFO] org.springframework:spring-beans: jar:3.1.1.RELEASE:编译
[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: 3.1.1.RELEASE:编译
[INFO] org.springframework:spring-jdbc:jar:3.1.1.RELEASE:compile
[INFO] org.springframework:spring-test:jar:3.1。 1.RELEASE:测试
[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
[信息] oro:oro:jar:2.0.8:test
[信息] tomcat:jasper -compiler:jar:5.5.12:test
[INFO] tomcat:jasper-runtime:jar:5.5.12:test
[INFO] xmlenc:xmlenc:jar:0.52:test


解决方案

看起来像一个maven bug - 或者如果没有,如果有任何文件,我不会相信这些文件。

在你的情况中,我可能会:


  1. 升级maven并查看问题是否在最新的m3中修复

  2. 尝试使用其他工件ID或组名重新打包其中一个hadoop版本。 maven-shade-plugin,不要打扰改变包名称,但只介绍一些maven不会知道的与org.apache.hadoop没有什么不同:hadoop-core

  3. 尝试移动测试到一个单独的项目下游

  4. 开始调试maven并明白发生了什么(然后回落到1或2 ...)
  5. ol>

    I'm suffering an issue similar to Maven 2 - different dependency versions in test and compile but the specified answer there does not work.

    In my project I need to depend on a Cloudera distribution of Hadoop and a 'vanilla' version for JUnit testing, as the former only works on *nix.

    When I try and execute my application, I get Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/conf/Configuration. When I run JUnit tests from Maven or Eclipse, everything works fine. If I comment out the test dependencies, the application runs successfully.

    Why is the compile dependency getting ignored when the test dependency is uncommented?

        <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>
    

    mvn dependency:list shows the following, which does not show the compile scoped version at all:

    [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
    

    解决方案

    Looks like a maven bug - or if not, something that is fishy enough I wouldn't trust the documentation if there were any.

    In your case, I would probably:

    1. upgrade maven and see if the issue is fixed in the latest m3
    2. try repackaging one of the hadoop versions with another artifact id or group name. maven-shade-plugin, don't bother changing the package name, but just introduce something maven won't know is no different from org.apache.hadoop:hadoop-core
    3. try moving the tests to a separate project downstream
    4. start debugging maven and understand what's going on exactly (and then fall back on 1 or 2...)

    这篇关于Maven - 测试中的不同依赖版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆