Maven - Spark和HtmlUnit的依赖冲突

3
我想在 HtmlUnit 中使用 "Spark" web 框架(Java 中的 Sinatra 克隆版)。
这个想法是,我的 Web 服务将能够下载网站并解析它们(和执行 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>

我也找到了enforce插件,它会列出所有的冲突。以下是输出结果:
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
]
2个回答

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。


看起来spark-core和HtmlUnit似乎使用相同的类文件,这些文件似乎存在冲突(websockets似乎是罪魁祸首)。没有任何一种spark-core版本和HtmlUnit版本的组合使用相同的版本。 - Peanut
你尝试过从HtmlUnit中排除org.eclipse.jetty.websocket:websocket-client吗? - Misa Lazovic
是的。我尝试过了。似乎存在类冲突...虽然我不需要WebSockets...我尝试将它们移除,或者使用另一个版本的HtmlUnit。还有其他解决方法吗? - Peanut
我创建了一个包含Java代码的Gist:https://gist.github.com/ammoniak/1bfe3ddb26f3eb3b893c - Peanut
1
在我切换到旧版的HtmlUnit并使用您的排除项后,它可以工作了。WebSockets不起作用(但对我来说没关系)。 - Peanut

1

我曾经遇到过完全相同的问题,但是通过设置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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接