我有一個Maven項目,在我的Mac上編譯得很好 - 但我第一次將它移到我的Windows 10 PC上,並且發現我的JUnit測試出現奇怪的編譯問題。失敗是:編譯JUnit測試時從Maven拋出的ClassNotFoundException
stringContainingEmojiWithTrailingSpaceIsDetected(com.myapp.EmojiUtilTest) Time elapsed: 0.002 sec <<< ERROR!
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JavaType
at emoji4j.EmojiUtils.removeAllEmojis(EmojiUtils.java:241)
...
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JavaType
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
這似乎是一個傳遞依賴:
<dependency>
<groupId>com.kcthota</groupId>
<artifactId>emoji4j</artifactId>
<version>5.0</version>
</dependency>
作爲mvn dependency:tree
包含:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ myapp ---
[INFO] com.craigotis.myapp:myapp:jar:0.0.1-SNAPSHOT
[INFO] +- com.google.code.gson:gson:jar:2.8.0:compile
[INFO] +- mysql:mysql-connector-java:jar:6.0.5:compile
[INFO] +- org.springframework:spring-jdbc:jar:4.3.4.RELEASE:compile
[INFO] | +- org.springframework:spring-beans:jar:4.3.4.RELEASE:compile
[INFO] | +- org.springframework:spring-core:jar:4.3.4.RELEASE:compile
[INFO] | \- org.springframework:spring-tx:jar:4.3.4.RELEASE:compile
[INFO] +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] +- org.apache.commons:commons-dbcp2:jar:2.0.1:compile
[INFO] | +- org.apache.commons:commons-pool2:jar:2.2:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- com.ibm.icu:icu4j:jar:58.1:compile
[INFO] \- com.kcthota:emoji4j:jar:5.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.7.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.0:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.7.0:compile
[INFO] \- com.googlecode.lambdaj:lambdaj:jar:2.3.3:compile
[INFO] +- org.hamcrest:hamcrest-all:jar:1.1:compile
[INFO] +- org.objenesis:objenesis:jar:1.2:compile
[INFO] \- cglib:cglib-nodep:jar:2.2:compile
但我似乎無法理解爲什麼類沒有找到 - 如果我檢查我的Maven存儲庫,JAR存在:
~\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.7.0\jackson-databind-2.7.0.jar
與JAR中的JavaType.class
類一樣。
手動添加jackson-databind
依賴項到我的POM似乎沒有什麼區別。
什麼會導致此類在測試編譯期間未被Maven找到,該機器上的?
編輯:此外,如果我有調試日誌記錄運行Maven,我看到:
[DEBUG] test classpath classpath:
[DEBUG] C:\Users\Craig\projects\myproject\target\test-classes
[DEBUG] C:\Users\Craig\projects\myproject\target\classes
[DEBUG] C:\Users\Craig\.m2\repository\com\google\code\gson\gson\2.8.0\gson-2.8.0.jar
[DEBUG] C:\Users\Craig\.m2\repository\mysql\mysql-connector-java\6.0.5\mysql-connector-java-6.0.5.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\springframework\spring-jdbc\4.3.4.RELEASE\spring-jdbc-4.3.4.RELEASE.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\springframework\spring-beans\4.3.4.RELEASE\spring-beans-4.3.4.RELEASE.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\springframework\spring-core\4.3.4.RELEASE\spring-core-4.3.4.RELEASE.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\springframework\spring-tx\4.3.4.RELEASE\spring-tx-4.3.4.RELEASE.jar
[DEBUG] C:\Users\Craig\.m2\repository\javax\transaction\javax.transaction-api\1.2\javax.transaction-api-1.2.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\apache\commons\commons-dbcp2\2.0.1\commons-dbcp2-2.0.1.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\apache\commons\commons-pool2\2.2\commons-pool2-2.2.jar
[DEBUG] C:\Users\Craig\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\apache\logging\log4j\log4j-api\2.7\log4j-api-2.7.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\apache\logging\log4j\log4j-core\2.7\log4j-core-2.7.jar
[DEBUG] C:\Users\Craig\.m2\repository\junit\junit\4.11\junit-4.11.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar
[DEBUG] C:\Users\Craig\.m2\repository\com\ibm\icu\icu4j\58.1\icu4j-58.1.jar
[DEBUG] C:\Users\Craig\.m2\repository\com\kcthota\emoji4j\5.0\emoji4j-5.0.jar
[DEBUG] C:\Users\Craig\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.7.0\jackson-core-2.7.0.jar
[DEBUG] C:\Users\Craig\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.7.0\jackson-databind-2.7.0.jar
[DEBUG] C:\Users\Craig\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.7.0\jackson-annotations-2.7.0.jar
[DEBUG] C:\Users\Craig\.m2\repository\com\googlecode\lambdaj\lambdaj\2.3.3\lambdaj-2.3.3.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\hamcrest\hamcrest-all\1.1\hamcrest-all-1.1.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\objenesis\objenesis\1.2\objenesis-1.2.jar
[DEBUG] C:\Users\Craig\.m2\repository\cglib\cglib-nodep\2.2\cglib-nodep-2.2.jar
[DEBUG] provider classpath classpath:
[DEBUG] C:\Users\Craig\.m2\repository\org\apache\maven\surefire\surefire-junit4\2.12.4\surefire-junit4-2.12.4.jar
[DEBUG] C:\Users\Craig\.m2\repository\org\apache\maven\surefire\surefire-api\2.12.4\surefire-api-2.12.4.jar
這似乎清楚地列出了傑克遜的JAR文件。
難道MVN依賴的所有輸出:樹目標是什麼?如果不是,可以將它發佈嗎? – davidxxx
謝謝你,但內部沒有潛在的衝突。你如何運行你的測試?用IDE或命令行包裝?如果你轉到命令行並輸入'mvn clean install -DskipTests',然後輸入'mvn test'。你有同樣的問題? – davidxxx
@davidxxx是的,同樣的問題 - '乾淨安裝'結束,但'測試'失敗,同樣的錯誤。 –