简单记录一下建立一个Spring Boot的Web项目,打包并发布到Linux服务器的过程
1.建立Spring Boot项目
项目使用Maven构建,Idea集成开发,通过xshell连接远程服务器及利用sftp上传jar包
pom.xml文件内容需要注意:
1.1<parent>节点需要存在
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
</parent>
1.2<start-class>节点指向main函数的类
不能缺省,否则部署到服务器,启动程序时有Could not find the main class错误
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<start-class>com.cmos.WebApplication</start-class>
</properties>
1.3.当然,maven打包的插件也不能少
<finalName>节点内容,最终是jar包的名称
<build>
<finalName>signature-test-1.0.0-SNAPSHOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
完整的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.signature</groupId>
<artifactId>signature-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
</parent>
<name>signature-test</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<start-class>com.WebApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.45</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.16.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>signature-test-1.0.0-SNAPSHOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.Spring Boot项目开发
启动项,如下所示
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
之后按照业务逻辑书写控制层、接口、业务层、dao层代码。简单的项目结构如图1所示:
图 1
3.Maven 打包并上传linux服务器
3.1Maven打包
maven 打包命令,如下:
clean package -Dmaven.test.skip=true
在Idea中,执行maven命令图下图所示
图 2
由于使用Maven构建项目,打包后的jar文件,存在 "项目名称/target" 目录下,示例如下图所示:
3.2上传并部署到服务器
利用xshell连接到远程服务器,并将打包好的jar包通过xftp上传到linux服务器
执行的命令如下:
(1)如果java定义了jar包的main class入口,则使用如下命令
$ java -jar xxx.jar
(2)如果Java没有定义jar的main class入口,则使用如下命令
$ java -cp xxx.jar com.example.MainClass
看到如下图示,就算是完成一个小任务了
3.3测试
开发的项目是Spring Boot的web项目,部署后需要测试一下。用PostMan进行一次简单测试,如下图示:
3.4 其他
如果,修改了业务逻辑,需要重新上传代码。此时需要把原来的进程杀死,之后再启动新的进程。过程,如下所示:
Linux查询进程和结束进程
1. ps -ef |grep redis
这条命令的意思是显示有关redis有关的进程
ps:将某个进程显示出来
-A 显示所有程序。
-e 此参数的效果和指定"A"参数相同。
-f 显示UID, PPIP , C与 STIME栏位。
grep命令是查找
中间的|是管道命令 是指ps命令与grep同时执行
2. kill[参数][进程号]
kill -9 4394
kill就是给某个进程id发送了一个信号。默认发送的信号是SIGTERM,而kill -9发送的信号是SIGKILL,即exit。exit信号不会被系统阻塞,所以kill -9能顺利杀掉进程。当然你也可以使用kill发送其他信号给进程。