maven dependency scop 解決maven和tomcat包衝突
阿新 • • 發佈:2019-02-16
今天遇到點問題,
maven引用的包 servlet-api.jar jsp-api.jar,我在除錯的時候釋出到了專案的lib目錄,但是tomcat中包含著兩個包,所以出現了包衝突。
HTTP Status 500 - Unable to compile class for JSP: type Exception report message Unable to compile class for JSP: description The server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: [32] in the generated java file: [D:\tomcatv752\work\Catalina\localhost\springmvc\org\apache\jsp\WEB_002dINF\jsp\index_jsp.java] The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468) org.apache.jasper.compiler.Compiler.compile(Compiler.java:378) org.apache.jasper.compiler.Compiler.compile(Compiler.java:353) org.apache.jasper.compiler.Compiler.compile(Compiler.java:340) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:6
解決辦法是釋出,除錯的時候servlet-api.jar jsp-api.jar這兩個包不釋出到專案目錄,使用到了
dependency 下面的scope標籤元素
具體配置:
<scope>provided</scope>解決了大問題:<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency>
以下是關於scope的詳解。
scope的使用場景和說明
1.compile
編譯範圍,預設scope,在工程環境的classpath(編譯環境)和打包(如果是WAR包,會包含在WAR包中)時候都有效。
2.provided
容器或JDK已提供範圍,表示該依賴包已經由目標容器(如tomcat)和JDK提供,只在編譯的classpath中載入和使用,打包的時候不會包含在目標包中。最常見的是j2ee規範相關的servlet-api和jsp-api等jar包,一般由servlet容器提供,無需在打包到war包中,如果不配置為provided,把這些包打包到工程war包中,在tomcat6以上版本會出現衝突無法正常執行程式(版本不符的情況)。
3.runtime
一般是執行和測試環境使用,編譯時候不用加入classpath,打包時候會打包到目標包中。一般是通過動態載入或介面反射載入的情況比較多。也就是說程式只使用了介面,具體的時候可能有多個,執行時通過配置檔案或jar包掃描動態載入的情況。典型的包括:JDBC驅動等。
4.test
測試範圍,一般是單元測試場景使用,在編譯環境加入classpath,但打包時不會加入,如junit等。
5.system
系統範圍,與provided類似,只是標記為該scope的依賴包需要明確指定基於檔案系統的jar包路徑。因為需要通過systemPath指定本地jar檔案路徑,所以該scope是不推薦的。如果是基於組織的,一般會建立本地映象,會把本地的或組織的基礎元件加入本地映象管理,避過使用該scope的情況。
6.import
(僅在 Maven 2.0.9 或者更新版本中有效)