Maven - 与 Spark 和 HtmlUnit 的依赖冲突

Maven - dependency conflict with Spark and HtmlUnit

我想将 "spark" 网络框架(Java 中的 sinatra 克隆)与 HtmlUnit 结合使用。

我的想法是我的 WebService 将能够下载网站并解析它们(并执行 Java 脚本等),然后收集一些数据,进行一些统计等。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
]

排除在所有地方产生冲突的依赖关系,除了这样的地方:

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

我遇到了完全相同的问题,我能够通过将 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>