简体   繁体   English

Maven Shade插件-NoClassDefFound

[英]Maven Shade Plugin - NoClassDefFound

I am trying to create a jar to send to a colleague for testing purposes. 我正在尝试创建一个罐子,以发送给同事进行测试。 My program works fine on the Eclipse IDE. 我的程序在Eclipse IDE上运行良好。 However, when I try to use mvn install and mvn package to compile it into a jar, when I try to run the jar, I get the following error: [NoClassDefFound][1] (click on the link to see the screenshot). 但是,当我尝试使用mvn install和mvn软件包将其编译到jar中时,当我尝试运行jar时,出现以下错误:[NoClassDefFound] [1](单击链接以查看屏幕截图)。

Here is my POM file: 这是我的POM文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 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>

I would be really grateful if someone could help me to figure out what's happening. 如果有人可以帮助我弄清楚发生了什么,我将非常感激。

Thanks in advance! 提前致谢!

EDIT 2 编辑2

I added the following code just for testing purposes on my program: 我在程序中添加了以下代码,仅用于测试目的:

package com.br.iacit.tutorialdoJar; 包com.br.iacit.tutorialdoJar;

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

} }

On eclipse ide it shows Sun Microsystems, Inc. however in my jar file it prints null. 在Eclipse上,它显示了Sun Microsystems,Inc.,但是在我的jar文件中,它显示为null。 Maybe it has something to do with it? 也许与它有关?

I think you are missing the dependencies in your classpath, to correct this you might use the maven dependency plugin: 我认为您在类路径中缺少依赖项,要更正此问题,您可以使用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>

Then you would need also to add the classpath to your manifest, for shade I think you need to add in <ManifestEntries> the following: 然后,您还需要将类路径添加到清单中,对于阴影,我认为您需要在<ManifestEntries>添加以下内容:

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

Edit: Fair enough if you don't need the shade plugin, what I can suggest to you is the pom below, which uses a simpler plugin to compile the depedencies, the maven dependency plugin : 编辑:如果您不需要shade插件,那还算公平,我可以向您建议的是以下pom,它使用一个更简单的插件来编译依赖关系 ,即maven依赖插件

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

I solved my problem by adding the following attributes to my pom file: 我通过在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>

Here is my final POM file: 这是我最后的POM文件:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 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>

i faced same issue. 我面临着同样的问题。 but now its working fine for me. 但现在对我来说很好。 try below steps. 请尝试以下步骤。

first you need to delete this ' .m2 ' file from given location 首先,您需要从指定位置删除此“ .m2”文件

Windows: C:\\Users\\.m2 Windows:C:\\ Users \\ .m2

then - update your maven project. 然后-更新您的Maven项目。 then - clean install and everything seems to be fine 然后-干净安装,一切似乎都很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM