java - Mavn執(zhí)行測試時(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為測試
執(zhí)行mvn compile 或者mvn package也會(huì)報(bào)錯(cuò)
當(dāng)把pom.xml中junit依賴的scope去掉時(shí),編譯和測試都能成功。
<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確定了測試時(shí)會(huì)引入junit
問題解答
回答1:這個(gè)問題其實(shí)你因?yàn)槟悴皇煜aven文件結(jié)構(gòu)所致.測試類一般是放在src/test/java,而不是放在src/main/java下.maven在編譯的時(shí)候,src/main/java下是不引用<scope>test</scope>的jar,而編譯src/test/java下的測試這會(huì)引用<scope>test</scope>的jar
相關(guān)文章:
1. javascript - node.js promise沒用2. android 如何實(shí)現(xiàn)如圖中的鍵盤上的公式及edittext的內(nèi)容展示呢3. c++ - 如何正確的使用QWebEngineView?4. golang - 用IDE看docker源碼時(shí)的小問題5. javascript - js 寫一個(gè)正則 提取文本中的數(shù)據(jù)6. 算法 - python 給定一個(gè)正整數(shù)a和一個(gè)包含任意個(gè)正整數(shù)的 列表 b,求所有<=a 的加法組合7. yii2中restful配置好后在nginx下報(bào)404錯(cuò)誤8. java - 我在用Struts2上傳文件時(shí),報(bào)以下錯(cuò)誤怎么回事?9. PHP注冊(cè)功能10. php - 注冊(cè)驗(yàn)證郵箱失效后操作問題
