提问者:小点点

Spring Boot从战争走向战争


我尝试使用Spring Boot 2.0.2从战争转移到罐子,并且在使用故障安全进行maven测试时遇到了问题。

我看到两种错误:

> < li>

无法找到@SpringBootConfiguration,您需要使用@ContextConfiguration或@SpringBootTest

< code > Java . lang . noclassdeffounderror ,其中列出了我的一个bean类

当我在 IntelliJ 中运行这些测试时,一切正常,但它们在 maven 中失败。与此同时,当我回到建立战争而不是罐子时,一切都有效。

通过从战争转移到jar,我的意思是在Spring启动专家插件中添加一个重新打包目标:

        <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方法的类中删除扩展SpringBootServletOrializer,并将confiure方法移动到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个答案

匿名用户

好吧,所以问题是我用的是故障保护2.19.1,这个版本自Spring启动1.4 . x:https://github.com/spring-projects/spring-boot/issues/6254以来就有问题

解决方案是将故障保护降级到2.18,升级它(但是另一组问题出现)或添加

<classifier>exec</classifier>

对于spring-boot-maven-plugin的配置部分,在我的例子中是:org . spring framework . boot spring-boot-maven-plugin $ { spring . boot . skip } exec build-info org . spring framework spring loaded 1 . 2 . 6 . release