2016-02-02 115 views
3

我想將「spark」web框架(Java中的sinatra克隆)與HtmlUnit結合使用。Maven - 與Spark和HtmlUnit的依賴衝突

這個想法是,我的WebService將能夠下載網站和解析它們(並執行JavaScript等),然後收集一些數據,做一些統計等。HtmlUnit不僅僅用於測試,但實際上需要在主要項目。

無論如何,Spark在Jetty上運行,Spark和HtmlUnit似乎都使用相同的websocket客戶端庫(org.eclipse.jetty.websocket:websocket-client:9.3.2.v20150730),但版本不同。還有一些其他圖書館似乎也存在問題。

項目編譯正常,但無法啓動Web服務器。

有沒有辦法以某種方式解決這些衝突?

這裏是我的依賴關係:

<dependencies> 
    <dependency> 
    <groupId>com.sparkjava</groupId> 
    <artifactId>spark-core</artifactId> 
    <version>2.3</version> 
    </dependency> 

    <dependency> 
    <groupId>com.sparkjava</groupId> 
    <artifactId>spark-template-freemarker</artifactId> 
    <version>2.0.0</version> 
    </dependency> 
    <dependency> 
    <groupId>net.sourceforge.htmlunit</groupId> 
    <artifactId>htmlunit</artifactId> 
    <version>2.19</version> 
    </dependency> 
</dependencies> 

我還發現了強制插件,它列出了所有衝突。下面是輸出:

Failed while enforcing releasability the error(s) are [ 
Dependency convergence error for org.slf4j:slf4j-api:1.7.12 paths to dependency are: 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-com.sparkjava:spark-core:2.3 
    +-org.slf4j:slf4j-api:1.7.12 
and 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-com.sparkjava:spark-core:2.3 
    +-org.slf4j:slf4j-simple:1.7.12 
     +-org.slf4j:slf4j-api:1.7.12 
and 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-com.sparkjava:spark-template-freemarker:2.0.0 
    +-org.slf4j:slf4j-api:1.7.2 
, 
Dependency convergence error for commons-codec:commons-codec:1.9 paths to dependency are: 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-net.sourceforge.htmlunit:htmlunit:2.19 
    +-org.apache.httpcomponents:httpclient:4.5.1 
     +-commons-codec:commons-codec:1.9 
and 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-net.sourceforge.htmlunit:htmlunit:2.19 
    +-commons-codec:commons-codec:1.10 
, 
Dependency convergence error for xml-apis:xml-apis:1.3.04 paths to dependency are: 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-net.sourceforge.htmlunit:htmlunit:2.19 
    +-xalan:xalan:2.7.2 
     +-xalan:serializer:2.7.2 
     +-xml-apis:xml-apis:1.3.04 
and 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-net.sourceforge.htmlunit:htmlunit:2.19 
    +-xerces:xercesImpl:2.11.0 
     +-xml-apis:xml-apis:1.4.01 
, 
Dependency convergence error for org.eclipse.jetty.websocket:websocket-client:9.3.2.v20150730 paths to dependency are: 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-com.sparkjava:spark-core:2.3 
    +-org.eclipse.jetty.websocket:websocket-server:9.3.2.v20150730 
     +-org.eclipse.jetty.websocket:websocket-client:9.3.2.v20150730 
and 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-net.sourceforge.htmlunit:htmlunit:2.19 
    +-org.eclipse.jetty.websocket:websocket-client:9.2.13.v20150730 
, 
Dependency convergence error for com.sparkjava:spark-core:2.3 paths to dependency are: 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-com.sparkjava:spark-core:2.3 
and 
+-com.example:helloworld:1.0-SNAPSHOT 
    +-com.sparkjava:spark-template-freemarker:2.0.0 
    +-com.sparkjava:spark-core:2.0.0 
] 

回答

3

排除創建在所有地方發生衝突,除了一個像這樣的依存關係:

<dependency> 
    <groupId>com.sparkjava</groupId> 
    <artifactId>spark-core</artifactId> 
    <version>2.3</version> 
    <exclusions> 
     <exclusion> 
      <artifactId>slf4j-simple</artifactId> 
      <groupId>org.slf4j</groupId> 
     </exclusion> 
    </exclusions> 
</dependency> 

添加其他排除地方有必要

編輯:我創建的測試應用程序與您列出的依賴關係,我認爲這可能工作

<dependencies> 
    <dependency> 
     <groupId>com.sparkjava</groupId> 
     <artifactId>spark-core</artifactId> 
     <version>2.3</version> 
     <exclusions> 
      <exclusion> 
       <artifactId>slf4j-simple</artifactId> 
       <groupId>org.slf4j</groupId> 
      </exclusion> 
      <exclusion> 
       <artifactId>websocket-server</artifactId> 
       <groupId>org.eclipse.jetty.websocket</groupId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <dependency> 
     <groupId>com.sparkjava</groupId> 
     <artifactId>spark-template-freemarker</artifactId> 
     <version>2.0.0</version> 
     <exclusions> 
      <exclusion> 
       <artifactId>slf4j-api</artifactId> 
       <groupId>org.slf4j</groupId> 
      </exclusion> 
      <exclusion> 
       <artifactId>spark-core</artifactId> 
       <groupId>com.sparkjava</groupId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

    <dependency> 
     <groupId>net.sourceforge.htmlunit</groupId> 
     <artifactId>htmlunit</artifactId> 
     <version>2.19</version> 
     <exclusions> 
      <exclusion> 
       <artifactId>httpclient</artifactId> 
       <groupId>org.apache.httpcomponents</groupId> 
      </exclusion> 
      <exclusion> 
       <artifactId>xalan</artifactId> 
       <groupId>xalan</groupId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
</dependencies> 

您可以使用的第二件事是POM根目錄中的<dependencyManagement>(與迄今爲止使用的<dependencies>的水平相同)。在dependencyManagement你指定哪個版本,其中庫將被使用,如:

<dependencyManagement> 
    <dependencies> 
    <dependency> 
     <groupId>foo</groupId> 
     <artifactId>bar</artifactId> 
     <version>1.2.3</version> 
    </dependency> 
    </dependencies> 
</dependencyManagement> 

這樣,無論哪個庫foo:bar的版本包含在任何依賴關係,1.2.3版本將始終使用。

+1

好的,我會試試這個。 – Peanut

+0

它看起來像火花核心和HtmlUnit似乎使用相同的類文件,似乎是衝突(Websocket似乎是罪魁禍首)。火花核心版本和使用相同版本的HtmlUnit版本沒有組合 – Peanut

+0

您是否試過從HtmlUnit中排除org.eclipse.jetty.websocket:websocket-client? –

0

我有相同的問題,我能夠通過所有兩種的HtmlUnit和sparkjava共同依賴設置爲相同的版本,以解決這個問題如下:

請注意,我用的兩個最新版本。

<!-- Spark framework to create restful endpoints --> 
     <dependency> 
      <groupId>com.sparkjava</groupId> 
      <artifactId>spark-core</artifactId> 
      <version>2.5.4</version> 
     </dependency> 

    <!-- HTMLUnit for crawling purposes --> 
     <dependency> 
      <groupId>net.sourceforge.htmlunit</groupId> 
      <artifactId>htmlunit</artifactId> 
      <version>2.23</version> 
     </dependency> 
    </dependencies> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.eclipse.jetty</groupId> 
       <artifactId>jetty-webapp</artifactId> 
       <version>9.3.6.v20151106</version> 
      </dependency> 
      <dependency> 
       <groupId>org.eclipse.jetty</groupId> 
       <artifactId>jetty-util</artifactId> 
       <version>9.3.6.v20151106</version> 
      </dependency> 
      <dependency> 
       <groupId>org.eclipse.jetty.websocket</groupId> 
       <artifactId>websocket-client</artifactId> 
       <version>9.3.6.v20151106</version> 
      </dependency> 
     </dependencies> 
    </dependencyManagement>