Maven Shade 插件 - NoClassDefFounderror

Maven Shade Plugin - NoClassDefFound

我正在尝试创建一个 jar 发送给同事进行测试。我的程序在 Eclipse IDE 上运行良好。但是,当我尝试使用 mvn install 和 mvn package 将其编译成 jar 时,当我尝试 运行 jar 时,出现以下错误: [NoClassDefFound][1](点击link查看截图)。

这是我的 POM 文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>org.geotools.tutorial.tutorialCerto</groupId>
<artifactId>tutorialCerto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>tutorial</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>15.1</geotools.version>
    <geotools.version>17-SNAPSHOT</geotools.version>
</properties>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-process</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-swing</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-geotiff</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-image</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-wms</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-swing</artifactId>
        <version>${geotools.version}</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net repository</name>
        <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
        <id>osgeo</id>
        <name>Open Source Geospatial Foundation Repository</name>
        <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
    <repository>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <id>boundless</id>
        <name>Boundless Maven Repository</name>
        <url>http://repo.boundlessgeo.com/main</url>
    </repository>
</repositories>
<build>

    <plugins>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <encoding>UTF-8</encoding>
              <target>1.8</target>
              <source>1.8</source>
          </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.0.0</version>
          <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                      <goal>shade</goal>
                  </goals>
                  <configuration>
                      <transformers>
                          <!-- This bit sets the main class for the executable jar as you otherwise -->
                          <!-- would with the assembly plugin                                       -->
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                              <manifestEntries>
                                  <Main-Class>org.geotools.tutorial.tutorialCerto.ImageLab</Main-Class>
                              </manifestEntries>
                          </transformer>
                          <!-- This bit merges the various GeoTools META-INF/services files         -->
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                      </transformers>
                  </configuration>
              </execution>
          </executions>
      </plugin>
    </plugins>
</build>

如果有人能帮助我弄清楚发生了什么,我将不胜感激。

提前致谢!

编辑 2

我添加了以下代码只是为了在我的程序中进行测试:

包 com.br.iacit.tutorialdoJar;

public class App {
public static void main( String[] args )
{      System.out.println(com.sun.media.imageioimpl.common.PackageUtil.getVendor());
}

}

在 eclipse ide 上它显示 Sun Microsystems, Inc. 但是在我的 jar 文件中它打印 null。或许跟它有关系?

我认为您缺少类路径中的依赖项,要更正此问题,您可以使用 maven 依赖项插件:

     <build>
         <plugins>
           <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
   </build>

然后您还需要将类路径添加到您的清单中,我认为您需要在 <ManifestEntries> 中添加以下内容:

 <Class-Path>${project.build.directory}/lib</Class-Path>

编辑:很公平 如果你不需要 shade 插件,我可以向你建议的是下面的 pom,它使用更简单的插件来编译 depedencies, maven dependency plugin:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test.whatever</groupId>
    <artifactId>shade-tester</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>shade-tester</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>15.1</geotools.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-process</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geotiff</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-image</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-wms</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>boundless</id>
            <name>Boundless Maven Repository</name>
            <url>http://repo.boundlessgeo.com/main</url>
        </repository>
    </repositories>
    <build>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
<!--                Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.test.whatever.shade_tester.App</mainClass>
                        </manifest>
                    </archive>
                    <excludes>
                        <exclude>**/*log4j*</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!-- <plugin> -->
            <!-- <groupId>org.apache.maven.plugins</groupId> -->
            <!-- <artifactId>maven-shade-plugin</artifactId> -->
            <!-- <version>3.0.0</version> -->
            <!-- <executions> -->
            <!-- <execution> -->
            <!-- <phase>package</phase> -->
            <!-- <goals> -->
            <!-- <goal>shade</goal> -->
            <!-- </goals> -->
            <!-- <configuration> -->
            <!-- <transformers> -->
            <!-- This bit sets the main class for the executable jar as you otherwise -->
            <!-- would with the assembly plugin -->
            <!-- <transformer -->
            <!-- implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> -->
            <!-- <manifestEntries> -->
            <!-- <Main-Class>org.test.whatever.shade_tester.App</Main-Class> -->
            <!-- <Class-Path>lib/</Class-Path> -->
            <!-- </manifestEntries> -->
            <!-- </transformer> -->
            <!-- This bit merges the various GeoTools META-INF/services files -->
            <!-- <transformer -->
            <!-- implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" 
                /> -->
            <!-- </transformers> -->
            <!-- </configuration> -->
            <!-- </execution> -->
            <!-- </executions> -->
            <!-- </plugin> -->
        </plugins>
    </build>
</project>

我通过将以下属性添加到我的 pom 文件解决了我的问题:

<manifestEntries>
    <Main-Class>com.br.iacit.tutorialdoJar.ImageLab</Main-Class>
    <Specification-Title>Java Advanced Imaging Image I/O Tools</Specification-Title>
    <Specification-Version>1.1</Specification-Version>
    <Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
    <Implementation-Title>com.sun.media.imageio</Implementation-Title>
    <Implementation-Version>1.1</Implementation-Version>
    <Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>

这是我最终的 POM 文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.br.iacit</groupId>
<artifactId>tutorialdoJar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>tutorialdoJar</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>17-SNAPSHOT</geotools.version>
</properties>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-process</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-swing</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-epsg-hsql</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-geotiff</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-image</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-wms</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-swing</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-imageio-ext-gdal</artifactId>
        <version>${geotools.version}</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net repository</name>
        <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
        <id>osgeo</id>
        <name>Open Source Geospatial Foundation Repository</name>
        <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
    <repository>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <id>boundless</id>
        <name>Boundless Maven Repository</name>
        <url>http://repo.boundlessgeo.com/main</url>
    </repository>
</repositories>
<build>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <encoding>UTF-8</encoding>
                <target>1.8</target>
                <source>1.8</source>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <!-- This bit sets the main class for the executable jar as you otherwise -->
                            <!-- would with the assembly plugin -->
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>com.br.iacit.tutorialdoJar.ImageLab</Main-Class>
                                    <Specification-Title>Java Advanced Imaging Image I/O Tools</Specification-Title>
                                    <Specification-Version>1.1</Specification-Version>
                                    <Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
                                    <Implementation-Title>com.sun.media.imageio</Implementation-Title>
                                    <Implementation-Version>1.1</Implementation-Version>
                                    <Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
                                </manifestEntries>
                            </transformer>
                            <!-- This bit merges the various GeoTools META-INF/services files -->
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我遇到了同样的问题。但现在它对我来说工作正常。 尝试以下步骤。

首先您需要从给定位置删除此“.m2”文件

Windows: C:\用户\.m2

然后 - 更新您的 Maven 项目。 然后 - 全新安装,一切似乎都很好