我们在用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 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也安装一下,问题应该就解决了。
分享到:
相关推荐
<scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework....
本文将深入探讨"Maven中scope test的使用以及依赖继承传递"这一主题,帮助开发者更好地理解和应用Maven的核心特性。 首先,`scope test`是Maven依赖管理中的一个关键概念。当我们在`pom.xml`文件中为某个依赖设置`...
Maven允许在POM中配置插件,通过`<build><plugins>`和`<profiles>`标签可以定制化构建过程,比如设置源代码编码、资源过滤等。 总结,Maven的多项目打包模块通过POM文件实现了项目的分层管理,简化了大型项目中的...
`<scope>`标签在Maven的依赖管理中扮演着决定依赖范围的角色,它定义了特定的依赖在项目的哪些阶段可用,并决定了依赖是否会被包含在最终的部署包中。本文将详细解析`<scope>provided</scope>`的使用和意义。 `...
--> <name>My Project</name> <description>This is my Maven project</description> <organization> <name>My Company</name> <url>http://www.mycompany.com</url> </organization> <licenses> <license> <name>...
你可以在`pom.xml`文件中直接添加对应的`<dependency>`标签,指定`groupId`, `artifactId`, `version`,并将`scope`设置为`compile`。这种方式适用于所有开发环境,但需要确保JAR包在项目结构中的正确位置,例如在`...
<scope>provided</scope> </dependency> ``` 这里,`scope`设置为`provided`表示Tomcat已经在服务器环境中提供,无需在最终部署的WAR包中包含。 总结来说,Maven和Tomcat插件的结合使用,能够帮助开发者高效地...
<scope>system</scope> <systemPath>${basedir}/lib/cpdetector_1.0.10.jar</systemPath> </dependency> ``` 配置Maven插件 在上述配置以后,我们已经可以在编写代码时引入Jar包中的类了。但是,在打包时,由于...
<scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> </build> ``` 配置中的`<option>`标签用于指定需要保留的类或方法,`<obfuscate>true</obfuscate>`开启混淆,`<injar>`和`...
<scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> ... </build> ... </project> ``` 在这个配置中,我们指定了`proguard-maven-plugin`的版本,并在`execution`元素中定义了...
<scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> </...
`<scope>system</scope>`表示这是一个系统级别的依赖,不会被Maven上传到远程仓库。 - **方法二:使用`maven-install-plugin`** 另一种方法是通过Maven的`maven-install-plugin`将本地jar包安装到本地Maven仓库。...
注释掉原有的aspose依赖,将下载文件挡在lib文件夹下,并在pom文件中添加新的依赖。... <scope>system</scope> <systemPath>${project.basedir}/lib/aspose-cad-23.7.jar</systemPath> </dependency>
- `<scope>compile</scope>`:定义依赖的作用范围,如编译、运行、测试等。 - `</dependency>` 5. **插件管理(Plugins)**:配置Maven插件,用于执行特定的构建任务。例如: - `<build>` - `<plugins>` - `...
<scope>test</scope> </dependency> </dependencies> <build> <finalName>HelloWorld</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</...
使用`<scope>system</scope>`和`<systemPath>`的方式,可以有效地将本地的JAR包纳入到Maven项目的依赖管理中。 #### 2. 安装本地JAR至本地仓库 另一种更为推荐的做法是将本地的JAR包安装到Maven的本地仓库中,通过...
<scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </...
- **项目坐标**:在 Maven 项目的核心配置文件 `pom.xml` 中,通过 `<groupId>`, `<artifactId>`, `<version>` 等元素来定义项目的坐标。 - **依赖声明**:使用 `<dependencies>` 元素来声明项目依赖。 ```xml ...
<scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.44.v20210927</version> </dependency> ``` 然后,我们需要...
<scope>test</scope> </dependency> <!-- 添加更多依赖 --> </dependencies> </dependencyManagement> <!-- 插件管理 --> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven....