java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > maven 生成SpringBoot打包文件

maven快速生成SpringBoot打包文件的方法步骤

作者:angushine

本文主要介绍了使用Maven快速生成SpringBoot项目打包文件的方法,包括如何生成可执行的JAR文件,如何将配置文件、运行脚本、调试脚本、证书文件等拷贝到指定目录,及如何编译出部署包,这种方法能大大方便微服务的部署,提高部署效率

最近在部署基于SpringBoot开发的项目时,由于微服务较多,本地工程编译后只得出一个JAR包,部署起来实在不方便,因此总想着怎么偷偷懒,执行一次命令编译出整个部署的文件。先说结果,最后期望打包的目录如下:

在这里插入图片描述

各个目录或文件说明如下:

bin:包含程序启动和停止的两个脚本,后台运行脚本
cert:程序运行过程中使用的一些证书
config:各种配置文件
logs:运行日志
kafka-roma.jar:主程序
run.bat:windows下运行脚本
run.sh:linux下运行脚本,非后台脚本

先上pom1.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>
    <groupId>org.example</groupId>
    <artifactId>kafka-roma</artifactId>
    <version>1.0</version>
    <name>kafka-roma</name>
    <description>kafka-roma</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <maven.test.skip>true</maven.test.skip>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.52</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>*.properties</exclude>
                    <exclude>*.yml</exclude>
                    <exclude>*.xml</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>org.example.roma.KafkaRomaApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <includes>
                                        <include>*.properties</include>
                                        <include>*.yml</include>
                                        <include>**/*.xml</include>
                                    </includes>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/roma/config/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-resources1</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/bin</directory>
                                    <includes>
                                        <include>start.sh</include>
                                        <include>stop.sh</include>
                                    </includes>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/roma/bin/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-resources2</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/bin</directory>
                                    <includes>
                                        <include>run.sh</include>
                                        <include>run.bat</include>
                                    </includes>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/roma/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-resources3</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>cert</directory>
                                    <includes>
                                        <include>*.jks</include>
                                        <include>*.crt</include>
                                    </includes>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/roma/cert/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-resources4</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}</directory>
                                    <includes>
                                        <include>*.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/roma/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

下面逐一说明打包的各个plugin,对打包输出关键看build节点,finalName确定最后输出jar包的名字,这里采用project.artifactId,因此输出的文件名为kafka-roma.jar。

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>*.properties</exclude>
            <exclude>*.yml</exclude>
            <exclude>*.xml</exclude>
        </excludes>
    </resource>
</resources>

这里主要控制不要把src/main/resources目录下properties、xml、yml等配置文件打包到JAR中,由于后面考虑方便修改配置文件,因此没有将配置文件打包到JAR中,如果采用DOCKER等方式部署,方便后期生成镜像,可以删除这个节点。如果该目录下有子目录中的相关的文件也不想打包到JAR中,修改如下

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.yml</exclude>
            <exclude>**/*.xml</exclude>
        </excludes>
    </resource>
</resources>

**表示多级子目录,*表示一级子目录

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring-boot.version}</version>
    <configuration>
        <mainClass>org.example.roma.KafkaRomaApplication</mainClass>
    </configuration>
    <executions>
        <execution>
            <id>repackage</id>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这个配置会将你的应用程序打包成一个可执行的 JAR 文件,包含了所有依赖项。mainClass为JAR包中的启动类。
maven-resources-plugin主要拷贝各类文件

<execution>
    <id>copy-resources</id>
    <phase>package</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.properties</include>
                    <include>*.yml</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <outputDirectory>${project.build.directory}/roma/config/</outputDirectory>
    </configuration>
</execution>

拷贝配置文件到config目录下

<execution>
    <id>copy-resources1</id>
    <phase>package</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <resources>
            <resource>
                <directory>src/main/bin</directory>
                <includes>
                    <include>start.sh</include>
                    <include>stop.sh</include>
                </includes>
            </resource>
        </resources>
        <outputDirectory>${project.build.directory}/roma/bin/</outputDirectory>
    </configuration>
</execution>

拷贝运行脚本到bin目录下

<execution>
    <id>copy-resources2</id>
    <phase>package</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <resources>
            <resource>
                <directory>src/main/bin</directory>
                <includes>
                    <include>run.sh</include>
                    <include>run.bat</include>
                </includes>
            </resource>
        </resources>
        <outputDirectory>${project.build.directory}/roma/</outputDirectory>
    </configuration>
</execution>

拷贝调试脚本到根目录下

<execution>
    <id>copy-resources3</id>
    <phase>package</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <resources>
            <resource>
                <directory>cert</directory>
                <includes>
                    <include>*.jks</include>
                    <include>*.crt</include>
                </includes>
            </resource>
        </resources>
        <outputDirectory>${project.build.directory}/roma/cert/</outputDirectory>
    </configuration>
</execution>

拷贝证书文件到根目录下

<execution>
    <id>copy-resources4</id>
    <phase>package</phase>
    <goals>
        <goal>copy-resources</goal>
    </goals>
    <configuration>
        <resources>
            <resource>
                <directory>${project.build.directory}</directory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </resource>
        </resources>
        <outputDirectory>${project.build.directory}/roma/</outputDirectory>
    </configuration>
</execution>

拷贝JAR到根目录下。
当需要编译出部署包时,执行maven clean package -f pom1.xml即可。

到此这篇关于maven快速生成打包文件的方法步骤的文章就介绍到这了,更多相关maven 生成打包文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文