java - Maven Spring 启动 :run against compiled jar

标签 java maven spring-boot

是否可以直接针对作为包目标而不是源创建的 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/

相关文章:

java - 用于跟踪文档管理系统内文档状态的数据库

java - ORA-01843: 不是有效的月份 JPA

java - Spring Boot 项目的 Redis max-active 配置值

Spring boot Embedded Derby 在最新版本中不起作用。

java - 如何从 StateMachineBase.java 调用 native 接口(interface)并使用代号一从 native 实现发回一些值?

具有通用返回类型的访问者模式的 Java/Kotlin 强制转换异常

java - 新的 ModelAndView 不起作用

maven - org.apache.http.conn.HttpHostConnectException : Connect to localhost:19538 [localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)

java - 如何在 netbeans IDE 7.4 中指定构建选项

spring-boot - 我们可以使用 Electron 工具为 Spring boot web mvc 创建独立的应用程序吗?