You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven特定OS与架构构建:寻找abiFilters等效配置缩小JAR包体积

Maven指定目标架构裁剪JAR包方案

你遇到的JAR体积过大问题基本都是引入了包含多架构native二进制的依赖(如JavaCPP绑定库、Netty native组件、OpenCV等),默认会拉取全平台的二进制文件导致体积膨胀,可通过以下步骤配置裁剪:

1. 全局过滤非目标架构资源

使用maven-shade-plugin在打包阶段直接排除不需要的架构相关文件,适配所有带native资源的依赖:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.5.1</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <filters>
              <filter>
                <artifact>*:*</artifact>
                <excludes>
                  <!-- 排除所有非目标架构的资源,可根据实际情况增删 -->
                  <exclude>**/linux-x86_64/**</exclude>
                  <exclude>**/windows*/**</exclude>
                  <exclude>**/macosx-arm64/**</exclude>
                  <exclude>**/android*/**</exclude>
                  <exclude>**/ios*/**</exclude>
                </excludes>
              </filter>
            </filters>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

2. 特定依赖直接指定目标架构

针对常用的native依赖,可通过属性配置直接只拉取目标架构的依赖包,从依赖引入阶段就裁剪冗余:

JavaCPP系列依赖(如OpenCV、AI框架绑定等)

在pom的properties节点添加配置:

<properties>
  <javacpp.platform>macosx-x86_64,linux-arm64</javacpp.platform>
</properties>

Netty native传输依赖

<properties>
  <netty-transport-native-epoll.classifier>linux-aarch_64</netty-transport-native-epoll.classifier>
  <netty-transport-native-kqueue.classifier>osx-x86_64</netty-transport-native-kqueue.classifier>
</properties>

3. 分架构单独生成JAR(可选)

如果需要为两个架构分别生成独立的最小体积JAR,可使用Maven Profile做差异化配置:

<profiles>
  <!-- macosx-x86_64 架构配置 -->
  <profile>
    <id>mac-x86</id>
    <properties>
      <target.platform>macosx-x86_64</target.platform>
      <javacpp.platform>macosx-x86_64</javacpp.platform>
    </properties>
  </profile>
  <!-- linux-arm64 架构配置 -->
  <profile>
    <id>linux-arm</id>
    <properties>
      <target.platform>linux-arm64</target.platform>
      <javacpp.platform>linux-arm64</javacpp.platform>
    </properties>
  </profile>
</profiles>

打包时指定对应Profile即可:

  • 生成macosx-x86_64版本:mvn clean package -P mac-x86
  • 生成linux-arm64版本:mvn clean package -P linux-arm

4. 结果验证

打包完成后执行以下命令确认仅保留目标架构资源:
jar tf 你的包名.jar | grep -E "(linux|macosx|windows|android|ios)"
确认输出结果中仅包含macosx-x86_64和linux-arm64相关的路径即可。

内容的提问来源于stack exchange,提问作者kesari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 03:36:04