您现在的位置是:首页 >技术杂谈 >maven 插件 assembly 打tar.gz包网站首页技术杂谈

maven 插件 assembly 打tar.gz包

摸鱼佬 2024-09-20 12:01:06
简介maven 插件 assembly 打tar.gz包


GitHub: link. 欢迎star

注意:本篇博客风格(不多比比就是撸代码!!!)

一、项目目录

在这里插入图片描述

二、pom文件

1. profiles

	<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
    </profiles>

2. plugins

		<plugins>
            <plugin>
                <groupId>org.cyclonedx</groupId>
                <artifactId>cyclonedx-maven-plugin</artifactId>
                <version>2.5.3</version>
            </plugin>
            <!--  配置assembly.xml文件路径  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <!--  项目启动jar包中排除依赖包  -->
                        <include>
                            <groupId>non-exists</groupId>
                            <artifactId>non-exists</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>
            <!--  自定义maven jar打包内容  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!--  项目启动类  -->
                            <mainClass>com.andon.springbootutil.SpringBootUtilApplication</mainClass>
                            <classpathPrefix>../lib</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- 复制项目的依赖包到指定目录  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
        </plugins>

3. resource

		<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>application-${profiles.active}.yml</include>
                    <include>mapperxml/*.xml</include>
                    <include>*.xml</include>
                    <include>*.properties</include>
                    <include>db/migration/*.sql</include>
                    <include>banner.txt</include>
                </includes>
            </resource>
        </resources>
        <finalName>springboot-util</finalName>

三、assembly.xml

<?xml version="1.0" encoding="UTF-8" ?>
<assembly>
    <id>release</id>
    <!--  打包类型  -->
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <!--    输出bin目录所有文件到根目录    -->
        <fileSet>
            <directory>${basedir}/src/bin</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>**.sh</include>
                <include>**.bat</include>
            </includes>
        </fileSet>
        <!--    输出配置文件到config目录    -->
        <fileSet>
            <directory>${basedir}/target/classes</directory>
            <outputDirectory>config</outputDirectory>
            <fileMode>0644</fileMode>
            <includes>
                <include>application.yml</include>
                <include>application-${profiles.active}.yml</include>
                <include>mapperxml/*.xml</include>
                <include>db/migration/*.sql</include>
                <include>*.xml</include>
                <include>*.properties</include>
                <include>banner.txt</include>
            </includes>
        </fileSet>
        <!--    第三方依赖打包到lib目录    -->
        <fileSet>
            <directory>${basedir}/target/lib</directory>
            <outputDirectory>lib</outputDirectory>
            <fileMode>0755</fileMode>
        </fileSet>
        <!--    将项目启动jar打包到boot目录    -->
        <fileSet>
            <directory>${basedir}/target</directory>
            <outputDirectory>boot</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>${project.build.finalName}.jar</include>
            </includes>
        </fileSet>
        <!--    打包根目录下的文件    -->
        <fileSet>
            <directory>${basedir}</directory>
            <includes>
                <include>**.md</include>
                <include>**.conf</include>
                <include>**.zip</include>
                <include>**.zip</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${basedir}/nginxconfig</directory>
            <outputDirectory>nginxconfig</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${basedir}/nginx</directory>
            <outputDirectory>nginx</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${basedir}/nginx/ssl</directory>
            <outputDirectory>nginx${file.separator}ssl</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>
    </fileSets>

</assembly>

四、application.yml

server:
  port: 8866
  servlet:
    context-path: /springboot-util
  forward-headers-strategy: native
spring:
  profiles:
    active: @profiles.active@

五、启动脚本

1. start.sh

#!/bin/sh
source /etc/profile

APPLICATION="springboot-util"
APPLICATION_JAR="${APPLICATION}.jar"

JAVA_OPT="-server -XX:NewRatio=2 -XX:SurvivorRatio=8 -Xms8g -Xmx16g"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"

JVM_REMOTE_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=7766"

nohup java ${JAVA_OPT} ${JVM_REMOTE_OPTS} -jar ./boot/${APPLICATION_JAR} > nohup.out 2>&1 &

2. stop.sh

#!/bin/sh

APPLICATION="springboot-util"
APPLICATION_JAR="${APPLICATION}.jar"

PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{print $2}')
kill -9 ${PID}

六、执行 mvn 打包命令

mvn clean package -DskipTests=true -P dev
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

七、tar.gz 包上传服务器并解压

在这里插入图片描述

八、执行 start.sh 启动脚本

在这里插入图片描述

九、访问 swagger

在这里插入图片描述

GitHub: link. 欢迎star

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。