maven程序集插件的行为与mvn exec:java不同

时间:2016-06-15 14:41:21

标签: java maven maven-assembly-plugin

使用<form id="registerform" name="registerform" method="post"> First name:<br> <input type="text" name="firstname"><br> Last name:<br> <input type="text" name="lastname"> <input type="hidden" name="token" value="<?php echo $token; ?>"> </form> 运行java程序时,程序运行正常。当我使用mvn exec:java创建单个jar文件时,运行相同的类会导致加载旧版本的依赖项导致的问题。

我得到的错误是:

maven-assembly-plugin

导致此错误是因为加载的Caused by: java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144) at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.getPreferredSocketFactory(ApacheConnectionManagerFactory.java:87) at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:65) at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:58) at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:50) at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:38) 版本小于4.3。

我将此依赖项置于httpclient的顶部。

pom.xml

我使用的<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> 文件如下所示:

assembly.xml

这是依赖树

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <excludes>
                <exclude>com.almworks.sqlite4java:libsqlite4java-linux-i386</exclude>
                <exclude>com.almworks.sqlite4java:libsqlite4java-linux-amd64</exclude>
                <exclude>com.almworks.sqlite4java:sqlite4java-win32-x64</exclude>
                <exclude>com.almworks.sqlite4java:sqlite4java-win32-x86</exclude>
                <exclude>com.almworks.sqlite4java:libsqlite4java-osx</exclude>

            </excludes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>false</unpack>
            <includes>
                <include>com.almworks.sqlite4java:libsqlite4java-linux-i386</include>
                <include>com.almworks.sqlite4java:libsqlite4java-linux-amd64</include>
                <include>com.almworks.sqlite4java:sqlite4java-win32-x64</include>
                <include>com.almworks.sqlite4java:sqlite4java-win32-x86</include>
                <include>com.almworks.sqlite4java:libsqlite4java-osx</include>
            </includes>
        </dependencySet>
    </dependencySets>

</assembly>

为什么会这样?

2 个答案:

答案 0 :(得分:0)

代码结构不是问题。问题是由Spark 1.2.2引起的。当我使用spark-submit运行代码时,SparkSubmit.java会加载分配给Spark代码的旧版httpclient,从而导致此冲突。

我使用的解决方案很简单,我使用apache-shade插件`,对库进行着色,效果很好。

这意味着两个版本可以同时工作而不会出现任何问题。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <relocations>
                            <relocation>
                                <pattern>org.apache.http</pattern>
                                <shadedPattern>shaded.org.apache.http</shadedPattern>
                            </relocation>
                        </relocations>
                        ......

答案 1 :(得分:-1)

从您的依赖项列表中,您在类路径上有两个不同的http-client个jar:

[INFO] +- com.mohataher.xx:base:jar:1.0-SNAPSHOT:compile
[INFO] |  \- com.amazonaws:dynamodb-titan100-storage-backend:jar:1.0.0:compile
[INFO] |     +- com.amazonaws:aws-java-sdk-dynamodb:jar:1.11.7:compile
[INFO] |     |  \- com.amazonaws:aws-java-sdk-core:jar:1.11.7:compile
[INFO] |     |     +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile

[INFO] +- com.mohataher.xx:importer:jar:1.0-SNAPSHOT:compile
[INFO] |  \- org.apache.spark:spark-core_2.10:jar:1.2.2:compile
[INFO] |     +- net.java.dev.jets3t:jets3t:jar:0.7.1:compile
[INFO] |     |  \- commons-httpclient:commons-httpclient:jar:3.1:compile

您需要排除旧版本。我建议在com.mohataher.xx:importer的pom.xml中执行此操作:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>1.2.2</version>
    <exclusions>
      <exclusion>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
      </exclusion>
    </exclusions>
</dependency>

然后在同一个pom.xml中添加对新版本的依赖。