首页 > 技术文章 > CentOs部署boot项目(jar方式)

ihuqi 2021-08-26 11:12 原文

相关依赖安装

1.jdk安装

参考 https://www.cnblogs.com/ihuqi/p/15188353.html

2.mysql安装

参考 https://www.cnblogs.com/ihuqi/p/15160786.html

 

jar方式部署

1.项目打包

2.上传jar

将jar用xftp上传到指定目录,比如/usr/local/project/system/目录下

 3.运行jar

先切换到jar包所在目录

cd /usr/local/springboot-helloworld/

运行jar

java -jar thinvent-modules-system.jar

  -- 运行成功检测方式

  •   控制台会打印和windows一样的东西
  •   浏览器访问项目地址

  -- 缺点

  •   打包后,配置文件和依赖包都在jar包内部,配置文件无法修改。而实际项目中,开发环境的配置与服务器环境配置并不完全一致,例如数据库信息,日志信息保存路径等,想要修改配置文件也无法修改
  •   项目依赖包一般不会有变化,但是每次打包都会把依赖包打包到jar包内
  •   控制台页面一关闭,springboot服务也关闭

 

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

1.背景

由于上述打包方式的缺陷,可以将jar包、配置文件、依赖包分开打包。分开打包后,有如下好处:

  • 可以实现当依赖包有变化时,才重新打包依赖包
  • 配置文件也可以修改
  • 由于依赖包和jar包分离,可以减少jar文件大小。jar文件减少,可以缩短上传到服务器的时间

2.修改pom文件(主要关注build下)

<?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">
<build> <plugins> <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>

3.在idea中Terminal控制台执行maven打包命令

mvn clean package -Dmaven.test.skip=true

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

4.部署jar

略,后续部署方式和jar方式部署一致

 

Jar包运行

1.普通运行

java -jar thinvent-module.system.jar 

2.指定端口运行

java -jar thinvent-module.system.jar --server.port=8888

3.后台运行

nohup java -jar thinvent-module.systemjar 

4.查看启动后的服务

ps -ef | grep java

 5.关闭服务

kill -9 3730

 

参考出处

https://blog.csdn.net/xinzi11243094/article/details/81240413

 

推荐阅读