前言
在之前的項目中,一個項目被不同的package劃分,util負責各種工具類,exception負責異常處理還有mapper等完成各自的內容,項目需要發佈時,通過mvn package產生一個jar包或war包進行全量發佈。若util包中的一個類出現問題,則需要再次進行全量發佈(直接替換class文件這個粗暴的方式就不說了)。通過閱讀幾個開源項目代碼,發現了一個比較不錯的包結構設計方式:將原本的單體項目以maven聚合工程的方式展開,每個子模塊完成特定的工作,降低耦合性。例如:新建子工程commo就可以將dto、vo、exception和util單獨劃分爲一個模塊,在需要時引入依賴即可。
聚合工程打包部署的兩種方式
打包爲可執行的Jar包
使用maven assembly自定義打包
通過assembly插件可以將項目文檔、依賴、配置文件及其他文件打包爲zip、tar.gz等格式的文件,這裏推薦用zip。更多關於此插件的描述點這裏
- 我認爲插件最強大的功能是可以自定義打包格式,通過讀取XML配置文件完成文件管理,依賴管理等。
代碼
項目結構是這樣的
- POM文件添加插件
<?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>com.lhc</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.lhc</groupId>
<artifactId>serviceproducer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>serviceproducer</name>
<description>服務提供者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Nacos Discovery Starter-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<!--引用子模塊-->
<dependency>
<groupId>com.lhc</groupId>
<artifactId>commo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<!--項目本地打包:執行打包命令只有只有一個Jar包,只包含Java代碼-->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<!--配置配置文件-->
<configuration>
<excludes>
<exclude>*.yml</exclude>
<exclude>*.properties</exclude>
<exclude>*/**.xml</exclude>
<exclude>mapper</exclude>
<exclude>*.xml</exclude>
<exclude>*.txt</exclude>
</excludes>
<archive>
<manifest>
<!-- 主函數的入口 -->
<mainClass>com.lhc.serviceproducer.ServiceproducerApplication</mainClass>
<!-- 打包時 MANIFEST.MF文件不記錄的時間戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>./</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>../conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!--項目全局打包:包含所有的依賴-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- assembly.xml定義了項目的打包格式
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- assembly id -->
<id>build</id>
<!--打包後文件的格式,目前支持zip、tar、tar.gz (or tgz)、tar.bz2 (or tbz2)、tar.snappy、tar.xz (or txz)、jar、dir、war,支持指定多種格式-->
<formats>
<format>tar.gz</format>
<format>zip</format>
</formats>
<!--在最終存檔中包含基本目錄-->
<includeBaseDirectory>true</includeBaseDirectory>
<!--置生成的程序集檔案的基本目錄。-->
<baseDirectory>${project.artifactId}</baseDirectory>
<!--最終生成bin文件夾,用於存放啓動腳本-->
<fileSets>
<fileSet>
<!--路徑-->
<directory>${basedir}/src/main/assembly/bin</directory>
<!--文件夾名稱-->
<outputDirectory>bin</outputDirectory>
<!-- linux文件權限 -->
<fileMode>0755</fileMode>
</fileSet>
<!--最終生成conf文件夾,用於存放配置文件等-->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<includes>
<!--這裏更具自己的項目設置,支持通配符-->
<include>application.properties</include>
<include>logback.xml</include>
<include>banner.txt</include>
</includes>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<!--最終生成lib文件夾,用於存放項目所需的Jar包-->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>${project.artifactId}.jar</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<!-- 打包時跳過測試與-DskipTests效果類似-->
<excludes>
<exclude>junit:junit</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
官網提供了非常豐富的配置,這裏只使用了部分,多去官網瞭解吧
-
執行mvn package命令,得到文件,上傳發布即可
-
在服務器上解壓文件得到三個目錄
-
運行啓動腳本即可
最後
通過這種打包方式,可以進行快速發佈,修改配置文件接口啓動。若依賴的commo模塊需要修改,修改之後替換lib包中的文件即可
原文地址
若有錯誤,歡迎指教,謝謝