美文网首页
spring boot 实战 / mvn spring-boot

spring boot 实战 / mvn spring-boot

作者: qwfys | 来源:发表于2018-04-26 18:31 被阅读81次

    概述

      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项目。

    SPRING INITIALIZR

      点击按钮"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找到了想要的答案,具体如下图所示。

    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 实战 / mvn spring-boot

          本文链接:https://www.haomeiwen.com/subject/tapmlftx.html