【Maven】SpringBoot项目使用maven-assembly-plugin插件多环境打包

SpringBoot项目使用maven-assembly-plugin插件多环境打包

1.创建SpringBoot项目并在pom.xml文件中添加maven-assembly-plugin配置

Java 复制代码
<!--  多环境配置  -->
    <profiles>
        <!--  开发环境  -->
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <!-- 默认激活的环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--  生产环境  -->
        <profile>
            <id>prod</id>
            <properties>
                <profileActive>prod</profileActive>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

    <build>
        <!-- 打包后的jar包名称 -->
        <finalName>spring-boot-demo</finalName>
        <!-- 打包配置 -->
        <plugins>
            <!-- maven 打包时跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- &lt;!&ndash;  指定该Main Class为全局的唯一入口 &ndash;&gt;-->
                    <!-- <mainClass>com.xhs.ToolsApp</mainClass> -->
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <!--可以把依赖的包都打包到生成的Jar包中-->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 个性化打包 -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <!-- 打包文件名字不包含 assembly.xml 中 id -->
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <!-- 资源配置 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.yml</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

2.创建 在src/main/assembly目录下创建assembly.xml文件

java 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- 打包文件名的标识符,用来做后缀-->
    <id>make-assembly</id>
    <!-- 打包的类型,如果有N个,将会打N个类型的包 -->
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <!-- 压缩包下是否生成和项目名相同的根目录 -->
    <includeBaseDirectory>true</includeBaseDirectory>
    <!-- 用来设置一组文件在打包时的属性。-->
    <fileSets>
        <!-- 0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;-->
        <!-- 0644->即用户具有读写权限,组用户和其它用户具有只读权限;-->
        <!-- 将src/bin目录下的jar启动脚本输出到打包后的目录中 -->
        <fileSet>
            <directory>${basedir}/src/main/bin</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>**.sh</include>
                <include>**.bat</include>
            </includes>
        </fileSet>

        <!-- 把项目的配置文件,打包进压缩文件的config目录 -->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <fileMode>0644</fileMode>
            <includes>
                <include>*.properties</include>
                <include>*.yml</include>
                <include>*.xml</include>
            </includes>
        </fileSet>

        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

3.在src/main/bin创建在linux环境启动jar包的脚本

java 复制代码
#!/bin/bash

# jar包名称
JAR_FILE="spring-boot-demo.jar"
# pid 名称
PID_FILE="spring-boot-demo.pid"

start() {
    if [ -f "$PID_FILE" ]; then
        echo "应用程序已在运行,PID: $(cat $PID_FILE) .........."
    else
    	# 后台启动jar包,并将启动日志输出到log.log文件中
        nohup java -jar -Dloader.path=.,3rd-li $JAR_FILE >> log.log 2>&1 &
        echo $! > $PID_FILE
        echo "应用程序已成功启动,PID: $(cat $PID_FILE) .........."
    fi
}

stop() {
    if [ -f "$PID_FILE" ]; then
        kill -9 $(cat $PID_FILE)
        rm $PID_FILE
        echo "应用程序已成功停止.........."
    else
        echo "应用程序未运行.........."
    fi
}

restart() {
    echo "正在重启.........."
    stop
    start
    echo "重启成功.........."
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        echo "使用: $0 {start|stop|restart} 命令"
        exit 1
        ;;
esac

4.配置application.yml文件

java 复制代码
#application.yml
spring:
  profiles:
    active: @profileActive@
java 复制代码
#application-dev.yml
server:
  port: 8001

spring:
  application:
    name: spring-boot-demo
java 复制代码
#application-prod.yml
server:
  port: 8001

spring:
  application:
    name: spring-boot-demo

5.启动项目

6.打包

java 复制代码
mvn clean package -P prod


7.打包后的目录结构

8.上传到linux服务器并启动jar包

java 复制代码
# 解压tar包
tar -zxf spring-boot-demo.tar.gz
java 复制代码
#启动jar包
sh app.sh start
java 复制代码
#查看日志
tail -f log.log 


9.调用接口测试


10.源码地址
https://gitee.com/xhs101/spring-boot-demo

相关推荐
workflower2 小时前
单元测试-例子
java·开发语言·算法·django·个人开发·结对编程
YuanlongWang2 小时前
C# 基础——装箱和拆箱
java·开发语言·c#
摇滚侠2 小时前
Spring Boot 3零基础教程,WEB 开发 静态资源默认配置 笔记27
spring boot·笔记·后端
b78gb2 小时前
电商秒杀系统设计 Java+MySQL实现高并发库存管理与订单处理
java·开发语言·mysql
wb043072014 小时前
性能优化实战:基于方法执行监控与AI调用链分析
java·人工智能·spring boot·语言模型·性能优化
天若有情6735 小时前
Java Swing 实战:从零打造经典黄金矿工游戏
java·后端·游戏·黄金矿工·swin
lichong9515 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
lichong9515 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++
@yanyu6666 小时前
Tomcat安装与HTML响应实战
java·tomcat·html
Chen-Edward7 小时前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring