Maven verify ClassNotFoundException for class of Spring Boot application

2022-09-02 20:32:58

当我尝试为我的Java Spring Boot项目运行Maven的验证生命周期时,我得到了一个。它编译良好,但当我运行时,它失败了,因此:NoClassDefFoundErrormvn spring-boot:runmvn clean verify

$mvn clean verify -Dsurefire.skip=true -X
...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.209 sec <<< FAILURE! - in com.fitforger.FitForgerBackendApplicationTests
    initializationError(com.fitforger.FitForgerBackendApplicationTests)  Time elapsed: 0.005 sec  <<< ERROR!
    java.lang.NoClassDefFoundError: com/fitforger/model/GymRat
    Caused by: java.lang.ClassNotFoundException: com.fitforger.model.GymRat

其中 是我的应用程序的类之一。com.fitforger.model.GymRat

项目结构:

+----src
     +----main
     |    +----java
     |    |    +----com
     |    |         +----fitforger
     |    |              +----controller
     |    |              |    +----GymRatController.java
     |    |              |    +----WorkoutNodeController.java
     |    |              +----dao
     |    |              |    +----GymRatDAO.java
     |    |              +----exception
     |    |              |    +----GymRatCreationFailure.java
     |    |              |    +----GymRatExistsException.java
     |    |              |    +----GymRatNotFound.java
     |    |              |    +----GymRatUpdateFailure.java
     |    |              +----FitForgerBackendApplication.java
     |    |              +----model
     |    |              |    +----FitForgerModel.java
     |    |              |    +----GymRat.java
     |    |              |    +----WorkoutAttribute.java
     |    |              |    +----WorkoutNode.java
     |    |              |    +----WorkoutNodeTypes.java
     |    |              +----repository
     |    |                   +----CouchbaseRepository.java
     |    |                   +----FitForgerRepository.java
     |    +----resources
     |         +----application.properties
     +----test
          +----java
          |    +----com
          |         +----fitforger
          |              +----FitForgerBackendApplicationTests.java
          |              +----GymRatSteps.java
          |              +----repository
          |                   +----CouchbaseRepositoryTest.java
          +----resources
               +----com
                    +----fitforger
                         +----GymRat.feature

我的pom.xml的相关部分:

<build>
    <resources>
        <resource>
            <directory>resources</directory>
            <targetPath>${project.build.outputDirectory}</targetPath>
            <includes>
                <include>application.properties</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.5.1.RELEASE</version>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.1.RELEASE</version>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <excludes>
                        <exclude>com.fitforger.FitForgerBackendApplicationTests</exclude>
                    </excludes>
                    <skip>${surefire.skip}</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>com.fitforger.FitForgerBackendApplicationTests</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build

答案 1

通过执行以下操作解决OP问题,

看起来与您面临的情况相似。你能试试吗?

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <configuration>
        <classifier>exec</classifier>
   </configuration>
 </plugin>

答案 2

此问题也可能表现为集成测试用例无法找到您的资源。application.properties

发生这种情况的原因是 Spring Boot 重新打包的交互作用(由 完成)以及 用于设置集成测试类路径的逻辑。spring-boot-maven-pluginmaven-failsafe-plugin

Failsafe 插件将打包的 JAR 放在类路径上,而不是放在保存解压缩类和资源的目录上(由属性给出,通常是 )。但是,通过重新打包将应用程序的类和资源放在 JAR 中一个不寻常的位置,因此尽管 Failafe 会检查 JAR,但它找不到要查找的内容。project.build.outputDirectory${basedir}/target/classesspring-boot-maven-plugin

您可以通过显式告诉 Failsafe 插件使用类路径将保存解压缩的类和资源的目录放在使用类路径上来解决此问题,方法是在其配置中使用:additionalClasspathElements

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
            <additionalClasspathElements>
                <additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement>
            </additionalClasspathElements>
            <includes>
                <include>**/*IT.java</include>
            </includes>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

推荐