是否可以直接针对作为包目标而不是源创建的 jar 文件来运行 maven,特别是 spring-boot:run 目标?
我想这样做的原因是因为我想使用 spring boot Devtools 在具有热交换类(存储在主机上并使用共享卷映射到容器中)的 Docker 容器中运行 spring boot 应用程序(遗憾的是,如果您使用 Java -jar 运行应用程序,则该应用程序不起作用),并且我不想将应用程序的源代码也放入容器内。
最佳答案
classes
参数不是可选的(通常使用标准 Maven 项目结构推断),但您可以为其提供一个空目录,然后使用 folders
参数包含之前打包的 JAR。这是一个例子
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.this.that.YourApplication</mainClass>
<!-- any directory will do as long as (a) it exists and (b) it does not contain classes -->
<classesDirectory>/tmp/</classesDirectory>
<folders>
<folder>
<!-- the address, relative to the plugin's workingDirectory, of the 'original' application JAR -->
tmp/lib/your-application.jar.original
</folder>
</folders>
</configuration>
</plugin>
使用此配置,如果您使用 -X
运行,您将看到 Spring Boot 插件生成的 JVM 类路径中的前两个条目是 (1) 您的应用程序 JAR 和 (2) 空类目录。例如:
[DEBUG] Classpath for forked process: <full_path_removed>/tmp/lib/your-application.jar.original:/tmp:
注释:
- 您需要提供
mainClass
,因为您使用的是原始 JAR,该 JAR 不包含 META-INF/MANIFEST.MF 中的 Main-Class 指令 - 您需要引用“原始”JAR,而不是 Spring Boot 打包的 JAR,因为原始 JAR 在其原始位置包含应用程序的主类(Spring Boot 打包的 JAR 会重新定位它)。
关于java - Maven Spring 启动 :run against compiled jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869397/