简介

  Spring Boot Maven Plugin插件提供spring boot在maven中的支持。允许你打包可运行的jar包或war包。

  插件提供了几个maven目标和Spring Boot 应用一起工作。总的有:

  run: 这个目标和直接java -jar 来启动基本相同,唯一不同的是java -jar需要打包后才能跑,而run目标不需要。这个当程序编译打包需要比较长时间时,用run目标来跑可以节约时间。

一、简单使用

  Spring Boot Maven Plugin 包括一个 run 目标,该目标被用于从命令行启动你的应用程序。比如:

 mvn spring-boot:run 

  eclipse用法如下:

  

  启动后的日志:

 [INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springboot 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) > test-compile @ springboot >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) < test-compile @ springboot <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) @ springboot ---
[INFO] Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.6.RELEASE/spring-boot-loader-tools-1.5.6.RELEASE.pom
[INFO] Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.6.RELEASE/spring-boot-loader-tools-1.5.6.RELEASE.pom (3.8 kB at 5.0 kB/s)
[INFO] Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.6.RELEASE/spring-boot-loader-tools-1.5.6.RELEASE.jar
[INFO] Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/springframework/boot/spring-boot-loader-tools/1.5.6.RELEASE/spring-boot-loader-tools-1.5.6.RELEASE.jar (151 kB at 107 kB/s) . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE) 2017-09-16 16:15:53.173 INFO 6900 --- [ main] c.p.springboot.SpringbootApplication : Starting SpringbootApplication on pc-PC with PID 6900 (D:\qinxiong\workspaceSpring\springboot\target\classes started by Administrator in D:\qinxiong\workspaceSpring\springboot)
2017-09-16 16:15:53.173 INFO 6900 --- [ main] c.p.springboot.SpringbootApplication : No active profile set, falling back to default profiles: default
2017-09-16 16:15:53.204 INFO 6900 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2689929b: startup date [Sat Sep 16 16:15:53 CST 2017]; root of context hierarchy
2017-09-16 16:15:54.668 INFO 6900 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-16 16:15:54.683 INFO 6900 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-09-16 16:15:54.683 INFO 6900 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-09-16 16:15:54.762 INFO 6900 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-09-16 16:15:54.762 INFO 6900 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1558 ms
2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-16 16:15:55.168 INFO 6900 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2689929b: startup date [Sat Sep 16 16:15:53 CST 2017]; root of context hierarchy
2017-09-16 16:15:55.230 INFO 6900 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.pitaya.springboot.HelloController.index()
2017-09-16 16:15:55.230 INFO 6900 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-09-16 16:15:55.230 INFO 6900 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-09-16 16:15:55.246 INFO 6900 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-16 16:15:55.246 INFO 6900 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-16 16:15:55.293 INFO 6900 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-16 16:15:55.404 INFO 6900 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-09-16 16:15:55.466 INFO 6900 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-09-16 16:15:55.466 INFO 6900 --- [ main] c.p.springboot.SpringbootApplication : Started SpringbootApplication in 2.637 seconds (JVM running for 8.001)

run目标的日志

二、实战

  默认情况下,应用程序直接从Maven JVM执行。 如果您需要在分叉进程(maven进程fork出来的子进程)中运行,可以使用'fork'选项。 如果指定了'jvmArguments'或'agent'选项,或者如果存在devtools,应用程序也会在分叉进程执行。

  如果需要指定一些JVM参数(即用于调试目的),可以使用jvmArguments参数,请看下面实例:

 <project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<configuration>
<jvmArguments>
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
</jvmArguments>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

插件使用jvmArguments

  请注意,由于您指定了一些JVM参数,所以该应用是自动在分叉进程中运行。这些参数也可以在命令行中指定,具体如下:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

命令行指定jvm参数

  当我们程序中设置了多个profile的yml文件,比如application.yml,application-dev.yml,application-test.yml,application-prd.yml。我们可以通过设置profiles参数来指定应用程序的active的profile。下面的例子演示指定要application.yml和application-test.yml两个文件。具体如下:

<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<configuration>
<profiles>
<profile>test</profile>
</profiles>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

插件使用profile

  如果要多加一个profile,只要多添加<profile>prd</profile>即可。

  同样的,要启动的profiles也可以通过命令行来指定,需要使用多个profile时,用逗号隔开,具体如下:

 mvn spring-boot:run -Drun.profiles=test,prd

命令行指定profile

喜欢请微信扫描下面二维码,关注我公众号--“精修Java”,做一些实战项目中的问题和解决方案分享。

Spring Boot Maven Plugin(二):run目标的更多相关文章

  1. Spring Boot Maven Plugin(一):repackage目标

    简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...

  2. Spring Boot Maven Plugin打包异常及三种解决方法:Unable to find main class

    [背景]spring-boot项目,打包成可执行jar,项目内有两个带有main方法的类并且都使用了@SpringBootApplication注解(或者另一种情形:你有两个main方法并且所在类都没 ...

  3. Spring Boot的Maven插件Spring Boot Maven plugin详解

    Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...

  4. spring boot高性能实现二维码扫码登录(上)——单服务器版

    前言 目前网页的主流登录方式是通过手机扫码二维码登录.我看了网上很多关于扫码登录博客后,发现基本思路大致是:打开网页,生成uuid,然后长连接请求后端并等待登录认证相应结果,而后端每个几百毫秒会循环查 ...

  5. spring boot高性能实现二维码扫码登录(中)——Redis版

    前言 本打算用CountDownLatch来实现,但有个问题我没有考虑,就是当用户APP没有扫二维码的时候,线程会阻塞5分钟,这反而造成性能的下降.好吧,现在回归传统方式:前端ajax每隔1秒或2秒发 ...

  6. spring boot高性能实现二维码扫码登录(下)——订阅与发布机制版

     前言 基于之前两篇(<spring boot高性能实现二维码扫码登录(上)——单服务器版>和<spring boot高性能实现二维码扫码登录(中)——Redis版>)的基础, ...

  7. 微服务下 Spring Boot Maven 工程依赖关系管理

    单体 Spring Boot Maven 工程 最基本的 pom.xml 包含工程信息.Spring Boot 父工程.属性配置.依赖包.构建插件 <?xml version="1.0 ...

  8. Spring Boot 2.X(二):集成 MyBatis 数据层开发

    MyBatis 简介 概述 MyBatis 是一款优秀的持久层框架,支持定制化 SQL.存储过程以及高级映射.它采用面向对象编程的方式对数据库进行 CRUD 的操作,使程序中对关系数据库的操作更方便简 ...

  9. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

随机推荐

  1. 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(二)示例

    一.搭建RTSP服务器 要想测试RTSP客户端,没有服务端怎么行呢?然而,有时候条件有限,手头并没有独立的RTSP服务器拿来用,那么我们不妨自己撘一个. 以下有2种方便的做法可供选择: 第一种:使用v ...

  2. Python 中的重点来了 : 迭代器 生成器

    可迭代的对象:带有__iter__方法的,就是迭代器.字符串,列表,元祖,字典,集合,文件,都是可迭代的 可迭代的对象使用了__iter__方法的,就是迭代器,带有__next__,__iter__方 ...

  3. Andrew Ng机器学习课程笔记--week8(K-means&PCA)

    Unsupervised Learning 本周我们讲学习非监督学习算法,会学习到如下概念 聚类(clustering) PCA(Principal Componets Analysis主成分分析), ...

  4. C# 逆变与协变

    该文章中使用了较多的 委托delegate和Lambda表达式,如果你并不熟悉这些,请查看我的文章<委托与匿名委托>.<匿名委托与Lambda表达式>以便帮你建立完整的知识体系 ...

  5. PyQt4 初试牛刀二

    一.最小话托盘后,调用showNormal()后窗口不刷新,解决办法如下: 重写showNormal 方法,调用父类方法后,repaint窗体 def showNormal(self):     su ...

  6. web中转发、重定向等问题的路径

    web中常用路径,转发,重定向,form表单action的路径 路径的写法: a.绝对路径写法:ServeltContext都必须用绝对路径."/" b.相对路径:其他情况都可以使 ...

  7. 进程管理之system

    system定义 #include<stdlib.h> int system(const char *command); 首先要知道,system函数是c库中的函数,而不是系统调用.其实s ...

  8. Redis[三] @Hash 哈希

    Redis的哈希值是字符串字段和字符串值之间的映射,所以他们是表示对象的完美数据类型 在Redis中的哈希值,可存储超过400十亿键值对. redis 提供了2套操纵 一种是批量 一种是非批量 假设需 ...

  9. 一台电脑 一起跑python2 python3

    我习惯使用python2.7,命令都是使用的python和pip,这时候装了python3.4,首先到python3下修改python.exe,pythonw.exe为python3.exe,pyth ...

  10. Tornado-数据库(torndb包)

    1.torndb数据库简介     在Tornado3.0版本以前提供tornado.database模块用来操作MySQL数据库,而从3.0版本开始,此模块就被独立出来,作为torndb包单独提供. ...