Maven发布轻量二方包

本文介绍了如何使用Maven将公共模块打包成fat jar,以解决依赖管理问题。通过修改pom.xml和使用assembly插件,可以将公共模块的依赖整合进jar包,避免客户端应用手动引入额外依赖。同时,为了减少重复依赖和潜在的包冲突,还展示了如何优化打包过程,排除通用依赖,创建更轻量化的二方包。

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

森伯416

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值