使用SNAPSHOT依赖项时找不到Maven类定义

时间:2018-08-02 18:30:40

标签: maven noclassdeffounderror

我正在为使用Pi4j的RaspberryPi构建一个应用程序。在我遇到需要将Pi4j版本从1.1更改为1.2-SNAPSHOT之前,它运行良好。我仅在使用SNAPSHOT时得到NoCassDefFoundException

我正在使用一些插件来分离我的jar和所有库,它们存储在lib/*目录中。 maven-dependency-plugin完成了工作

maven-jar-plugin将带有libs的目录链接到我的jar

maven-antrun-plugin复制我的jar,仅在mvn install中更改对树莓的依赖关系

我的pom.xml文件(已剥离很多)

<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>Rpi</groupId>
<artifactId>Rpi</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <!--<pi4j.version>1.2-SNAPSHOT</pi4j.version>-->
    <pi4j.version>1.1</pi4j.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.pi4j</groupId>
        <artifactId>pi4j-core</artifactId>
        <version>${pi4j.version}</version>
    </dependency>
    (...)
</dependencies>

<repositories>
    <repository>
        <id>oss-snapshots-repo</id>
        <name>Sonatype OSS Maven Repository</name>
        <url>https://oss.sonatype.org/content/groups/public</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </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>true</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>${pi.main.class}</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <scp
                                    file="${project.build.directory}/${project.build.finalName}.jar"
                                    todir="${pi.user}:${pi.password}@${pi.host}:${pi.deployDirectory}"
                                    port="${pi.port}" trust="true" verbose="false" failonerror="false">
                            </scp>

                            <!-- copy dependencies, only new or modified-->
                            <scp todir="${pi.user}:${pi.password}@${pi.host}:${pi.deployDirectory}/lib"
                                 port="${pi.port}" trust="true" verbose="false" failonerror="false">
                                <fileset dir="${project.build.directory}/lib">
                                    <modified>
                                        <param name="cache.cachefile" value="localdev.cache"/>
                                    </modified>
                                </fileset>
                            </scp>

                        </tasks>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant-jsch</artifactId>
                    <version>1.9.6</version>
                </dependency>
            </dependencies>
        </plugin>
        (...)
    </plugins>
</build>

mvn install一切看起来都很好之后,我的jar就应该放置在这里,所有依赖项都被复制了,我使用java -jar Rpi-0.0.1-SNAPSHOT.jar运行了我的jar,它一直运行到提出NoClassDefFoundError为止。

问题是当我仅使用Pi4j 1.2-SNAPSHOT时。当我使用1.1版时,它工作正常(崩溃,这就是为什么我必须切换到1.2-SNAPSHOT的原因)。如果我在pom.xml中使用1.1版,并且在mvn install重命名lib\pi4j-core-1.2-SNAPSHOT.jarlib\pi4j-core-1.1.jar之后,使用1.2版本,一切都按预期运行,没有崩溃...

1 个答案:

答案 0 :(得分:0)

Maven-jar-plugin在我的jar类路径中更改了SNAPSHOT依赖项的名称。要解决此问题,我必须在插件清单中添加<useUniqueVersions>false</useUniqueVersions>,所以现在看起来像这样

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>${pi.main.class}</mainClass>
                        <useUniqueVersions>false</useUniqueVersions>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

工作正常