SpringBoot系列:部署

环境

  • Spring Boot 2.0.4.RELEASE
  • JDK 1.8.0_181
  • Maven 3.5.4
  • IntelliJ IDEA 2018.1.6

jar包方式

添加插件

Spring Boot项目要创建一个可执行jar,我们需要将spring-boot-maven-plugin添加到我们的pom.xml中。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

执行打包


$ mvn package

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.itxiaoer:demo-spring-boot >--------------------
[INFO] Building demo-spring-boot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ demo-spring-boot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo-spring-boot ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ demo-spring-boot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/liuyukuai/Documents/develop/demo-spring-cloud/demo-spring-boot/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ demo-spring-boot ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ demo-spring-boot ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ demo-spring-boot ---
[INFO] Building jar: /Users/liuyukuai/Documents/develop/demo-spring-cloud/demo-spring-boot/target/demo-spring-boot-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ demo-spring-boot ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.457 s
[INFO] Finished at: 2018-08-31T10:50:46+08:00
[INFO] ------------------------------------------------------------------------


启动

打包成功后会在target目录下发现生成一个叫demo-spring-boot-0.0.1-SNAPSHOT.jar的jar包,我们可以将这个jar上传到服务器,然后启动


    java -jar demo-spring-boot-0.0.1-SNAPSHOT.jar 

war包方式

  • 修改pom.xml打包形式
<packaging>war</packaging>
  • 去掉Spring Boot内置的tomcat
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--开发使用-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

  • 设置war名称

<build>
    <finalName>demo-spring-boot-war</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

  • 执行mvn package,新生产的war包在target目录下
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.itxiaoer:demo-spring-boot-war >------------------
[INFO] Building demo-spring-boot-war 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ demo-spring-boot-war ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ demo-spring-boot-war ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ demo-spring-boot-war ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/liuyukuai/Documents/develop/demo-spring-cloud/demo-spring-boot-war/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ demo-spring-boot-war ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ demo-spring-boot-war ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ demo-spring-boot-war ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo-spring-boot-war] in [/Users/liuyukuai/Documents/develop/demo-spring-cloud/demo-spring-boot-war/target/demo-spring-boot-war]
[INFO] Processing war project
[INFO] Webapp assembled in [125 msecs]
[INFO] Building war: /Users/liuyukuai/Documents/develop/demo-spring-cloud/demo-spring-boot-war/target/demo-spring-boot-war.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ demo-spring-boot-war ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.859 s
[INFO] Finished at: 2018-08-31T12:35:16+08:00

  • 上传war到服务器就可以部署了

完整代码:https://github.com/liuyukuai/demo-spring-cloud/tree/master/demo-spring-boot-war