Maven - 与Spark和HtmlUnit

时间:2016-02-02 15:12:49

标签: java maven 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
]

2 个答案:

答案 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 :(得分: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>