Java maven-dependency-plugin运行时的ClassNotFoundException

时间:2015-08-05 13:59:43

标签: java maven classnotfoundexception maven-dependency-plugin

我在我的项目中使用TwelveMonkeys库(com.twelvemonkeys.imageio:imageio-tiff:3.1.1)。另外,我正在使用 Maven maven-dependency-plugin来构建我的项目,我正在配置它:

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

该项目正在正确编译,但它在运行时抱怨ClassNotFoundException: com.twelvemonkeys.io.enc.Decoder。我猜,includeArtifactIds不包括其中指定的库的依赖项(嵌套依赖项)。这就是我在pom.xml中包含 TwelveMonkeys 库的方法:

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

有人可以建议我如何在我的FINAL FAT JAR文件中包含imageio-tiffimageio-psd所依赖的所有库?只需要有一个更好的方法来指定“包含所有依赖项,并具有自己的依赖项”。

修改

我也使用ProGaurd进行混淆,配置如下:

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

1 个答案:

答案 0 :(得分:0)

这是我的pom.xml的一部分,用于构建包含所有依赖项的.JAR文件。

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

并添加到您的依赖项:

<scope>compile</scope>

我通常按以下顺序构建一个jar:

  • Maven Clean
  • Maven Compile
  • Maven Package

这适用于我包括所有库。