maven快速生成SpringBoot打包文件的方法步驟
最近在部署基于SpringBoot開發(fā)的項目時,由于微服務(wù)較多,本地工程編譯后只得出一個JAR包,部署起來實在不方便,因此總想著怎么偷偷懶,執(zhí)行一次命令編譯出整個部署的文件。先說結(jié)果,最后期望打包的目錄如下:

各個目錄或文件說明如下:
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,對打包輸出關(guān)鍵看build節(jié)點,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等方式部署,方便后期生成鏡像,可以刪除這個節(jié)點。如果該目錄下有子目錄中的相關(guān)的文件也不想打包到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>
這個配置會將你的應(yīng)用程序打包成一個可執(zhí)行的 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>
拷貝調(diào)試腳本到根目錄下
<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到根目錄下。
當(dāng)需要編譯出部署包時,執(zhí)行maven clean package -f pom1.xml即可。
到此這篇關(guān)于maven快速生成打包文件的方法步驟的文章就介紹到這了,更多相關(guān)maven 生成打包文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java FileDescriptor總結(jié)_動力節(jié)點Java學(xué)院整理
FileDescriptor 是“文件描述符”??梢员挥脕肀硎鹃_放文件、開放套接字等。接下來通過本文給大家分享Java FileDescriptor總結(jié),感興趣的朋友一起學(xué)習(xí)吧2017-05-05
Java命令設(shè)計模式優(yōu)雅解耦命令和執(zhí)行提高代碼可維護(hù)性
本文介紹了Java命令設(shè)計模式,它將命令請求封裝成對象,以達(dá)到解耦命令請求和執(zhí)行者的目的,從而提高代碼可維護(hù)性。本文詳細(xì)闡述了該模式的設(shè)計原則、實現(xiàn)方法和優(yōu)缺點,并提供了實際應(yīng)用場景和代碼示例,幫助讀者深入理解和應(yīng)用該模式2023-04-04
在Spring Boot中如何使用數(shù)據(jù)緩存
本篇文章主要介紹了在Spring Boot中如何使用數(shù)據(jù)緩存,具有一定的參考價值,有興趣的可以了解一下。2017-04-04
Java IO中字節(jié)流復(fù)制圖片實現(xiàn)代碼
這篇文章主要介紹了Java IO中字節(jié)流復(fù)制圖片實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04

