【问题标题】:Is there a way to distinguish fat jar from thin jar?有没有办法区分胖罐子和薄罐子?
【发布时间】:2020-07-12 11:38:31
【问题描述】:

我想避免在类路径中有胖罐子。 有没有办法检测某个 jar 文件是否是从多个 jar 文件编译而来的(除了查看它的大小)?

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

    这是不可能的。唯一的方法是查看是否包含库的依赖项。假设您有一个类 a/b/Foo 的库,并且它依赖于具有类 c/d/Baz 的外部库(也有一个类)bar.jar。 现在,如果 jar 中有两个类(a/b/Foo 和 c/d/Baz),我会说这是一个胖 jar。

    【讨论】:

      【解决方案2】:

      如果你使用下面的maven插件

      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      

      你可以使用包含 descriptorRefs 的配置来告诉 maven 这个配置应该构建什么类型的程序集。

      示例:

          <configuration>
              <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
          </configuration> 
      

      jar-with-dependencies 是 maven 的预制描述符 告诉 maven 构建具有与 FatJar 类似术语的依赖项的 jar

      然后只需启动 mvn clean package

      对于瘦 Jar,我没有解决方案,但如果你有一个 Spring Boot 项目 这个回购可以做到。 https://mvnrepository.com/artifact/org.springframework.boot.experimental/spring-boot-thin-layout/1.0.25.RELEASE

      也许两者的混合可以帮助你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-25
        • 2021-08-10
        • 2017-05-20
        • 1970-01-01
        • 2018-02-20
        • 1970-01-01
        相关资源
        最近更新 更多