繁体   English   中英

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

[英]Spring Boot moving from war to 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与我列出的我的一个豆类

当我在IntelliJ中运行这些测试时,一切正常,但在maven中失败。 同时,当我回到战争而不是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它们才会失败,为什么?

好的,所以问题是我正在使用故障安全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.发布

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM