fat jar(打包发布自身依赖的jar包)
在公共项目开发了common模块,其中包含了发钉钉消息、时间处理等多种公共方法,现在想要发布到maven仓库给其他应用使用。
其他应用使用过程中发现,引入common模块的依赖后还需要单独引用common内部的其他依赖。
client如果要使用发消息模块的能力,需要引入如下依赖:
pom.xml:
<!-- common内部发消息的依赖 -->
<dependency>
<groupId>com.dingtalk.open</groupId>
<artifactId>dingtalk-openapi-sdk</artifactId>
<version>xxx</version>
</dependency>
<dependency>
<groupId>com.alibaba.logistics</groupId>
<artifactId>logistics-infra-common</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
这样使用存在两个问题:
1.client需要手动引入common内部使用的依赖,没有起到统一管理的效果。
2.common升级内部依赖client也要跟着升级,否则可能出现低版本使用高版本依赖的方法,比如:ObjectUtils.allNull() 方法就在 commons-lang3 低版本就没有。
那为什么不直接将common模块所需要依赖都打进去呢?
pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 包名称末尾不跟assemblyId -->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<!-- 打包源码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
assembly.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>common.jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<!-- 忽略自己产生的依赖包 -->
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
MAC解压:tar -zxvf xx.jar -C test1
依赖展示:

看看现在打包后的结果,确实把common所需要的依赖都打包进去了,client不用再单独引入,但是这样有一个问题,common与client的包有很多都是重复的,有可能发生包冲突等问题,对client端很不友好。
那能不能给common依赖瘦下身?排除掉常用的依赖,只包含特有的依赖。
pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<!-- 包名称末尾不跟assemblyId -->
<appendAssemblyId>false</appendAssemblyId>
<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.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
assembly:
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>common.jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<!-- 忽略自己产生的依赖包 -->
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>com.aliyun.tair:tairjedis-sdk-singlepath</include>
</includes>
<excludes>
<exclude>javax.annotation:javax.annotation-api</exclude>
<exclude>log4j:log4j</exclude>
<exclude>org.slf4j:slf4j-api</exclude>
<exclude>org.slf4j:jul-to-slf4j</exclude>
<exclude>ch.qos.logback:logback-classic</exclude>
<exclude>ch.qos.logback:logback-core</exclude>
<exclude>org.apache.logging.log4j:log4j-to-slf4j</exclude>
<exclude>org.apache.logging.log4j:log4j-api</exclude>
<exclude>org.projectlombok:lombok</exclude>
<exclude>org.springframework:spring-aop</exclude>
<exclude>org.springframework:spring-beans</exclude>
<exclude>org.springframework.boot:spring-boot</exclude>
<exclude>org.springframework.boot:spring-boot-autoconfigure</exclude>
<exclude>org.springframework.boot:spring-boot-starter</exclude>
<exclude>org.springframework.boot:spring-boot-starter-logging</exclude>
<exclude>org.springframework:spring-context</exclude>
<exclude>org.springframework:spring-core</exclude>
<exclude>org.springframework:spring-expression</exclude>
<exclude>org.springframework:spring-jcl</exclude>
<exclude>org.yaml:snakeyaml</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
依赖展示:

经过上述的打包优化后,打出的common包已经很轻量化了,client使用只需要引入common本身即可。
<dependency>
<groupId>com.alibaba.logistics</groupId>
<artifactId>logistics-infra-common</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
正常打包
<build>
<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>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
本文介绍了如何使用Maven将公共模块打包成fat jar,以解决依赖管理问题。通过修改pom.xml和使用assembly插件,可以将公共模块的依赖整合进jar包,避免客户端应用手动引入额外依赖。同时,为了减少重复依赖和潜在的包冲突,还展示了如何优化打包过程,排除通用依赖,创建更轻量化的二方包。

1306

被折叠的 条评论
为什么被折叠?



