java - Mavn執(zhí)行測(cè)試時(shí)<scope>test</scope>導(dǎo)致錯(cuò)誤
問題描述
學(xué)習(xí)maven test時(shí),執(zhí)行mvn test時(shí),會(huì)找不到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)錯(cuò)信息如下文件目錄如下
hello目錄下存在如下文件
其中GreeterTest為測(cè)試
執(zhí)行mvn compile 或者mvn package也會(huì)報(bào)錯(cuò)
當(dāng)把pom.xml中junit依賴的scope去掉時(shí),編譯和測(cè)試都能成功。
<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>
造成這個(gè)的原因是什么?maven在執(zhí)行compile時(shí)同時(shí)編譯*Test的文件嗎,那么為什么mvn test也不能成功?mvn test不是會(huì)自動(dòng)執(zhí)行*Test的文件嗎?而且scope test確定了測(cè)試時(shí)會(huì)引入junit
問題解答
回答1:這個(gè)問題其實(shí)你因?yàn)槟悴皇煜aven文件結(jié)構(gòu)所致.測(cè)試類一般是放在src/test/java,而不是放在src/main/java下.maven在編譯的時(shí)候,src/main/java下是不引用<scope>test</scope>的jar,而編譯src/test/java下的測(cè)試這會(huì)引用<scope>test</scope>的jar
相關(guān)文章:
1. python - 管道符和ssh傳文件2. initPage:是什么意思? 是返回的意思嗎?3. html - eclipse 標(biāo)簽錯(cuò)誤4. 如何解決docker宿主機(jī)無法訪問容器中的服務(wù)?5. javascript - 如果所有請(qǐng)求都放到actions 里面,那拿到的數(shù)據(jù)應(yīng)該 放在哪里,state 還是vue實(shí)例里面的data?6. mysql - java ResultSetMetaData 獲取中文別名亂碼7. javascript - 圖片能在網(wǎng)站顯示,但控制臺(tái)仍舊報(bào)錯(cuò)403 (Forbidden)8. 這是什么情況???9. mysql多表查詢10. mysql - AttributeError: ’module’ object has no attribute ’MatchType’
