打包演示项目

以下部署方式均基于maven构建的springboot项目,项目名称springboot-helloworld,开发工具为IntelliJ IDEA,以下检查为idea。

 项目目录结构

代码

 ServletInitializer类:该类用于已tomcat方式启动springboot

package com.lnjecit.springboothelloworld;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringbootHelloworldApplication.class);
    }

}

HelloWorldController类:

package com.lnjecit.springboothelloworld.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @create 2018-04-16 16:06
 **/
@RequestMapping("/helloWorld")
@RestController
public class HelloWorldController {

    @RequestMapping("/sayHello")
    public String sayHello(@RequestParam("name") String name) {
        return "Hi, " + name;
    }
}

以war包方式部署

步骤:

第一步:将项目打包方式修改为war

 

第二步:在idea中Terminal控制台执行maven打包命令

mvn clean package -Dmaven.test.skip=true

命令执行完毕后,在target目录下生成了war包,如下图:

 第三步:使用xftp将war包上传到服务器/usr/local/apache-tomcat-8.5.30/webapps目录下

先将该目录下的ROOT目录删除,然后将springboot-helloworld-0.0.1-SNAPSHOT.war重命名为ROOT.war

 第四步:启动tomcat

 第五步:测试

在浏览器中输入:http://ip:8080/helloWorld/sayHello?name=李白 ip改为自己服务器的ip

到这里就部署成功了。

但是这样每次以war包方式打包,war包比较大,我们解压解压之后,可以看到文件总共13.7M,lib包也是13.7M,而classes文件夹只有2.69kb

可以看到lib包的大小占到了90%以上,实际代码容量非常小,每次都重新打war包,实际上是比较麻烦的,也没有必要。所以在部署实际项目的时候,如果决定要以war包方式部署项目,我通常在pom依赖没有改变的时候,只会将项目target目录下的classes目录覆盖服务器上的classes目录即可,如下图:

这样可以大幅度的提升项目部署速度,尤其是在网速比较慢的时候。

 只有当pom依赖有改动,才重新打一个完整的war包部署到服务器上。

以jar包方式部署

完整jar包

第一步:将项目打包方式修改为jar

第二步:在idea中Terminal控制台执行maven打包命令

mvn clean package -Dmaven.test.skip=true

命令执行完毕后,在target目录下生成了jar包,如下图:

 第三步:使用xftp将jar包上传到服务器/usr/local/springboot-helloworld目录下

第四步:运行jar包

先切换到jar包所在目录:

cd /usr/local/springboot-helloworld/

运行jar:

java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar

出现如下信息表示启动成功:

第五步:测试

在浏览器中输入:http://ip:8080/helloWorld/sayHello?name=李白 ip改为自己服务器的ip

 

到这里就部署成功了。

缺点:

1、打包后,配置文件和依赖包都在jar包内部,配置文件无法修改。而实际项目中,开发环境的配置与服务器环境配置并不完全一致,例如数据库信息,日志信息保存路径等,想要修改配置文件也无法修改

2、项目依赖包一般不会有变化,但是每次打包都会把依赖包打包到jar包内

3、控制台页面一关闭,springboot服务也关闭

Jar包、配置文件、依赖包分开打包

由于每次以jar包方式部署会有上面说的缺点,为了解决这些问题,可以将jar包、配置文件、依赖包分开打包。分开打包后,有如下好处:

1、可以实现当依赖包有变化时,才重新打包依赖包

2、配置文件也可以修改

3、由于依赖包和jar包分离,可以减少jar文件大小。jar文件减少,可以缩短上传到服务器的时间

 第一步:pom文件修改如下:

<?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.lnjecit</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>*.properties</exclude>
                        <exclude>*.yml</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <!-- 为依赖包添加路径, 这些路径会写在MANIFEST文件的Class-Path-->
                            <mainClass>com.lnjecit.springboothelloworld.SpringbootHelloworldApplication</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                        <manifestEntries>
                            <!--Class-Path下添加配置文件的路径 -->
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-xmls</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/config</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources</directory>
                                    <includes>
                                        <include>*.properties</include>
                                        <include>*.yml</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

第二步:在idea中Terminal控制台执行maven打包命令

mvn clean package -Dmaven.test.skip=true

命令执行完毕后,在target目录下生成了jar包、config、lib三部分,如下图:

运行完成后,有可能会出现如下情况:

https://img-blog.csdnimg.cn/20191203182806533.png
其实:前者是完整架包,可以直接服务器运行;后者才是依赖配置代码分开的jar包。

 第三步:使用xftp将jar包上传到服务器/usr/local/springboot-helloworld/deploy目录下

第四步:运行jar包

先切换到jar包所在目录:

cd /usr/local/springboot-helloworld/deploy/

springboot默认启动端口为8080,为了测试分开打包后可以修改配置文件,在application.properties文件中修改启动端口为8081

运行jar:

java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar

 在控制台可以看到启动端口变成了8081,如下图:

测试:

在浏览器中输入:http://ip:8081/helloWorld/sayHello?name=阿飞 ip改为自己服务器的ip

demo下载地址:https://github.com/linj6/springboot-learn/tree/master/springboot-helloworld

以jar包方式启动还有一个问题,当springboot服务启动端口关闭,服务也停止了。后面介绍将springboot设置为后台服务,启动窗口关闭后,服务依然运行。

Jar包运行

普通运行
java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar
指定端口运行
java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar --server.port=8082
后台运行

方法一:

nohup java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar &

启动后再当前目录生成一个nohup.out文件

 

查看启动后的服务:

ps -ef | grep java

关闭服务:

kill -9 3730

方法二: 

nohup java -jar demo.jar & > log.file 2>&1 &

springboot项目部署方式介绍完了,个人推荐使用将jar、配置文件、依赖包分开打包的方式来部署项目。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐