java - Mavn執(zhí)行測試時<scope>test</scope>導(dǎo)致錯誤
問題描述
學(xué)習(xí)maven test時,執(zhí)行mvn test時,會找不到org.junit在pom.xml中已經(jīng)引入
<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>
報(bào)錯信息如下文件目錄如下
hello目錄下存在如下文件
其中GreeterTest為測試
執(zhí)行mvn compile 或者mvn package也會報(bào)錯
當(dāng)把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在執(zhí)行compile時同時編譯*Test的文件嗎,那么為什么mvn test也不能成功?mvn test不是會自動執(zhí)行*Test的文件嗎?而且scope test確定了測試時會引入junit
問題解答
回答1:這個問題其實(shí)你因?yàn)槟悴皇煜aven文件結(jié)構(gòu)所致.測試類一般是放在src/test/java,而不是放在src/main/java下.maven在編譯的時候,src/main/java下是不引用<scope>test</scope>的jar,而編譯src/test/java下的測試這會引用<scope>test</scope>的jar
相關(guān)文章:
1. docker容器呢SSH為什么連不通呢?2. docker網(wǎng)絡(luò)端口映射,沒有方便點(diǎn)的操作方法么?3. nignx - docker內(nèi)nginx 80端口被占用4. angular.js - angular內(nèi)容過長展開收起效果5. css - chrome瀏覽器input記錄上次cookie信息后,有個黃色背景~如何去除!6. docker綁定了nginx端口 外部訪問不到7. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?8. macos - mac下docker如何設(shè)置代理9. php - 第三方支付平臺在很短時間內(nèi)多次異步通知,訂單多次確認(rèn)收款10. 前端 - ng-view不能加載進(jìn)模板
