部署springboot项目,jar包和war的方式目前我用过,两者各有优缺点。
比如jar包部署,命令比较简单,但是如果想修改一个配置或者换个jar,需要重新打包部署。
war包部署的话,需要依赖外置tomcat,工作量也很大。
这里推荐使用一个maven插件,appassembler-maven-plugin,打包后会将配置文件、jar包分离,并且还提供windows/linux一键部署的脚本。
一、配置方法
在pom.xml里设置build如下代码,为了方便新人学习,这里创建了一个springboot-demo项目,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>-->
<!-- </plugins>-->
<!-- </build>-->
<build>
<!-- 加载资源文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<!-- jar发布时跳过此模块 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- 打包生成启动脚本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<!--lib下直接存放jar,没有路径文件夹(如com/apache),如果没有这个选项则放在lib下的jar包会被com/apache类似的多层文件夹包裹起来 -->
<repositoryLayout>flat</repositoryLayout>
<configurationDirectory>conf</configurationDirectory>
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<!--生成的项目的目录位置,这里的client是项目的名称,你可以根据你的需要自己随便命名 -->
<assembleDirectory>${project.build.directory}/springboot-demo</assembleDirectory>
<!--java启动参数 -->
<!-- <extraJvmArguments>-Xms1024m -Dfile.encoding=utf-8</extraJvmArguments> -->
<binFileExtensions>
<unix>.sh</unix>
</binFileExtensions>
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<repositoryName>lib</repositoryName>
<programs>
<program>
<!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台 -->
<mainClass>com.example.demo.DemoApplication</mainClass>
<!-- 生成的脚本文件的名称,比如start.sh,你也可以根据你的需要命名成其他名字 -->
<name>start</name>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、效果图
执行mvn clean install 打包
在target下找到springboot-demo,里面有conf,lib和bin三个目录,分别对应resources配置文件、jar包和启动脚本(windows系统直接双击start.bat, linux需要执行 nohup sh start.sh &)
2021年09月30日 22:22:00
牛
2021年09月21日 01:17:02
谢谢分享,2021加油,我的博客,欢迎回访