maven中的<scope>

本文详细解释了在使用Maven和Eclipse开发Web应用时,如何正确配置Servlet-API和JSP-API依赖,避免编译和打包时的冲突,并提供了解决方案,确保Web应用在Tomcat下运行无误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   我们在用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  
" name="code" style="">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)  
" name="code" style="">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>标签,如下

 

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>  
" name="code" style=""><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也安装一下,问题应该就解决了。

<think>好的,用户想了解Maven依赖中的<type>pom</type>和<scope>import</scope>的用途。首先,我得回想一下Maven的依赖管理机制。记得在Maven中,当需要引入一个父POM或者管理多个依赖的版本时,通常会使用dependencyManagement部分。而这里的<type>pom</type>通常用于指定依赖类型,比如当依赖本身是一个POM文件而不是jar或war的时候。这时候,可能需要使用type为pom。 然后是<scope>import</scope>。这个scope通常用在dependencyManagement部分,用于从另一个POM文件中导入依赖管理配置。这样可以将其他项目的依赖版本统一管理,避免重复定义。比如Spring Boot的BOM(Bill of Materials)就会用到这种方式,通过import scope来引入预定义的依赖版本。 所以,当这两个元素一起使用时,通常是为了导入另一个POM文件中的dependencyManagement配置。例如,在父项目的POM中,通过导入Spring Boot的BOM,子模块就可以直接使用其中定义的依赖版本,而无需指定版本号。 需要解释一下为什么需要这样做。可能用户在使用多模块项目时,希望统一管理依赖版本,或者引用第三方提供的BOM。这时候,使用type为pom和scope为import的组合就能实现这一目的。需要注意的是,import scope只在dependencyManagement中有效,并且只能用于type为pom的情况。 可能还需要提供一个具体的代码示例,展示如何在pom.xml中配置这样的依赖。同时,需要指出注意事项,比如只能在dependencyManagement中使用,以及导入的POM必须包含dependencyManagement部分等。 最后,思考用户可能的后续问题。他们可能会问dependencyManagement的作用,BOM的概念,Maven的作用域类型,或者如何创建自己的BOM。这些都是与当前问题紧密相关的话题,可以作为相关问题提出。</think><< 在 Maven 依赖管理中,`<type>pom</type>` 和 `<scope>import</scope>` 的组合用于**导入其他 POM 文件中的依赖管理配置**。这是 Maven 的 BOM(Bill of Materials)模式实现的核心机制。 ### 核心作用: 1. **`<type>pom</type>`** 表示依赖类型是 POM 文件而非默认的 JAR。通常用于引用包含 `<dependencyManagement>` 的父级或聚合 POM。 2. **`<scope>import</scope>`** 仅在 `<dependencyManagement>` 中有效,表示将该 POM 的 `<dependencyManagement>` 内容合并到当前项目中。类似「复制粘贴」其依赖版本控制配置。 ### 示例代码: ```xml <dependencyManagement> <dependencies> <!-- 导入 Spring Boot 的 BOM 来统一版本 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>3.1.5</version> <type>pom</type> <!-- 必须声明类型为 POM --> <scope>import</scope> <!-- 仅在此处生效 --> </dependency> </dependencies> </dependencyManagement> ``` ### 关键特性: - **版本集中管理**:被导入的 POM 必须包含 `<dependencyManagement>`,其内部定义的依赖版本会覆盖当前项目的配置。 - **非传递性**:仅影响依赖版本,不会实际引入依赖的代码。 - **作用域限制**:`<scope>import` 仅在 `<dependencyManagement>` 中有效。 ### 常见问题: 若未正确使用 `<type>pom</type>`,Maven 会尝试下载不存在的 JAR 文件(如 `spring-boot-dependencies.jar`),导致构建失败。 ### 典型应用场景: - 统一管理 Spring Boot、gRPC 等框架的依赖版本 - 企业级多模块项目的全局依赖控制 >>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值