2015-04-14 40 views
1

我是Maven的新手,所以如果這太基本了,請原諒我。我已經搜索了這個問題,每個人似乎都解決了從pom.xml文件中移除標籤的問題。Maven:包org.junit不存在

我沒有這個標籤有:

<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>GraphApp</groupId> 
    <artifactId>GraphApp</artifactId> 
    <version>1.0.0</version> 
    <name>MyApp</name> 
    <description>MyApp</description> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <testSourceDirectory>test</testSourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

我已經轉換與Eclipse現有的項目,所以這個問題可能來自。

此外,我已經讀過,Maven的默認架構是將代碼存儲在某些默認文件夾中,而我使用的是src/com/romanrdgz/core/myapp/test/com/romanrdgz/test/core。這也可能是問題的根源。

我看到這個錯誤:

Maven: package org.junit does not exist 

我需要做什麼改變嗎?

編輯:好吧,我已經包含JUnit4的依賴性,我也改變了我的文件夾是這樣的: enter image description here

不過,當我去安慰和執行mvn testmvn compile,我得到的以下輸出:

[INFO] Scanning for projects... 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building GraphApp 1.0.0 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ GraphApp - 
-- 
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, 
i.e. build is platform dependent! 
[INFO] skip non existing resourceDirectory C:\Users\rrrp\workspace\GraphApp\src\ 
main\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ GraphApp --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 0.515 s 
[INFO] Finished at: 2015-04-15T08:15:06+02:00 
[INFO] Final Memory: 11M/309M 
[INFO] ------------------------------------------------------------------------ 

我試過擦除bin和目標文件夾,但仍然是相同的。看起來它沒有找到類和測試。

任何幫助?

+0

請張貼實際的堆棧跟蹤。 – beerbajay

+0

將junit依賴關係添加到pom – Reimeus

+0

@Reimeus嘗試過,但eclipse將其顯示爲錯誤。我應該在哪裏添加它? –

回答

1

添加了JUnit依賴的POM文件的依賴關係部分

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

..... 
    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
     </dependency> 
    </dependencies> 
</project> 
+0

好的,這完全有效。我錯了。快速提問:如果我使用「mvn test」從控制檯啓動所有測試,我會收到警告「使用platgorm編碼(實際上是Cp1252)來複制已過濾的資源」和信息「跳過不存在的資源目錄C:\ Users \ myuser \ workspace \ MyApp \ SRC \測試\資源」。我的測試不在那裏,但在\ MyApp \ test中。如何改變POM? –

+0

但是,我必須堅持默認的Maven文件夾結構嗎?那是哪一個?或者我只需要創建一個空的test \ resources文件夾? –

+1

你不必但我會 - 更容易 – Reimeus