java - Mavn執行測試時<scope>test</scope>導致錯誤
問題描述
學習maven test時,執行mvn test時,會找不到org.junit在pom.xml中已經引入
<dependencies><dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version></dependency><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope></dependency> </dependencies>
報錯信息如下文件目錄如下
hello目錄下存在如下文件
其中GreeterTest為測試
執行mvn compile 或者mvn package也會報錯
當把pom.xml中junit依賴的scope去掉時,編譯和測試都能成功。
<dependencies><dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version></dependency><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version></dependency> </dependencies>
造成這個的原因是什么?maven在執行compile時同時編譯*Test的文件嗎,那么為什么mvn test也不能成功?mvn test不是會自動執行*Test的文件嗎?而且scope test確定了測試時會引入junit
問題解答
回答1:這個問題其實你因為你不熟悉maven文件結構所致.測試類一般是放在src/test/java,而不是放在src/main/java下.maven在編譯的時候,src/main/java下是不引用<scope>test</scope>的jar,而編譯src/test/java下的測試這會引用<scope>test</scope>的jar
相關文章:
1. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變2. html - vue項目中用到了elementUI問題3. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?4. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。5. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?6. javascript - 有什么比較好的網頁版shell前端組件?7. java - 如何寫一個intellij-idea插件,實現編譯時修改源代碼的目的8. javascript - [js]為什么畫布里不出現圖片呢?在線等9. java 中Long 類型如何轉換成Double?10. html5 - 有可以一次性把所有 css外部樣式轉為html標簽內style=" "的方法嗎?
