简体   繁体   中英

Java maven-dependency-plugin ClassNotFoundException at runtime

I am using TwelveMonkeys library ( com.twelvemonkeys.imageio:imageio-tiff:3.1.1 ) in my project. Also, I am using Maven with maven-dependency-plugin to build my project and I am configuring it like this:

<plugin>
    <!-- Get the dependencies jar files to a common place, for jar signing -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <includeArtifactIds>commons-net,imageio-psd,imageio-tiff</includeArtifactIds>
                <!-- <includeGroupIds>com.twelvemonkeys.imageio</includeGroupIds> -->
                <outputDirectory>${project.build.directory}/classes</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

The project is getting compiled properly, but its complaining about ClassNotFoundException: com.twelvemonkeys.io.enc.Decoder at runtime. I guess, includeArtifactIds is not including dependencies of the libraries specified inside it (nested dependencies). This is how I am including TwelveMonkeys libraries in my pom.xml:

<dependencies>  
    ...
    <dependency>
        <groupId>com.twelvemonkeys.imageio</groupId>
        <artifactId>imageio-psd</artifactId>
        <version>3.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.twelvemonkeys.imageio</groupId>
        <artifactId>imageio-tiff</artifactId>
        <version>3.1.1</version> <!-- Alternatively, build your own version -->
    </dependency>
    ...
</dependencies>

Could anybody please suggest me how can I include all libraries on which imageio-tiff and imageio-psd depends in my FINAL FAT JAR file? There just got to be a better way to specify "include all dependencies, with there own dependencies".

EDIT:

I am also using ProGaurd for obfuscation with following config:

<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.10</version>

    <dependencies>
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>5.2</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <executions>
       <execution>
           <phase>package</phase>
           <goals><goal>proguard</goal></goals>
       </execution>
    </executions>
    <configuration>
        <proguardVersion>5.2</proguardVersion>

        <obfuscate>true</obfuscate>
        <injar>${project.build.finalName}.jar</injar>
        <outjar>${project.build.finalName}.jar</outjar>
        <outputDirectory>${project.build.directory}</outputDirectory>

        <options>
            <option>-allowaccessmodification</option>
            ...
            <option>-keep public class com.twelvemonkeys.** { *; }</option>
        </options>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jsse.jar</lib>
            <lib>${java.home}/lib/jce.jar</lib>
            <lib>${java.home}/lib/plugin.jar</lib>
        </libs>
    </configuration>
</plugin>

This is part of my pom.xml to build an .JAR file with all the dependencies included.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
         </plugin
    </plugins>
</build>

And add to your dependency:

<scope>compile</scope>

I usually build a jar in the following order:

  • Maven Clean
  • Maven Compile
  • Maven Package

This works for me to include all libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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