Maven 3 从测试范围Maven中排除servlet api
我在pom.xml中有以下依赖项,因此我的IDE(IntelliJ)在编译期间有servlet api类可用,但在构建中没有提供Maven 3 从测试范围Maven中排除servlet api,maven-3,Maven 3,我在pom.xml中有以下依赖项,因此我的IDE(IntelliJ)在编译期间有servlet api类可用,但在构建中没有提供 <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
如果删除此依赖项,Jetty服务器将在测试范围内正确启动,但IntelliJ需要此依赖项来编译代码。解决这个问题的最佳方法是什么,有没有一种方法可以排除测试范围的依赖关系?尝试将其设置为编译范围在运行junit测试的类路径中尝试不包含javax.servlet-api时,我找到了解决方案。实际上,我将ServletAPI移到了类路径中JAR的最末端,并得到了启示 我使用了错误版本的servlet api。我用的是2.5,但需要3.0。 Maven范围我选择“提供”。用于在eclipse中运行junit和执行“mvn测试” 不过,我不明白为何没有冲突。如果我做对了,即使是“提供的”依赖项在测试时也会暴露在类路径中,因此可能存在冲突——或者,当然——如果我准确地找到了用于编译的servlet api和jetty的servlet api的正确版本,那么就没有冲突 不管怎么说,它对我很有用 以下是jetty+servlet api的我的dependencies/*pom设置:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.4.v20120524</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.1.4.v20120524</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.4.v20120524</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.4.v20120524</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
org.eclipse.jetty
jetty服务器
8.1.4.v20120524
罐子
测试
org.eclipse.jetty
码头servlet
8.1.4.v20120524
罐子
测试
org.eclipse.jetty
jetty网络应用程序
8.1.4.v20120524
罐子
测试
org.eclipse.jetty
码头
8.1.4.v20120524
罐子
测试
javax.servlet
javax.servlet-api
3.0.1
假如
我自己也遇到了这个问题,想与大家分享一下:
- 依赖于
,提供了范围javax.servlet:servlet api:3.0-alpha-1
,因此它不会干扰最终部署到的容器,
- 依赖于
,范围为org.eclipse.jetty:jetty webapp
,因此我可以将jetty服务器作为单元测试的一部分运行test
- 随后,
jetty webapp需要对
的可传递依赖项org.eclipse.jetty.orbit:javax.servlet:3.0.0.v20111016
jetty.orbit:javax.servlet
对我来说是没有选择的,因为jettyServer
需要javax.servlet.HttpConstraintElement
,而javax.servlet:servlet api:3.0-alpha-1
没有提供。我最终做了这样的事:
javax.servlet:servlet api的依赖关系
jetty.orbit:javax.servlet
的依赖,并提供范围,从而完全取代javax.servlet:servlet api
HttpConstraintTelement
的交易是什么;也许它将在未来版本的javax.servlet:ServletAPI
中提供,sorta认为它比Jetty的实现更具依赖性
编辑:
顺便说一句,这个问题是我通过修改自动格式化POM文件的插件的配置而引起的。它对依赖项进行了重新排序,因此与另一个海报的解决方案相反,它对POM文件进行了重新排序。在我丰富的Maven经验中:如果你“依赖”于你的依赖顺序,那对我来说是一种主要的气味。我发现Servlet(2.5)的旧版本与Servlet 3.0一起存在于我的路径中。一旦我删除(排除)旧版本,我的问题就解决了。您也可以将其与grizzly和jetty依赖项混合使用。排除在我的情况下是不够的,但将jetty降级到7.6.14.v20131031对我来说很有效。我使用以下sbt项目设置来解决类似问题:
"any dependency program that includes servlet-api java library code" % excludeAll ExclusionRule(organization = "org.eclipse.jetty.servlet-api"),
"org.mortbay.jetty" % "servlet-api" % "3.0.20100224"
对于Gradle用户,Jetty运行基于Spring WebMVC的嵌入式webapp的设置具有以下依赖性:
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
dependencies {
// Logging support
compile 'org.slf4j:slf4j-api:1.7.7'
runtime 'org.slf4j:slf4j-simple:1.7.7'
// Spring WebMVC part
compile 'org.springframework:spring-web:4.0.6.RELEASE'
compile 'org.springframework:spring-webmvc:4.0.6.RELEASE'
compile 'org.springframework:spring-context:4.0.6.RELEASE'
compile 'org.springframework:spring-core:4.0.6.RELEASE'
compile 'org.springframework:spring-beans:4.0.6.RELEASE'
compile 'org.springframework:spring-expression:4.0.6.RELEASE'
// Jetty support
compile 'org.eclipse.jetty:jetty-server:8.1.4.v20120524'
compile 'org.eclipse.jetty:jetty-servlet:8.1.4.v20120524'
compile 'org.eclipse.jetty:jetty-webapp:8.1.4.v20120524'
compile 'org.eclipse.jetty:jetty-jsp:8.1.4.v20120524'
// Web Container interaction
//providedCompile 'javax.servlet:servlet-api:2.5'
runtime 'jstl:jstl:1.2'
// Unit Testing
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.springframework:spring-test:3.2.0.RELEASE'
}
我在这里迷路了-你能再详细一点吗?“编译范围”是什么意思?编译范围的意思是,只在java文件的编译过程中包含jar库文件。
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
dependencies {
// Logging support
compile 'org.slf4j:slf4j-api:1.7.7'
runtime 'org.slf4j:slf4j-simple:1.7.7'
// Spring WebMVC part
compile 'org.springframework:spring-web:4.0.6.RELEASE'
compile 'org.springframework:spring-webmvc:4.0.6.RELEASE'
compile 'org.springframework:spring-context:4.0.6.RELEASE'
compile 'org.springframework:spring-core:4.0.6.RELEASE'
compile 'org.springframework:spring-beans:4.0.6.RELEASE'
compile 'org.springframework:spring-expression:4.0.6.RELEASE'
// Jetty support
compile 'org.eclipse.jetty:jetty-server:8.1.4.v20120524'
compile 'org.eclipse.jetty:jetty-servlet:8.1.4.v20120524'
compile 'org.eclipse.jetty:jetty-webapp:8.1.4.v20120524'
compile 'org.eclipse.jetty:jetty-jsp:8.1.4.v20120524'
// Web Container interaction
//providedCompile 'javax.servlet:servlet-api:2.5'
runtime 'jstl:jstl:1.2'
// Unit Testing
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.springframework:spring-test:3.2.0.RELEASE'
}