maven中的scope配置

maven中的scope配置

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

解决这个问题的方法就是使用<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也安装一下,问题应该就解决了。

你可能感兴趣的:(maven中的scope配置)