`
nkliuliu
  • 浏览: 207446 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

maven中的<scope>

阅读更多

       我们在用maven和eclipse开发WEB应用的时候,需要把 servlet-api和jsp-api加入进来,要不然编译不会通过,加入进来之后在打包的时候maven自动把所有的依赖包都放到lib下面,如果你 在tomcat下面运行就会有问题,因为tomcat发现你的web应用的lib中包含了servlet-api,他会报错。

 

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
   
org.apache.jasper.JasperException: java.lang.NullPointerException
	org.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>标签,如下

 

<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 of import do not actually participate in limiting the transitivity of a dependency.

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

分享到:
评论

相关推荐

    springboot 基础简易实例, maven项目

    &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework....

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;jstl&lt;/artifactId&gt; &lt;version&gt;1.2&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency...

    使用Maven管理进行多模块开发案例

    &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt; &lt;artifactId&gt;jsp-api&lt;/artifactId&gt; &lt;version&gt;2.1&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;/...

    maven依赖关系中的&lt;scope&gt;provided&lt;/scope&gt;使用详解

    主要介绍了maven依赖关系中的&lt;scope&gt;provided&lt;/scope&gt;使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    打jar包注意点.docx

    1、先在pom.xml文件中加入&lt;build&gt;&lt;/build&gt;标签 &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt; &lt;version&gt;2.3.1&lt;/version&gt; ...

    logback-kafka-appender:适用于Apache Kafka的Logback附加程序

    logback-kafka-appender 该追加程序使您的应用程序可以将其应用程序日志直接发布到Apache Kafka。 登录不兼容警告 由于Logback Encoder API中的重大更改,您至少需要使用... &lt; scope&gt;runtime&lt;/ scope&gt; &lt;/ dependenc

    gwt-webdriver-junit-runstyle:用于通过 WebDriver 的 JSON over HTTP 协议进行远程测试的 GWT JUnit 运行样式

    ): &lt;dependency&gt;&lt;groupId&gt;com.github.neothemachine&lt;/groupId&gt;&lt;artifactId&gt;gwt-webdriver-junit-runstyle&lt;/artifactId&gt;&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;&lt;scope&gt;test&lt;/scope&gt;&lt;/dependency&gt;调整gwt-maven-plugin...

    Maven整合ssm

    &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt; &lt;version&gt;3.1.0&lt;/version&gt; &lt;/dependency&gt; &lt;!-- mybatis依赖的jar包 --...

    hadoop mapreduce wordcount

    &lt;scope&gt;system&lt;/scope&gt; &lt;systemPath&gt;${JAVA_HOME}/lib/tools.jar&lt;/systemPath&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;version&gt;3.8.1&lt;/version&gt; &lt;scope...

    人大金仓驱动包kingbasejdbc.jar V8.6.0、8.8.0驱动jar包

    &lt;scope&gt;system&lt;/scope&gt; &lt;systemPath&gt;${basedir}/src/main/resources/jar/kingbase8-8.6.0.jar&lt;/systemPath&gt; &lt;/dependency&gt; jdbc driver-class-name: com.kingbase8.Driver url: jdbc:kingbase8://127.0.0.1:54321/...

    t淘淘商城项目 商城项目 视频和源码教程 详细

    &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;!-- 文件上传组件 --&gt; &lt;dependency&gt; &lt;groupId&gt;commons-fileupload&lt;/groupId&gt; &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt; &lt;version&gt;${commons-fileupload....

    基于SpringBoot+Vue前后端分离的仓库管理系统后端源码+数据库+项目说明.zip

    &lt;scope&gt;runtime&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.projectlombok&lt;/groupId&gt; &lt;artifactId&gt;lombok&lt;/artifactId&gt; &lt;optional&gt;true&lt;/optional&gt; &lt;/dependency&gt; &lt;!--...

    ssm的jar包

    &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt; &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt; &lt;version&gt;2.3.1&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/...

    autoconfig-demo.zip

    &lt;scope&gt;test&lt;/scope&gt; &lt;exclusions&gt; &lt;exclusion&gt; &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt; &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt; &lt;/exclusion&gt; &lt;/exclusions&gt; &lt;/dependency&gt; &lt;/dependencies...

    SSM框架整合

    &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;javax.servlet&lt;/groupId&gt; &lt;artifactId&gt;jsp-api&lt;/artifactId&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;!-- 如果不添加此...

    Maven之scope依赖范围.docx

    Maven在编译项目主代码的时候需要使用一套classspath。总共有三种classpath,分别对应于Maven编译项目主代码的时候、Maven编译和执行测试的时候、实际运行Maven项目的时候。

    ojdbc6(此包解决mybatis_plus查询时分秒为0问题).7z

    ojdbc6(此包解决mybatis_plus查询时分秒为0问题),springboot项目+oracle11g就用这个jdbc就行,... &lt;scope&gt;system&lt;/scope&gt; &lt;systemPath&gt;${pom.basedir}/src/main/resources/lib/ojdbc6.jar&lt;/systemPath&gt; &lt;/dependency&gt;

    基于springboot演示resuful api、mock请求、validate验证、异常捕捉、aop切面编程+源代码+文档说

    &lt;scope&gt;import&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.commons&lt;/groupId&gt; &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt; &lt;version&gt;${commons-lang3.version}&lt;/version&gt; &lt;/dependency&gt; ...

    springside-core-4.2.2.GA(含关联的test.jar)

    &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; 手动安装至本地maven仓库 mvn install:install-file -DgroupId=org.springside -DartifactId=springside-core -Dversion=4.2.2.GA -Dfile=./springside-core-4.2.2.GA.jar -...

    KKfile文件上传依赖aspose-cad-23.7.jar

    注释掉原有的aspose依赖,将下载文件挡在lib文件夹下,并在pom文件中添加新的依赖。... &lt;scope&gt;system&lt;/scope&gt; &lt;systemPath&gt;${project.basedir}/lib/aspose-cad-23.7.jar&lt;/systemPath&gt; &lt;/dependency&gt;

Global site tag (gtag.js) - Google Analytics