Maven 中maven-assembly-plugin插件的使用笔记 SpringBoot环境
首先创建一个多模块的SpringBoot项目
项目结构
父pom的内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.shanghai.abcd</groupId> <artifactId>content-service</artifactId> <packaging>pom</packaging> <version> 1.0 . 1 -SNAPSHOT</version> <modules> <module>web-service</module> <module>config-service</module> <module>common-service</module> <module>biz-service</module> <module>dal-service</module> <module>deploy-service</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 4 .RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <junit.version> 4.9 </junit.version> <java.version> 1.8 </java.version> <source.encoding>UTF- 8 </source.encoding> </properties> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 1.5 . 4 .RELEASE</version> <scope>test</scope> </dependency> </dependencies> </project> |
子模块web模块的pom文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId>content-service</artifactId> <groupId>com.shanghai.abcd</groupId> <version> 1.0 . 1 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>web-service</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.shanghai.abcd</groupId> <artifactId>biz-service</artifactId> <version> 1.0 . 1 -SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>content</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>${source.encoding}</encoding> </configuration> </plugin> <!--spring boot 的编译插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
web模块中main启动文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.shanghai.abcd.content; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.ComponentScan; /** * @Auther: * @Date: 2018/5/25 15:26 * @Description: PACKAGE_NAME */ @SpringBootApplication @ConfigurationProperties (value = "classpath:application.properties" ) @ComponentScan ( "com.shanghai.abcd.content" ) public class ContentApplication { public static void main(String[] args) { SpringApplication.run(ContentApplication. class , args); } } |
重点是在deploy-service模块,项目的部署文件都是在该模块维护的,文件结构如下:
deploy-service子模块文件结构
deploy模块的pom.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId>content-service</artifactId> <groupId>com.shanghai.abcd</groupId> <version> 1.0 . 1 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>deploy-service</artifactId> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version> 2.4 </version> <configuration> <finalName>content</finalName> <appendAssemblyId> false </appendAssemblyId> <!-- 如果只想有finalName,不需要连接release.xml中的id --> <tarLongFileMode>posix</tarLongFileMode> <!-- 解决tar大小的限制问题 --> <descriptors> <descriptor>src/main/assembly/release.xml</descriptor> </descriptors> <outputDirectory>../output</outputDirectory> <attach> false </attach> </configuration> <executions> <execution> <phase> package </phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
release.xml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <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" > <id>content</id> <formats> <format>tar.gz</format> </formats> <includeBaseDirectory> false </includeBaseDirectory> <fileSets> <!-- 程序运行的jar包 --> <fileSet> <directory>../web-service/target/</directory> <!--需要打包的项目文件目录 --> <outputDirectory>bin</outputDirectory> <!-- 打包后输出的路径,生成bin目录 --> <includes> <include>** /*.jar</include> </includes> <fileMode>0755</fileMode> <!--文件执行权限--> </fileSet> <!-- 程序运行的启动脚本 --> <fileSet> <directory>target/classes/bin</directory> <!-- 指定要包含的目录,必须是目录 --> <outputDirectory>bin</outputDirectory> <!-- 打包的文件解压后放在该目录下 --> <includes> <include>**/ *.sh</include> </includes> <fileMode> 0755 </fileMode> <!--文件执行权限--> </fileSet> <fileSet> <!-- 配置文件 --> <directory>../config-service/target/classes</directory> <!-- 指定要包含的目录,必须是目录 --> <outputDirectory>conf</outputDirectory> <!-- 指定当前要包含的目录的目的地 --> <includes> <include>**/*.properties</include> </includes> <fileMode> 0755 </fileMode> <!--文件执行权限--> </fileSet> </fileSets> <dependencySets> <!-- 项目的依赖包 --> <dependencySet> <outputDirectory>lib</outputDirectory><!-- 依赖jar包放置目录--> <useProjectArtifact> true </useProjectArtifact> <!-- 当前项目构件是否包含在这个依赖集合里 --> </dependencySet> </dependencySets> </assembly> |
配置文件是统一放在config-service模块的,该模块的目录结构如下:
config-service模块的目录结构
config模块的pom.xml文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <parent> <artifactId>content-service</artifactId> <groupId>com.shanghai.abcd</groupId> <version> 1.0 . 1 -SNAPSHOT</version> </parent> <modelVersion> 4.0 . 0 </modelVersion> <artifactId>config-service</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional> true </optional> </dependency> </dependencies> <profiles> <profile> <id>dev</id> <properties> <environment>dev</environment> </properties> <activation> <activeByDefault> true </activeByDefault> <!-- 默认是dev环境 --> </activation> </profile> <profile> <id>test</id> <properties> <environment>test</environment> </properties> </profile> <profile> <id>prod</id> <properties> <environment>prod</environment> </properties> </profile> </profiles> <build> <resources> <resource> <filtering> true </filtering> <directory>src/main/resources</directory> <excludes> <exclude>application-dev.properties</exclude> <exclude>application-test.properties</exclude> <exclude>application-prod.properties</exclude> <exclude>application.properties</exclude> </excludes> </resource> <resource> <filtering> true </filtering> <!-- 为了能让profiles中的内容能让resources中的文件使用到,需要resources插件将filtering设置为 true --> <directory>src/main/resources</directory> <includes> <include>application-${environment}.properties</include> <include>application.properties</include> </includes> </resource> </resources> </build> </project> |
最后通过 mvn clean package -Pdev命令来打包项目,在项目根目录下生成一个output文件
cd output 后解压该项目:
tar -zxvf content.tar.gz
得到如下目录:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?