gpt4 book ai didi

spring-boot - gradle 和对 spring-boot 子项目的依赖

转载 作者:行者123 更新时间:2023-12-03 03:06:09 27 4
gpt4 key购买 nike

我创建了一个包含多个 gradle 子项目的项目,包括:“app”和“tests”。

测试在其依赖项中有“app”。测试使用“app”中的类

当我运行时:

./gradlew clean test build

一切正常,测试运行并通过。

但是当我运行时:
./gradlew clean build

然后测试编译失败,错误提示缺少一个类 - 在这种情况下,它是一个 spring-boot 配置类。我使用 --debug 运行它,结果发现在失败的情况下 app:bootRepackage 任务在测试之前执行,应用程序编译生成的 jar 被更改,这就是找不到类的原因。

如何使“./gradlew clean build”正常工作?

使用:spring-boot 1.5、gradle 4.0(和 4.1 也是)、io.spring.dependency-management 插件 1.0.0.RELEASE

最佳答案

理想情况下,您不应该使用 Spring Boot 应用程序(已经重新打包的东西)作为依赖项。来自 the documentation :

Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.



如果建议的解决方案在您的情况下是不可能的,文档会继续描述替代方案:

If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.

To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.

To configure a classifier of exec … when using Gradle, the following configuration can be used:

bootRepackage  {
classifier = 'exec'
}

关于spring-boot - gradle 和对 spring-boot 子项目的依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45829185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com