spring boot 實戰 / mvn spring-boot:run 參數詳解
Spring boot項目通常情況下有如下幾種啟動方式:
- 通過主類啟動。
- 通過spring-boot的maven插件spring-boot-maven-plugin方式啟動。
- 通過可執行jar/war包方式啟動。
- 通過Servlet容器啟動,如Tomcat、Jetty等。
今天我們來聊一下spring boot的maven插件spring-boot-maven-plugin啟動過程中的profiles問題。首先,我們前往網站SPRING INITIALIZR,參照下圖創建一個名稱為demo的spring boot項目。
點擊按鈕"Generate Poject alt + Enter",將得到一個名稱為demo.zip的文件,解壓該文件,將得到一個名為demo的目錄。
我們接下來,將以這個demo項目為例展開講解。
項目結構
為了演示我們後面的實驗,我們對項目demo做一些調整,調整後的目錄結構如下如示。
lwk@qwfys ~/Public/project/io/spring/sts/ws/demo $ ll total 40 drwxrwxr-x 4 lwk lwk 4096 Apr 18 01:11 ./ drwxrwxr-x 10 lwk lwk 4096 Apr 18 00:36 ../ -rw-rw-r-- 1 lwk lwk 268 Apr 17 23:56 .gitignore drwxrwxr-x 3 lwk lwk 4096 Apr 17 23:56 .mvn/ -rwxrwxr-x 1 lwk lwk 6468 Apr 17 23:56 mvnw* -rw-rw-r-- 1 lwk lwk 4994 Apr 17 23:56 mvnw.cmd -rw-rw-r-- 1 lwk lwk 1567 Apr 18 00:59 pom.xml drwxrwxr-x 4 lwk lwk 4096 Apr 17 23:56 src/ lwk@qwfys ~/Public/project/io/spring/sts/ws/demo $ tree . ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── example │ │ └── demo │ │ └── DemoApplication.java │ └── resources │ ├── application-dev.yml │ ├── application-prod.yml │ ├── application-test.yml │ ├── application.yml │ ├── static │ └── templates └── test └── java └── com └── example └── demo └── DemoApplicationTests.java 14 directories, 9 files lwk@qwfys ~/Public/project/io/spring/sts/ws/demo $
這裏簡單說一下,我的電腦上安裝的是Linux mint 18.3系統,所以在目錄展示上可能與大家使用的Windows 7/8/10等風格上不一致,這個沒有關系的,希望大家不要感覺到奇怪。
這裏我們將環境粗略劃分為開發環境(dev)、測試環境(test)、生產環境(prod),主要是為了演示spring boot的spring-boot maven插件的profiles與spring boot中spring.profiles的用對應關系及相關用法。
接下來我們先簡要瀏覽一下上述目錄中的幾個關鍵文件的內容。
pom.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.name}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
項目啟動主類DemoApplication.java內容如下:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.yml內容如下:
spring:
profiles:
active: dev
開發環境配置文件application-dev.yml內容如下:
server:
port: 8080
生產環境配置文件application-prod.yml內容如下:
server:
port: 9999
測試環境配置文件application-test.yml內容如下:
server:
port: 7829
測試主類DemoApplicationTests.java內容如下:
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
}
}
啟動參數
默認啟動參數
結合上面的文件內容,我們可以看出,整個系統已經將開發環境設置為了默認開發環境,所以,默認啟動參數是針對開發環境的。
lwk@qwfys ~/Public/project/io/spring/sts/ws/demo $ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) > test-compile @ demo >>>
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ demo ---
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/lwk/Public/project/io/spring/sts/ws/demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ demo ---
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/lwk/Public/project/io/spring/sts/ws/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/lwk/Public/project/io/spring/sts/ws/demo/target/test-classes
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) < test-compile @ demo <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) @ demo ---
[INFO] Attaching agents: []
01:14:10.853 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
01:14:10.859 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
01:14:10.859 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/home/lwk/Public/project/io/spring/sts/ws/demo/target/classes/]
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-04-18 01:14:11.292 INFO 12906 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on qwfys with PID 12906 (/home/lwk/Public/project/io/spring/sts/ws/demo/target/classes started by lwk in /home/lwk/Public/project/io/spring/sts/ws/demo)
2018-04-18 01:14:11.292 INFO 12906 --- [ restartedMain] com.example.demo.DemoApplication : The following profiles are active: dev
2018-04-18 01:14:11.350 INFO 12906 --- [ restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4e7f290c: startup date [Wed Apr 18 01:14:11 CST 2018]; root of context hierarchy
2018-04-18 01:14:12.755 INFO 12906 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-04-18 01:14:12.794 INFO 12906 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-04-18 01:14:12.795 INFO 12906 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-18 01:14:12.805 INFO 12906 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-04-18 01:14:12.873 INFO 12906 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-04-18 01:14:12.873 INFO 12906 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1528 ms
2018-04-18 01:14:13.001 INFO 12906 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-04-18 01:14:13.005 INFO 12906 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter‘ to: [/*]
2018-04-18 01:14:13.005 INFO 12906 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2018-04-18 01:14:13.005 INFO 12906 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2018-04-18 01:14:13.005 INFO 12906 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter‘ to: [/*]
2018-04-18 01:14:13.130 INFO 12906 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:14:13.350 INFO 12906 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4e7f290c: startup date [Wed Apr 18 01:14:11 CST 2018]; root of context hierarchy
2018-04-18 01:14:13.433 INFO 12906 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-18 01:14:13.434 INFO 12906 --- [ restartedMain] 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-18 01:14:13.459 INFO 12906 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:14:13.460 INFO 12906 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:14:13.631 INFO 12906 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-04-18 01:14:13.673 INFO 12906 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-18 01:14:13.799 INFO 12906 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘
2018-04-18 01:14:13.808 INFO 12906 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 2.932 seconds (JVM running for 3.428)
那麽在如何命令行添加諸如test、prod之類的profiles參數呢,為此,我試了好久,後來終於在Spring Boot Maven Plugin找到了想要的答案,具體如下圖所示。
經過測試,發現完全滿足要求,具體介紹如下:
測試環境啟動參數
lwk@qwfys ~/Public/project/io/spring/sts/ws/demo $ mvn spring-boot:run -Dspring-boot.run.profiles=test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) > test-compile @ demo >>>
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ demo ---
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ demo ---
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/lwk/Public/project/io/spring/sts/ws/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) < test-compile @ demo <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) @ demo ---
[INFO] Attaching agents: []
01:47:28.228 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
01:47:28.231 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
01:47:28.231 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/home/lwk/Public/project/io/spring/sts/ws/demo/target/classes/]
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-04-18 01:47:28.547 INFO 14157 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on qwfys with PID 14157 (/home/lwk/Public/project/io/spring/sts/ws/demo/target/classes started by lwk in /home/lwk/Public/project/io/spring/sts/ws/demo)
2018-04-18 01:47:28.548 INFO 14157 --- [ restartedMain] com.example.demo.DemoApplication : The following profiles are active: test
2018-04-18 01:47:28.590 INFO 14157 --- [ restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@afeee95: startup date [Wed Apr 18 01:47:28 CST 2018]; root of context hierarchy
2018-04-18 01:47:29.550 INFO 14157 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 7829 (http)
2018-04-18 01:47:29.578 INFO 14157 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-04-18 01:47:29.579 INFO 14157 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-18 01:47:29.590 INFO 14157 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-04-18 01:47:29.644 INFO 14157 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-04-18 01:47:29.644 INFO 14157 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1059 ms
2018-04-18 01:47:29.741 INFO 14157 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-04-18 01:47:29.743 INFO 14157 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter‘ to: [/*]
2018-04-18 01:47:29.744 INFO 14157 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2018-04-18 01:47:29.744 INFO 14157 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2018-04-18 01:47:29.744 INFO 14157 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter‘ to: [/*]
2018-04-18 01:47:29.826 INFO 14157 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:47:29.967 INFO 14157 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@afeee95: startup date [Wed Apr 18 01:47:28 CST 2018]; root of context hierarchy
2018-04-18 01:47:30.029 INFO 14157 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-18 01:47:30.030 INFO 14157 --- [ restartedMain] 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-18 01:47:30.046 INFO 14157 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:47:30.046 INFO 14157 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:47:30.163 INFO 14157 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-04-18 01:47:30.186 INFO 14157 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-18 01:47:30.287 INFO 14157 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 7829 (http) with context path ‘‘
2018-04-18 01:47:30.290 INFO 14157 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 2.047 seconds (JVM running for 2.401)
生產環境啟動參數
lwk@qwfys ~/Public/project/io/spring/sts/ws/demo $ mvn spring-boot:run -Dspring-boot.run.profiles=prod
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) > test-compile @ demo >>>
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ demo ---
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ demo ---
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/lwk/Public/project/io/spring/sts/ws/demo/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) < test-compile @ demo <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:run (default-cli) @ demo ---
[INFO] Attaching agents: []
01:48:08.788 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
01:48:08.791 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]
01:48:08.791 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/home/lwk/Public/project/io/spring/sts/ws/demo/target/classes/]
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-04-18 01:48:09.112 INFO 14280 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on qwfys with PID 14280 (/home/lwk/Public/project/io/spring/sts/ws/demo/target/classes started by lwk in /home/lwk/Public/project/io/spring/sts/ws/demo)
2018-04-18 01:48:09.113 INFO 14280 --- [ restartedMain] com.example.demo.DemoApplication : The following profiles are active: prod
2018-04-18 01:48:09.168 INFO 14280 --- [ restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@35a08e90: startup date [Wed Apr 18 01:48:09 CST 2018]; root of context hierarchy
2018-04-18 01:48:10.031 INFO 14280 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9999 (http)
2018-04-18 01:48:10.054 INFO 14280 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-04-18 01:48:10.054 INFO 14280 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-18 01:48:10.063 INFO 14280 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-04-18 01:48:10.132 INFO 14280 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-04-18 01:48:10.132 INFO 14280 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 967 ms
2018-04-18 01:48:10.248 INFO 14280 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-04-18 01:48:10.251 INFO 14280 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter‘ to: [/*]
2018-04-18 01:48:10.251 INFO 14280 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]
2018-04-18 01:48:10.251 INFO 14280 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*]
2018-04-18 01:48:10.252 INFO 14280 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter‘ to: [/*]
2018-04-18 01:48:10.349 INFO 14280 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:48:10.496 INFO 14280 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@35a08e90: startup date [Wed Apr 18 01:48:09 CST 2018]; root of context hierarchy
2018-04-18 01:48:10.564 INFO 14280 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-18 01:48:10.565 INFO 14280 --- [ restartedMain] 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-18 01:48:10.593 INFO 14280 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:48:10.594 INFO 14280 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-18 01:48:10.741 INFO 14280 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-04-18 01:48:10.769 INFO 14280 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-04-18 01:48:10.894 INFO 14280 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9999 (http) with context path ‘‘
2018-04-18 01:48:10.897 INFO 14280 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 2.097 seconds (JVM running for 2.377)
至此任務完成。
總結
從上面的實例我們可以看到,如果在項目中通過spring.profiles定義了多個環境:
application-xxx.yml
用maven插件spring-boot啟動時,可以用
mvn spring-boot:run -Dspring-boot.run.profiles=xxx
來啟動。
參考文獻
- Spring Boot Reference Guide
- Spring Boot Maven Plugin
spring boot 實戰 / mvn spring-boot:run 參數詳解