适用于salesforce API jar的Maven shaded jar中的java.lang.NoClassDefFoundError

时间:2018-03-21 17:17:29

标签: java maven jar salesforce

我有一个Apache Maven Java项目,它导入了类:

com.sforce.soap.enterprise.EnterpriseConnection

从IntelliJ运行项目时,我执行代码没有问题。但是,我希望能够从命令行运行项目作为着色jar。我一直在使用“mvn package'”对罐子进行着色。当我从命令行运行jar时,我收到错误:

线程中的异常" main" java.lang.NoClassDefFoundError:com / sforce / soap / enterprise / EnterpriseConnection

我的pom包含依赖项:

<dependency>
  <groupId>qa-integration.sfdc</groupId>
  <artifactId>enterprise</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/libs/enterprise-1.0.0.jar</systemPath>
</dependency>
<dependency>
  <groupId>qa-integration.sfdc</groupId>
  <artifactId>metadata</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/libs/metadata-1.0.0.jar</systemPath>
</dependency>

我的阴影执行如下:

      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
          <createDependencyReducedPom>false</createDependencyReducedPom>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.sunrun.integration.sfdc.metadata.Main</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>

当我列出jar的内容时,我根本看不到任何salesforce文件。

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,我的解决方案是将以下代码段添加到我的pom.xml中:

<build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>corrigo.Main</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id> <!-- this is used for inheritance merges -->
                            <phase>package</phase> <!-- bind to the packaging phase -->
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!--<plugin>-->
                    <!--<groupId>org.apache.maven.plugins</groupId>-->
                    <!--<artifactId>maven-jar-plugin</artifactId>-->
                    <!--<version>2.4</version>-->
                    <!--<configuration>-->
                        <!--<archive>-->
                            <!--<manifest>-->
                                <!--<mainClass>corrigo.Main</mainClass>-->
                            <!--</manifest>-->
                        <!--</archive>-->
                    <!--</configuration>-->
                <!--</plugin>-->
            </plugins>
        </build>

然后你就跑mvn package。 这是一个可靠的资源: https://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/