2012-06-18 22 views
14

我在我的pom.xml中有以下依賴項,以便我的IDE(IntelliJ)在編譯期間具有可用的servlet-api類,但未在構建中提供。從測試範圍中排除servlet-api Maven

<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>servlet-api</artifactId> 
    <version>3.0-alpha-1</version> 
    <scope>provided</scope> 
</dependency> 

但在測試範圍內運行時所提供的範圍增加了班,這種依賴到classpath,這對碼頭,我開始編程的問題。因爲它已經在其圖書館中,我得到一個

java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package 

如果我刪除此依賴Jetty服務器在測試範圍內可以正常啓動,但我需要這種依賴的IntelliJ編譯我的代碼。什麼是解決這個問題的最好方法,是否有一種方法可以排除測試範圍的這種依賴關係?

回答

5

嘗試設置它來編譯範圍

+0

謝謝,解決了它! –

+0

我迷路了 - 你能再詳細一點嗎?什麼「編譯範圍」是指? – xhudik

+0

編譯範圍是指在java文件的編譯過程中只包含jar庫文件。 – Raman

3

我找到了解決辦法儘量不包括在運行JUnit測試類路徑的javax.servlet-API時。其實我把servlet-api放在類路徑中的罐子的最後,並且啓發來了...

我使用了錯誤版本的servlet-api。我使用2.5但需要3.0。 Maven範圍我選擇「提供」。適用於在eclipse內運行junit以及執行「mvn test」。

不過,我不明白爲什麼沒有衝突。如果我知道了,即使在測試時,「提供的」依賴關係也會暴露在類路徑中,所以可能會有衝突 - 或者當然 - 如果我確切地點擊了用於編譯的servlet-api的正確版本以及碼頭的servlet-api則沒有衝突。

無論如何,它適用於我。

這裏是我的依賴性/ *爲碼頭+ Servlet API的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> 
7

我只是有這個問題我自己,並希望分享:

  • 依賴於javax.servlet:servlet-api:3.0-alpha-1,具有範圍provided ,這樣它就不會干涉我的WAR最終部署到的容器
  • 依賴於org.eclipse.jetty:jetty-webapp,範圍爲test,這樣我就可以運行Jetty Serv呃我的單位的部分測試
  • 隨後在org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016傳遞依賴,通過jetty-webapp

排除的jetty.orbit:javax.servlet需要的是沒有選項(對我來說),因爲碼頭Server需要的不是由javax.servlet:servlet-api:3.0-alpha-1提供的javax.servlet.HttpConstraintElement。最後我做這個:

  1. 卸下​​
  2. 的依賴顯式添加上jetty.orbit:javax.servlet的依賴,範圍provided,從而完全取代​​

我不知道這筆交易是什麼與它需要的HttpConstraintElement;也許它會在未來版本的​​中可用,這種感覺比Jetty的實現更適合依賴。

編輯:

順便說一句,這個問題得到了由我介紹了與自動格式化POM文件的插件的配置擺弄。它重新排序依賴關係,因此可以解決另一張海報的重新排序POM文件的問題。在我豐富的Maven經驗中:如果你「依賴」你的依賴順序,這是主要味道

3

對我來說同樣的錯誤來了。我發現舊版本的Servlet(2.5)與servlet 3.0一起存在於我的路徑中。一旦我刪除(排除)舊版本我的問題解決。

0

你也可以把它與灰熊和碼頭的依賴關係混合。

0

在我的情況排除是不夠的,但降級碼頭到7.6.14.v20131031爲我工作。

1

我用下面的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" 
-1

對於搖籃用戶,碼頭的設置運行基於Spring WebMVC嵌入式Web應用程序適用於以下依存關係:

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' 
}