首页 > 解决方案 > Spring Boot 从战争转移到 jar

问题描述

我尝试使用 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,我的意思是repackage向 spring-boot-maven-plugin 添加一个目标:

        <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>

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,为什么?

标签: javaspringmavenspring-bootmaven-failsafe-plugin

解决方案


好的,所以问题是我使用的是故障安全 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} exec build-info org.springframework springloaded 1.2。 6.发布


推荐阅读