mvn spring-boot:运行vs Java -jar [英] mvn spring-boot:run vs java -jar

查看:75
本文介绍了mvn spring-boot:运行vs Java -jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来可能很愚蠢,但是我无法理解mvn spring-boot:runjava -jar(通过mvn install生成的.jar文件)之间的区别

I know it may sound silly question but I am unable to understand the difference between mvn spring-boot:run and java -jar (.jar file generated with mvn install)

我在/src/main/resources/META-INF/resources/WEB-INF/中有一个带有jsp页面的spring boot应用程序.如果我使用mvn spring-boot:run,则将提供这些页面.但是,如果使用java -jar,则应用程序找不到这些页面.

I have a spring boot application with jsp pages in /src/main/resources/META-INF/resources/WEB-INF/. If I use mvn spring-boot:run these pages are served. But If I use java -jar these pages are not found by application.

我正在处理的应用程序位于 https://github.com/ArslanAnjum/angularSpringApi

The application that I am working on is at https://github.com/ArslanAnjum/angularSpringApi

更新: 它适用于Spring Boot 1.4.2.RELEASE,而我打算使用最新版本,即1.5.8.RELEASE.

UPDATE: It works with spring boot 1.4.2.RELEASE while I intend to use the latest version i.e., 1.5.8.RELEASE.

更新: 好吧,我通过将jsps放入src/main/webapp/WEB-INF/views/并将包装类型更改为war,然后使用java -jar target/myapp.war进行了该战争,并且现在可以正常工作了.

UPDATE: Well I solved the problem by putting jsps in src/main/webapp/WEB-INF/views/ and changing packaging type to war and then running this war using java -jar target/myapp.war and its working fine now.

推荐答案

简短的回答:spring-boot:run是steroïd上的>命令 ,它作为Maven构建的一部分运行,确保所有必需的参数被传递到您的应用程序(例如资源). spring-boot:run还将通过在运行应用之前执行test-compile生命周期目标来确保编译项目.

Short answer: spring-boot:run is a java -jar command on steroïd running as part of your Maven build, ensuring all required parameters are passed to your app (such as resources). spring-boot:run will also ensure that your project is compiled by executing test-compile lifecycle goals prior to running your app.

长答案:

运行java -jar时,将使用传递给此JVM的所有参数启动新的JVM实例.例如,使用Spring文档示例

When you run java -jar, you launch a new JVM instance with all the parameters you passed to this JVM. For example, using the Spring doc example

java -Xdebug -Xrunjdwp:server=y, \
    transport=dt_socket, address=8000, suspend=
    -jar target/myproject-0.0.1-SNAPSHOT.jar

您将使用给定的参数启动一个全新的JVM.您需要确保在命令行中包括所需的所有内容,例如类路径元素,应用程序参数,JVM选项等.

You will launch a brand new JVM with the given parameters. You need to make sure to include everything needed, such as classpath elements, application parameters, JVM options, etc. on the command line.

运行mvn spring-boot:run时,您将启动一个Maven构建,该构建将:

When you run mvn spring-boot:run, you launch a Maven build that will:

  • 运行test-compile生命周期目标,默认情况下它将是Maven资源和编译器插件的resources:resourcescompiler:compileresources:testResourcescompiler:testCompile目标.
  • 使用一堆参数启动您的应用程序,这些参数取决于 您在项目中定义的Spring Boot Maven插件配置(您的pom.xml,父项和设置,命令行等).其中包括:
    • 许多类路径元素:您的target/classes文件夹,其中可能包含应用程序所需的资源和库,您的Maven依赖项等.
    • 是否分叉JVM(是否创建全新的JVM来运行您的应用程序或重新使用Maven构建的JVM),请参见插件的forkagent参数
    • Run the test-compile lifecycle goals, by default it will be resources:resources, compiler:compile, resources:testResources, compiler:testCompile goals of the Maven Resources and Compiler plugin.
    • Launch your application with a bunch of parameters that will depend on the Spring Boot Maven Plugin configuration you defined in your project (your pom.xml, parents and settings, command line, etc.). This includes among other things:
      • A lot of classpath elements: your target/classes folder which may contain resources and libraries required by your app, your Maven dependencies, etc.
      • Whether to fork your JVM or not (whether to create a brand new JVM to run your app or re-use the JVM of the Maven build), see fork and agent parameter of the plugin

      按照:

      我有一个带有jsp页面的spring boot应用程序 /src/main/resources/META-INF/resources/WEB-INF/.如果我使用MVN spring-boot:运行这些页面.但是如果我使用java -jar这些 应用程序找不到页面.

      I have a spring boot application with jsp pages in /src/main/resources/META-INF/resources/WEB-INF/. If I use mvn spring-boot:run these pages are served. But If I use java -jar these pages are not found by application.

      这是因为mvn spring:boot命令将确保在您的应用程序运行时,您的target/classes文件夹位于类路径中.编译后,此文件夹将除其他内容外还包含target/classes/META-INF/resources/WEB-INF.然后,您的应用程序将能够找到META-INF/resources/WEB-INF并在询问时加载它们.当您运行java -jar命令时,该文件夹可能不在类路径中,因此您的应用程序无法找到您的资源. (这些资源是在resources:resources目标期间从src/main/resources文件夹复制的)

      It's because the mvn spring:boot command will make sure your target/classes folder is present in the Classpath when your app is running. After compilation, this folder will contain target/classes/META-INF/resources/WEB-INF among other things. Your app will then be able to find META-INF/resources/WEB-INF and load them when asked. When you ran java -jar command, this folder was probably not on the classpath, your app was then not able to find your resources. (these resources were copied from the src/main/resources folder during the resources:resources goal)

      要在java -jar命令中获得相似的结果,必须将资源包括在类路径中,例如javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/

      To have a similar result with your java -jar command, you must include your resources on the classpath such as javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/

      这篇关于mvn spring-boot:运行vs Java -jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆