maven常見錯誤


摘要: 1.Java-maven異常-cannot be cast to javax.servlet.Filter 報錯  tomcat 啟動后先將tomcat/lib目錄下的jar包全部讀入內存,如果webapps目錄里的應用程序中WEB-INF/lib目錄下有相同的包,將無法加載,報錯的Filter...

1.Java-maven異常-cannot be cast to javax.servlet.Filter 報錯

 tomcat 啟動后先將tomcat/lib目錄下的jar包全部讀入內存,如果webapps目錄里的應用程序中WEB-INF/lib目錄下有相同的包,將無法加載,報錯的Filter實現了javax.servlet.Filter接口,Filter是在servlet-api.jar里。

解決這個問題的方法就是對於servlet-ap.jar 使用 <scope>標簽,編譯的時候用到servlet-api和jsp-api,但在打包的時候不用這兩個依賴。

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>中<scope>,它主要管理依賴的部署。目前<scope>可以使用5個值:

* compile,缺省值,適用於所有階段,會隨着項目一起發布。 
* provided,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。 
* runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。 
* test,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發布。 
*system,類似provided,需要顯式提供包含依賴的jar,Maven不會在Repository中查找它。

2.Maven缺少依賴包,強制更新

mvn clean install -e -U

-e詳細異常,-U強制更新

3.java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0

解決辦法比較多,在項目右鍵查看Configure中的Java BuildPath,設置為和默認JDK一致就可以。

可以修改settings.xml,添加如下內容:

1
2
3
4
5
6
7
8
9
10
11
12
< profile >
< id >jdk-1.7</ id >
< activation >
< activeByDefault >true</ activeByDefault >
< jdk >1.7</ jdk >
</ activation >
< properties >
< maven.compiler.source >1.7</ maven.compiler.source >
< maven.compiler.target >1.7</ maven.compiler.target >
< maven.compiler.compilerVersion >1.7</ maven.compiler.compilerVersion >
</ properties >
</ profile >

  

4.編譯失敗 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project web_nanchang: There are test failures.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project web_nanchang: There are test failures.

[ERROR] Please refer to E:\maven\web_nanchang\target\surefire-reports for the individual test results.

解決方法:

這是因為測試代碼時遇到錯誤,它會停止編譯。只需要在pom.xml的<project>里添加以下配置,使得測試出錯不影響項目的編譯。

1
2
3
4
5
6
7
8
9
10
< build >
< plugins >
< plugin >
< groupId >org.apache.maven.plugins</ groupId >
< artifactId >maven-surefire-plugin</ artifactId >
< configuration >
< testFailureIgnore >true</ testFailureIgnore > </ configuration >
</ plugin >
</ plugins >
</ build >

命令行:mvn test -Dmaven.test.failure.ignore=true

5.在Eclipse中進行Maven項目斷點調試等

1、在安裝了m2eclipse插件並配置好maven-tomcat-plugin后,單擊Eclipse菜單“Run”中的“Run Configurations”。
2、在彈出的對話框中的左側樹中找“到Maven Build”,在其上點擊右鍵並單擊“New”。
3、在右側的“Name”一欄填入自定義的名稱。單擊在“Main”選項卡的“Browse Workspace”按鈕,選擇目標項目,選擇后在“Base directory”一欄中會出現形如“${workspace_loc:/project_a}”的內容(project_a是前文所述應用項目A,它會根據你所選的目標項目而改變)。
4、在“Goals”一欄中填入“tomcat:run”。
5、在“Maven Runtime”選擇你需要的Maven環境(注意:必須是2.0.8以上的版本)。
6、單擊“Apply”,配置完成。
通過以上配置,在eclipse自身的Run和Debug按鈕下都會找到以你先前配置中“Name”值為名的條目(條目最前端也會有“m2”標識),單擊之后即可啟動基於Tomcat之上的相應項目的運行操作或調試操作。

6.Maven編譯 No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

maven編譯項目時出錯,提示信息如下:

1
2
3
4
5
6
7
8
9
10
11
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 3.1 :testCompile ( default -testCompile) on project springside-core: Compilation failure
 
[ERROR] No compiler is provided in  this  environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help  1 ]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e  switch .
[ERROR] Re-run Maven using the -X  switch  to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
 
[ERROR] [Help  1 ] http: //cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

解決方案:

在eclipse的菜單中,進入 Window > Preferences > Java > Installed JREs > Execution Environments,選擇JavaSE-1.6, 在右側選擇jdk.

然后在maven菜單中使用 “update project ...”.

 

7.-Dmaven.multiModuleProjectDirectory system propery is not set

eclipse中使用maven插件的時候,運行run as maven build的時候報錯:

-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

直接的解決方法:使用低版本的maven。

或者可以執行下面的操作:

設置環境變量M2_HOME指向你的maven安裝目錄

M2_HOME=D:\Apps\apache-maven-3.3.1

然后在Window->Preference->Java->Installed JREs->Edit

在Default VM arguments中設置

-Dmaven.multiModuleProjectDirectory=$M2_HOME


 

8.pom文件提示"Missing artifact..."

更新maven工程之后,發現pom.xml文件在如下依賴處報錯:
<dependency>
<groupId>cms-dubbo</groupId>
<artifactId>cms-dubbo</artifactId>
<version>0.0.1</version>
</dependency>
報錯信息為:Missing artifact cms-dubbo:cms-dubbo:jar:0.0.1

解決方法:

1.確保遠程倉庫是有對應jar包的,有可能有jar包,但是和你的版本號不對應,這種情況直接更新版本號就可以。
2.確認本地repository相應目錄中是否下載好了對應的包。
同時檢查發現目錄下是否存在以如下結尾的文件:
-not-available
.lastUpdated
該類型文件可以影響更新。將這兩個文件刪掉,重新build,如果問題解決,應該是之前未下載成功產生了這兩個文件,影響了maven正常更新。

3.如果上述辦法無效,到eclipse-help-install new software-available software sites下,找之前安裝m2eclipse插件的地址,如果是http://download.eclipse.org/technology/m2e/releases,將其卸載,按如下地址重新安裝m2eclipse插件:http://m2eclipse.sonatype.org/sites/m2e

4.如果上述方法仍無效,可嘗試在eclipse中先用Close Project關掉出問題的工程,然后再Open Project打開;或用Project-Clean重新build該工程。

 

9.Maven+eclipse工程中Maven Dependencies 文件夾丟失問題

在項目構建路徑中更改。

10.Failure to transfer 依賴文件

Failure to transfer org.codehaus.plexus:plexus-archiver:jar:1.2 from http://172.17.xx.xx:xxxx/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus-news has elapsed or updates are forced. Original error: Could not transfer artifact org.codehaus.plexus:plexus-archiver:jar:1.2 from/to nexus-news (http://172.17.xx.xx:xxxx/nexus/content/groups/public/): No response received after 60000

錯誤原因已經說明:org.codehaus.plexus從nexus倉庫傳輸失敗,本地倉庫已經存在對應的緩存文件,Maven不會重新下載文件,直到更新索引或者強制更新項目。

解決辦法也很簡單,直接maven-->updates projects就可以。

 

11.不能使用eclipse直接創建Maven Web工程

使用Eclipse時,不能一次完成Maven Web項目的創建,需要分步驟才能創建一個完整的Maven Web項目。

我的建議是通過復制粘貼等手工建立Maven的文件結構和pom文件,然后再在Eclipse中import已經存在的Maven項目。

也可以通過下面的步驟,

在Eclipse下新建項目時,選擇新建Maven項目,默認點擊下一步,在“select an archetype”這步時,

選擇org.apache.maven.archetypes maven-archetype-webapp

maven archetype就是創建項目的腳手架,你可以通過命令行或者IDE集成簡化項目創建的工作。

下一步輸入項目名稱,起名為“testMavenWeb”項目,點擊【finish】,完成項目創建。
此時,一個Java Maven項目創建成功,但並不是Maven Web項目。

Java Maven 創建成功后,再創建一個Web項目,在“select an archetype”這步時,選擇“maven-archetype-webapp”,成功創建一個web項目,
然后將該web項目下的webapp文件夾,拷貝到testMavenWeb項目下,至此,在Eclipse下的Maven Web項目創建成功。

 

12.Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin...There are test failures.

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project myproject: There are test failures. 
Please refer to E:\myproject\target\surefire-reports for the individual test results.

解決方法:
測試代碼時遇到錯誤停止編譯。可以在pom.xml的忽略錯誤,使得測試出錯不影響項目的編譯。

1
2
3
4
5
6
7
8
9
10
< build >
     < plugins >
         < plugin >
             < groupId >org.apache.maven.plugins</ groupId >
             < artifactId >maven-surefire-plugin</ artifactId >
             < configuration >
                 < testFailureIgnore >true</ testFailureIgnore >            </ configuration >
         </ plugin >
     </ plugins >
</ build >

  

13.maven手動把安裝jar到本地倉庫

每次添加單個jar到maven本地倉庫的操作如下:

1.建立一個新的文件夾,將jar文件存放在該文件夾下。 注意文件夾下最好只存放該文件。

2.在該文件夾下建立一個pom.xml文件,在pom文件中定義其maven坐標。

3.在cmd窗口中執行以下命令: mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> 
例如:
mvn install:install-file -Dfile=D:/jar/xxx.jar -DgroupId=xxx.xxx -DartifactId=xxx -Dversion=x.x -Dpackaging=jar

14.xxx was cached in the local repository,resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced

把maven倉庫中xxx.lastUpdated文件全部刪掉,重新運行maven,
或者在執行maven操作時加 -U參數,就可以忽略xxx.lastUpdated。

15.[ERROR] 找不到javax.servlet.ServletContext的類文件

Maven項目下HttpServletRequest 或 HttpServletResponse需引用的依賴包:servlet-api.jar,並將scope設置為provided。

1
2
3
4
5
6
7
<!-- 使用HttpServletRequest或 HttpServletResponse需引用的依賴包 -->
< dependency >
< groupId >javax.servlet</ groupId >
< artifactId >servlet-api</ artifactId >
< version >2.5</ version >
< scope >provided</ scope >
</ dependency >


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM