2013-02-22 80 views
23

我想測試添加到我的小項目(請注意我刪除了一些位從代碼&改變包名,所以如果有你看不到任何關於錯誤這可能是沒有這一點;)):的Maven沒有找到org.junit即使它在依賴

package my.pckg; 


import static org.junit.Assert.*; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 


public class SignedRequestCallbackTest { 


    @Before 
    public void setUp() throws Exception { 
    } 

    @After 
    public void tearDown() throws Exception { 
    } 

    @Test 
    public void testCorrectSignedRequest() { 
     assertTrue(false); 
    } 

} 

(我也試圖從測試用例,以消除靜電導入延伸,但它並沒有幫助)

運行mvn test後,它顯示我的錯誤,它找不到org.junit:

[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building Test Server 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ server --- 
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent! 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ server --- 
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent! 
[INFO] Compiling 9 source files to /Users/michael/Projects/fbmuc/server/target/classes 
[INFO] ------------------------------------------------------------- 
[ERROR] COMPILATION ERROR : 
[INFO] ------------------------------------------------------------- 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[4,27] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[6,16] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[7,16] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[8,16] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[14,9] cannot find symbol 
symbol : class Before 
location: class my.pckgSignedRequestCallbackTest 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[18,9] cannot find symbol 
symbol : class After 
location: class my.pckgSignedRequestCallbackTest 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[22,9] cannot find symbol 
symbol : class Test 
location: class my.pckgSignedRequestCallbackTest 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[24,12] cannot find symbol 
symbol : method assertTrue(boolean) 
location: class my.pckgSignedRequestCallbackTest 
[INFO] 9 errors 
[INFO] ------------------------------------------------------------- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.161s 
[INFO] Finished at: Fri Feb 22 18:02:37 CET 2013 
[INFO] Final Memory: 8M/81M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project server: Compilation failure: Compilation failure: 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[4,27] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[4,4] static import only from classes and interfaces 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[6,16] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[7,16] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[8,16] package org.junit does not exist 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[14,9] cannot find symbol 
[ERROR] symbol : class Before 
[ERROR] location: class my.pckgSignedRequestCallbackTest 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[18,9] cannot find symbol 
[ERROR] symbol : class After 
[ERROR] location: class my.pckgSignedRequestCallbackTest 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[22,9] cannot find symbol 
[ERROR] symbol : class Test 
[ERROR] location: class my.pckgSignedRequestCallbackTest 
[ERROR] ~/code/src/test/java/my/pckg/SignedRequestCallbackTest.java:[24,12] cannot find symbol 
[ERROR] symbol : method assertTrue(boolean) 
[ERROR] location: class my.pckgSignedRequestCallbackTest 
[ERROR] -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

我的pom.xml看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.example</groupId> 
    <artifactId>server</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>TestServer</name> 
    <description>Test</description> 
    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <!-- Facebook library --> 
     <dependency> 
      <groupId>com.restfb</groupId> 
      <artifactId>restfb</artifactId> 
      <version>1.6.11</version> 
     </dependency> 
     <!-- Gson: Java to Json conversion --> 
     <dependency> 
      <groupId>com.google.code.gson</groupId> 
      <artifactId>gson</artifactId> 
      <version>2.2.2</version> 
      <scope>compile</scope> 
     </dependency> 
     <!-- Tigase server snapshot --> 
     <dependency> 
      <groupId>tigase</groupId> 
      <artifactId>tigase-server</artifactId> 
      <version>5.2.0-SNAPSHOT</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-codec</groupId> 
      <artifactId>commons-codec</artifactId> 
      <version>1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <repositories> 
     <repository> 
      <id>tigase</id> 
      <name>Tigase repository</name> 
      <url>http://maven.tigase.org</url> 
     </repository> 
     <repository> 
      <id>tigase-snapshot</id> 
      <name>Tigase repository</name> 
      <url>http://build.xmpp-test.net/maven/</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> 
    </repositories> 
</project> 

MVN依賴:樹:

[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building Test Server 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ server --- 
[INFO] my.pckg:server:jar:0.0.1-SNAPSHOT 
[INFO] +- com.restfb:restfb:jar:1.6.11:compile 
[INFO] +- com.google.code.gson:gson:jar:2.2.2:compile 
[INFO] +- tigase:tigase-server:jar:5.2.0-SNAPSHOT:compile 
[INFO] | +- tigase:tigase-utils:jar:3.4.1-SNAPSHOT:compile 
[INFO] | \- tigase:tigase-xmltools:jar:3.4.3-SNAPSHOT:compile 
[INFO] +- commons-codec:commons-codec:jar:1.7:compile 
[INFO] \- junit:junit:jar:4.11:test 
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 2.240s 
[INFO] Finished at: Fri Feb 22 18:07:55 CET 2013 
[INFO] Final Memory: 5M/81M 
[INFO] ------------------------------------------------------------------------ 

MVN版本:

Apache Maven 3.0.3 (r1075438; 2011-02-28 18:31:09+0100) 
Maven home: /usr/share/maven 
Java version: 1.6.0_41, vendor: Apple Inc. 
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 
Default locale: de_CH, platform encoding: MacRoman 
OS name: "mac os x", version: "10.8.2", arch: "x86_64", family: "mac" 

請注意,我不是專家在Java中或在Maven中,剛入門(尤其是使用maven)。 從我的其他文章和問題,看到了,我應該有內src/test/java我的測試中,我的「真實」的代碼中src/main/java - 我已經做了這個樣子。

此外,我除去,一旦整個〜/ .m2目錄/文件夾,它仍然沒有奏效。

我也沒有跑mvn test -X但它並沒有幫助我。如果我應該發佈它,請告訴我。

回答

39

除非您有充分的理由,否則您不應該在POM的<build>元素中覆蓋您的<sourceDirectory>設置。此屬性決定了Maven在哪裏尋找非測試代碼。該屬性的默認值是src/main/java。該<testSourceDirectory>屬性設置此默認爲src/test/java路徑測試代碼(通過設置<sourceDirectory>簡單src,Maven的認爲整個目錄包含主應用程序代碼。由於src目錄包含src/test/java,Maven就會試圖編譯測試代碼作爲主應用程序的一部分。

這是顯著因爲(在compile相)編譯主應用程序時,Maven的後省略依賴性與test範圍。測試代碼在一個單獨的相(test-compile相)編譯主要的編譯。

因此,由於Maven試圖將您的測試代碼編譯爲主應用程序的一部分,因此省略了junit依賴項,並且它們在類路徑中不可用。這裏的解決方案是不在POM中指定<sourceDirectory>元素。

+0

非常感謝!那真是我想到的最後一件事情,完全可以理解。我甚至不知道爲什麼它會出現這樣的情況..我想日食是這樣做的,因爲我創建了新項目並將Maven添加到了它中......再次感謝! – 2013-02-22 20:38:06

2

默認情況下,行家分別着眼於這些文件夾用於Java和測試類 - 的src /主/ java和SRC /測試/ JAVA

當與下源的測試類和junit的範圍被指定在src由於maven找不到test-org.unit,因此提到了pom.xml中的依賴關係。