简介
Spring Boot Maven Plugin插件提供spring boot在maven中的支持。允许你打包可运行的jar包或war包。
插件提供了几个maven目标和Spring Boot 应用一起工作。总的有:
- spring-boot:repackage 请阅读:Spring cloud的Maven插件(一):repackage目标
- spring-boot:run 请阅读: Spring cloud的Maven插件(二):run目标
- spring-boot:start and spring-boot:stop
- spring-boot:build-info
run: 这个目标和直接java -jar 来启动基本相同,唯一不同的是java -jar需要打包后才能跑,而run目标不需要。这个当程序编译打包需要比较长时间时,用run目标来跑可以节约时间。
一、简单使用
Spring Boot Maven Plugin 包括一个 run 目标,该目标被用于从命令行启动你的应用程序。比如:
1 mvn spring-boot:run
eclipse用法如下:
启动后的日志:
1 [INFO] Scanning for projects... 2 [INFO] 3 [INFO] ------------------------------------------------------------------------ 4 [INFO] Building springboot 0.0.1-SNAPSHOT 5 [INFO] ------------------------------------------------------------------------ 6 [INFO] 7 [INFO] >>> spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) > test-compile @ springboot >>> 8 [INFO] 9 [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot --- 10 [INFO] Using 'UTF-8' encoding to copy filtered resources. 11 [INFO] Copying 1 resource 12 [INFO] Copying 0 resource 13 [INFO] 14 [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot --- 15 [INFO] Nothing to compile - all classes are up to date 16 [INFO] 17 [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot --- 18 [INFO] Using 'UTF-8' encoding to copy filtered resources. 19 [INFO] Copying 0 resource 20 [INFO] 21 [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot --- 22 [INFO] Nothing to compile - all classes are up to date 23 [INFO] 24 [INFO] <<< spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) < test-compile @ springboot <<< 25 [INFO] 26 [INFO] 27 [INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) @ springboot --- 28 [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 29 [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) 30 [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 31 [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) 32 33 . ____ _ __ _ _ 34 /\ / ___'_ __ _ _(_)_ __ __ _ 35 ( ( )\___ | '_ | '_| | '_ / _` | 36 \/ ___)| |_)| | | | | || (_| | ) ) ) ) 37 ' |____| .__|_| |_|_| |_\__, | / / / / 38 =========|_|==============|___/=/_/_/_/ 39 :: Spring Boot :: (v1.5.6.RELEASE) 40 41 2017-09-16 16:15:53.173 INFO 6900 --- [ main] c.p.springboot.SpringbootApplication : Starting SpringbootApplication on pc-PC with PID 6900 (D:qinxiongworkspaceSpringspringboot argetclasses started by Administrator in D:qinxiongworkspaceSpringspringboot) 42 2017-09-16 16:15:53.173 INFO 6900 --- [ main] c.p.springboot.SpringbootApplication : No active profile set, falling back to default profiles: default 43 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 44 2017-09-16 16:15:54.668 INFO 6900 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 45 2017-09-16 16:15:54.683 INFO 6900 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 46 2017-09-16 16:15:54.683 INFO 6900 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 47 2017-09-16 16:15:54.762 INFO 6900 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 48 2017-09-16 16:15:54.762 INFO 6900 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1558 ms 49 2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 50 2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 51 2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 52 2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 53 2017-09-16 16:15:54.918 INFO 6900 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 54 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 55 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() 56 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) 57 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) 58 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] 59 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] 60 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] 61 2017-09-16 16:15:55.404 INFO 6900 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 62 2017-09-16 16:15:55.466 INFO 6900 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 63 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)
二、实战
默认情况下,应用程序直接从Maven JVM执行。 如果您需要在分叉进程(maven进程fork出来的子进程)中运行,可以使用'fork'选项。 如果指定了'jvmArguments'或'agent'选项,或者如果存在devtools,应用程序也会在分叉进程执行。
如果需要指定一些JVM参数(即用于调试目的),可以使用jvmArguments参数,请看下面实例:
1 <project> 2 ... 3 <build> 4 ... 5 <plugins> 6 ... 7 <plugin> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-maven-plugin</artifactId> 10 <version>1.5.7.RELEASE</version> 11 <configuration> 12 <jvmArguments> 13 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 14 </jvmArguments> 15 </configuration> 16 ... 17 </plugin> 18 ... 19 </plugins> 20 ... 21 </build> 22 ... 23 </project>
请注意,由于您指定了一些JVM参数,所以该应用是自动在分叉进程中运行。这些参数也可以在命令行中指定,具体如下:
mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
当我们程序中设置了多个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>prd</profile>即可。
同样的,要启动的profiles也可以通过命令行来指定,需要使用多个profile时,用逗号隔开,具体如下:
1 mvn spring-boot:run -Drun.profiles=test,prd
喜欢请微信扫描下面二维码,关注我公众号--“精修Java”,做一些实战项目中的问题和解决方案分享。