2014-05-23 63 views
0

爲了與m2e錯誤/問題保持一致,我遵循了提及的解決方案@SL4j m2e fails to load in Eclipse IDEJunit和Selenium的Maven編譯錯誤

我的pom.xml看起來像這樣(我的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>TestJUnit</groupId> 
    <artifactId>TestJUnit</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <dependencies> 
    <dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.11</version> 
    <type>jar</type> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>org.seleniumhq.selenium</groupId> 
    <artifactId>selenium-java</artifactId> 
    <version>2.41.0</version> 
    <type>jar</type> 
</dependency> 
<dependency> 
    <groupId>org.apache.poi</groupId> 
    <artifactId>poi</artifactId> 
    <version>3.10-FINAL</version> 
</dependency> 
    <dependency> 
     <groupId>org.seleniumhq.selenium</groupId> 
     <artifactId>selenium-chrome-driver</artifactId> 
     <version>2.41.0</version> 
    </dependency> 

    <dependency> 
     <groupId>org.hamcrest</groupId> 
     <artifactId>hamcrest-core</artifactId> 
     <version>1.3</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.7</source> 
      <target>1.7</target> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

但是當我做了mvn compile我得到以下類型的錯誤:

[ERROR] /path:/to/file/<foldername>/<JUnitClassName>.java:[9,17] package org.junit does 
not exist 

我對maven來說是新的,有什麼我在這裏做錯了。

+0

可以運行與debbuger在Eclipse中打開命令。看起來像junit jar沒有正確下載或junit版本崩潰在m2.you谷歌如何轉到eclipse中的調試選項 – vikeng21

+0

你是否在位於「主」包中的類中使用JUnit? http://stackoverflow.com/questions/12403497/maven-erro-package-org-junit-does-not-exist – jhamon

+0

@Trichoko感謝您指出。問題是由於我使用測試。正如我根據下面提供的解決方案來解決這個問題,我可以解決這個問題。 – Segmented

回答

2

在你<dependencies>列表中,您指定的JUnit與test範圍,這意味着它僅包含在罐子裏,當你做mvn test,但不包括在mvn compile。如果您希望JUnit包含在您的最終罐中,請將<scope>test</scope>更改爲<scope>compile</scope>,或者您可以刪除叉齒,它們都執行相同的操作。

TL; DR使用mvn test來測試您的應用程序;或在junit dependeny decleration中將<scope>test</scope>更改爲<scope>compile</scope>

我做了同樣的事情時,我使用maven第一次開始,幾乎每個人都沒有:P

+0

太棒了!像魔術般工作:) – Segmented