maven中的<scope>

来源:互联网 发布:政府网络维护员工资 编辑:程序博客网 时间:2024/06/05 02:51
   我们在用maven和eclipse开发WEB应用的时候,需要把 servlet-api和jsp-api加入进来,要不然编译不会通过,加入进来之后在打包的时候maven自动把所有的依赖包都放到lib下面,如果你在tomcat下面运行就会有问题,因为tomcat发现你的web应用的lib中包含了servlet-api,他会报错。

 

Java代码 复制代码 收藏代码
  1. validateJarFile(D:\springsource\apache-tomcat-6.0.24\wtpwebapps\semwinner\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class  
validateJarFile(D:\springsource\apache-tomcat-6.0.24\wtpwebapps\semwinner\WEB-INF\lib\servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
   
Java代码 复制代码 收藏代码
  1. org.apache.jasper.JasperException: java.lang.NullPointerException   
  2.     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:527)   
  3.     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:359)   
  4.     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)   
  5.     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)   
  6.     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)   
  7.     org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)   
  8.     org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:163)   
  9.     org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)   
  10.     org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)  
org.apache.jasper.JasperException: java.lang.NullPointerExceptionorg.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:527)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:359)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:163)org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
 

解决这个问题的方法就是使用<scope>标签,如下

 

Java代码 复制代码 收藏代码
  1. <dependency>   
  2.     <groupId>javax.servlet</groupId>   
  3.     <artifactId>servlet-api</artifactId>   
  4.     <version>2.4</version>   
  5.     <scope>provided</scope>   
  6.    </dependency>   
  7.    <dependency>   
  8.     <groupId>javax.servlet</groupId>   
  9.     <artifactId>jsp-api</artifactId>   
  10.     <version>2.0</version>   
  11.     <scope>provided</scope>   
  12.    </dependency>  
<dependency>    <groupId>javax.servlet</groupId>    <artifactId>servlet-api</artifactId>    <version>2.4</version>    <scope>provided</scope>   </dependency>   <dependency>    <groupId>javax.servlet</groupId>    <artifactId>jsp-api</artifactId>    <version>2.0</version>    <scope>provided</scope>   </dependency>
 

 

这里的意思是说,编译的时候用到servlet-api和jsp-api,但在打包的时候不用这两个依赖。

在maven的官方中是这样描述的

Dependency Scope

Dependency scope is used to limit the transitivity of a depedency, and also to affect the classpath used for various build tasks.

There are 6 scopes available:

  • compile
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided
    This is much like compile , but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime
    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test
    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
  • system
    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  • import (only available in Maven 2.0.9 or later)
    This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's<dependencyManagement> section. Since they are replaced, dependencies with a scope ofimport do not actually participate in limiting the transitivity of a dependency.

      如果你这样做了,但使用eclipse+tomcat做测试的时候发现servlet-api还是被打包到lib下面了,你要把maven插件中的WTP也安装一下,问题应该就解决了。

原创粉丝点击