春季靴子从战争转移到罐子

时间:2018-07-09 15:44:11

标签: java spring maven spring-boot maven-failsafe-plugin

我尝试使用Spring Boot 2.0.2从战争转移到jar,并且在使用故障安全的maven测试中遇到问题。

我看到两种错误:

  1. Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest

  2. java.lang.NoClassDefFoundError,其中列出了我的一个bean类

当我在IntelliJ中运行这些测试时,一切正常,但是在maven中失败。 同时,当我回到发动战争而不是jar的时候,一切也都有效。

通过从战争转移到jar,我的意思是向spring-boot-maven-plugin添加一个repackage目标:

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <skip>${spring.boot.skip}</skip>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.6.RELEASE</version>
                </dependency>
            </dependencies>
        </plugin>

然后从具有main方法的类中删除extends SpringBootServletInitializer,然后将configure方法移至main方法中:

@Import(JpaAuditingHandlerRegistrar.class)
@EnableTransactionManagement
@EnableCaching
@Lazy
public class App {

public static void main(String[] args) {
    configure(new SpringApplicationBuilder(App.class))
        .run(args);
}


private static SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    String env = System.getenv("ENV");
    if (env != null) {
        if (Collections.disjoint(List.of(PRODUCTION), List.of(pulsarEnv))) {
            throw new IllegalArgumentException(
                String.format("Unknown ENV provided: %s", env)
            );
        }
        application.profiles(
            env,
            Profiles.METRICS
        );
    }
    return application;
}


public static void main(String[] args) {
    configure(new SpringApplicationBuilder(App.class))
        .run(args);
}

我的测试类带有以下注释:

@EnableConfigurationProperties
@TestExecutionListeners(listeners = {
    DirtiesContextTestExecutionListener.class,
    DirtiesContextBeforeModesTestExecutionListener.class,
    ServletTestExecutionListener.class,
    DependencyInjectionTestExecutionListener.class,
    TransactionalTestExecutionListener.class,
    MockitoTestExecutionListener.class
})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(TestConfiguration.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)

另一个难题,当我使用进行测试时

mvn clean test-compile failsafe:integration-test

它们都起作用,所以只有在mvn clean install的情况下它们才会失败,为什么?

1 个答案:

答案 0 :(得分:0)

好的,所以问题是我使用的是故障安全2.19.1,此版本自Spring boot 1.4.x起存在问题:https://github.com/spring-projects/spring-boot/issues/6254

解决方案是将故障安全降级到2.18,对其进行升级(但随后会出现另一组问题)或添加

<classifier>exec</classifier>

对于spring-boot-maven-plugin的配置部分,在我的情况下是:                              org.springframework.boot                 spring-boot-maven-plugin                                      $ {spring.boot.skip}                     执行                                                                                                             构建信息                                                                                                                              org.springframework                         弹簧加载                         1.2.6。发布