Maven Failsafe Classpath

一世执手 提交于 2019-11-29 11:31:09

I figured it out, so I'm answering my own quesiton in case someone else has the same problem.

It turns out that maven-failsafe-plugin does not add target/classes directory to the classpath, but rather the resulting jar, which works fine in most cases.

When it comes to Spring Boot, however, the resulting jar contains Spring Boot custom classloader classes in place of contents of target/classes directory, which are moved to directory BOOT-INF/classes. Since maven-failsafe-plugin uses 'regular' classloader it only loads Spring Boot classloader classes, failing in the first place it is expected to use one of the project classes.

To run IT tests in Spring Boot project, one has to exclude the packaged jar from dependencies and add either the original, unmodified jar or target/classes directory, which is what I did.

The correct configuration for maven-failsafe-plugin and Spring Boot is:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.21.0</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                 <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <classpathDependencyExcludes>
            <classpathDependencyExcludes>${groupId}:${artifactId}</classpathDependencyExcludes>
        </classpathDependencyExcludes>
        <additionalClasspathElements>
            <additionalClasspathElement>${project.build.outputDirectory}</additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
</plugin>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!