java - Spring Boot 从战争转移到 jar
问题描述
我尝试使用 Spring Boot 2.0.2 从战争转移到 jar,但我在使用故障安全的 maven 测试中遇到了问题。
我看到两种错误:
Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest
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
,为什么?
解决方案
好的,所以问题是我使用的是故障安全 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.发布
推荐阅读
- firebase - 在颤振的异步函数中返回布尔值
- git - 如何根据谓词更改特定提交的作者
- angular - 当 Angular 中的 url 参数更改时,是否可以重新加载组件?
- javascript - 我正在尝试让我的游戏自动在单元格中放置一些东西,但我不确定如何获取单元格(js)
- java - 如何设置在按下 JavaFX 中的任何键盘键时发生的动作?
- node.js - 如何在nodejs中动态创建翡翠元素?
- excel - 如何将 RANDBETWEEN 用于 2 个数字范围?
- excel - 使用 UDF 从字符串中删除某些字符
- python - Python:为什么有时不调用 __init__ 方法?
- python - 在 Python 单元测试中模拟日志输出