如何使用 bootBuildImage 和 packeto 在 spring 引导中指定替代主 class

How to specify an alternative main class in spring boot using bootBuildImage and packeto

在 gradle 中调用 spring 启动插件 bootBuildImage 任务时,会使用 packeto 创建一个 docker 映像。它启动 spring 引导插件中指定的主要 class。您可以在下面找到 build.gradle 文件的摘录。

springBoot {
    mainClass = 'MyMainApp'
}


bootBuildImage {
    imageName = "$docker_repo/${project.name}"
}

调用 docker run 时,docker 将 运行 容器开始 MyMainApp

但是,我想 运行 另一个主要 class,使用相同的 docker 图像。我尝试了以下方法:

None 个选项开始 MyOtherApp.

Buildpacks 创建的图像提供了一些用于启动应用程序的有用工具。虽然这很好,但覆盖默认启动命令并不像仅向 docker run.

指定新命令那么容易

Buildpacks 为 starting up various processes in an image are described in the docs.

提供的所有功能

我在这里有点猜测,但听起来您想 运行 您自己的自定义进程(不是 buildpack 检测到的进程),所以请尝试 this one here.

You can even override the buildpack-defined process types:

docker run --rm --entrypoint launcher -it multi-process-app bash
docker run --rm --entrypoint launcher -it multi-process-app echo hello "$WORLD" # $WORLD is evaluated on the host machine
docker run --rm --entrypoint launcher -it multi-process-app echo hello '$WORLD' # $WORLD is evaluated in the container after profile scripts are sourced

Java 应该在路径上,所以你可以 运行 java -Dloader.main=MyOtherApp org.springframework.boot.loader.PropertiesLauncher.

https://www.baeldung.com/spring-boot-main-class#using-cli


或者,您可以将您的应用更改为默认使用 PropetiesLoader 并重建您的图像。 buildpack 只是从 MANIFEST.MF 文件中拉出启动命令的启动器。您需要使用 PropertiesLauncher,因为它支持 loader.main。参见 。