簡體   English   中英

Maven - 與Spark和HtmlUnit的依賴沖突

[英]Maven - dependency conflict with Spark and HtmlUnit

我想結合HtmlUnit使用“spark”Web框架(Java中的sinatra克隆)。

我的想法是,我的WebService將能夠下載網站並解析它們(並執行JavaScripts等),然后收集一些數據,做一些統計等.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
]

排除在所有地方創建沖突的依賴項,除了這樣的:

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

可以使用第二件事情是<dependencyManagement>在POM的根(相同的水平<dependencies>你迄今使用)。 在dependencyManagement中,您可以指定將使用哪個庫的版本,如下所示:

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

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

我有完全相同的問題,我能夠通過將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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM