当前位置: 首页 > 知识库问答 >
问题:

如何阻止Spring Boot用未使用的依赖项构建“胖罐子”

轩辕季同
2023-03-14

我正在用Spring Boot实现一个微服务,它构建了一个打包了所有所需依赖项的“胖罐子”,这真的很好。

但是,我甚至不使用打包依赖项,例如:我使用的是Cassandra数据库, <罢工> 但由于spring-starter依赖,它仍然将MongoDB依赖包在100多个其他依赖包中 ,生成一个24 MB的jar文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- Cassandra -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-cassandra</artifactId>
        <version>1.1.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

更新:下面是依赖项:树

[INFO] com.test.project:dataloader:jar:0.0.1
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.1.8.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.1.8.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.0.7.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-aop:jar:4.0.7.RELEASE:compile
[INFO] |  |        \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.1.8.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.1.8.RELEASE:compile
[INFO] |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.7:compile
[INFO] |  |  +- org.slf4j:log4j-over-slf4j:jar:1.7.7:compile
[INFO] |  |  \- ch.qos.logback:logback-classic:jar:1.1.2:compile
[INFO] |  |     \- ch.qos.logback:logback-core:jar:1.1.2:compile
[INFO] |  +- org.springframework:spring-core:jar:4.0.7.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.13:compile
[INFO] +- org.springframework.data:spring-data-cassandra:jar:1.1.0.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-cql:jar:1.0.4.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.0.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:4.0.7.RELEASE:compile
[INFO] |  |  +- org.apache.cassandra:cassandra-all:jar:2.0.6:compile
[INFO] |  |  |  +- org.xerial.snappy:snappy-java:jar:1.0.5:compile
[INFO] |  |  |  +- net.jpountz.lz4:lz4:jar:1.2.0:compile
[INFO] |  |  |  +- com.ning:compress-lzf:jar:0.8.4:compile
[INFO] |  |  |  +- commons-cli:commons-cli:jar:1.1:compile
[INFO] |  |  |  +- commons-codec:commons-codec:jar:1.2:compile
[INFO] |  |  |  +- org.apache.commons:commons-lang3:jar:3.1:compile
[INFO] |  |  |  +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.3:compile
[INFO] |  |  |  +- org.antlr:antlr:jar:3.2:compile
[INFO] |  |  |  |  \- org.antlr:antlr-runtime:jar:3.2:compile
[INFO] |  |  |  |     \- org.antlr:stringtemplate:jar:3.2:compile
[INFO] |  |  |  |        \- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  +- org.codehaus.jackson:jackson-core-asl:jar:1.9.2:compile
[INFO] |  |  |  +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] |  |  |  +- jline:jline:jar:1.0:compile
[INFO] |  |  |  +- com.googlecode.json-simple:json-simple:jar:1.1:compile
[INFO] |  |  |  +- com.github.stephenc.high-scale-lib:high-scale-lib:jar:1.1.2:compile
[INFO] |  |  |  +- edu.stanford.ppl:snaptree:jar:0.1:compile
[INFO] |  |  |  +- org.mindrot:jbcrypt:jar:0.3m:compile
[INFO] |  |  |  +- com.yammer.metrics:metrics-core:jar:2.2.0:compile
[INFO] |  |  |  +- com.addthis.metrics:reporter-config:jar:2.1.0:compile
[INFO] |  |  |  |  \- org.hibernate:hibernate-validator:jar:5.0.3.Final:compile
[INFO] |  |  |  |     +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  |  |     +- org.jboss.logging:jboss-logging:jar:3.1.1.GA:compile
[INFO] |  |  |  |     \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] |  |  |  +- com.thinkaurelius.thrift:thrift-server:jar:0.3.3:compile
[INFO] |  |  |  |  \- com.lmax:disruptor:jar:3.0.1:compile
[INFO] |  |  |  +- net.sf.supercsv:super-csv:jar:2.1.0:compile
[INFO] |  |  |  +- log4j:log4j:jar:1.2.17:compile
[INFO] |  |  |  +- org.apache.thrift:libthrift:jar:0.9.1:compile
[INFO] |  |  |  |  +- org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[INFO] |  |  |  |  \- org.apache.httpcomponents:httpcore:jar:4.2.4:compile
[INFO] |  |  |  +- org.apache.cassandra:cassandra-thrift:jar:2.0.6:compile
[INFO] |  |  |  +- com.github.stephenc:jamm:jar:0.2.5:compile
[INFO] |  |  |  \- io.netty:netty:jar:3.6.6.Final:compile
[INFO] |  |  +- com.google.guava:guava:jar:15.0:compile
[INFO] |  |  \- org.liquibase:liquibase-core:jar:3.0.8:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.0.7.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.8.4.RELEASE:compile
[INFO] |  +- com.datastax.cassandra:cassandra-driver-dse:jar:2.0.4:compile
[INFO] |  |  \- com.datastax.cassandra:cassandra-driver-core:jar:2.0.4:compile
[INFO] |  |     \- com.codahale.metrics:metrics-core:jar:3.0.2:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.7:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:1.1.8.RELEASE:test
[INFO]    +- junit:junit:jar:4.11:compile
[INFO]    +- org.mockito:mockito-core:jar:1.9.5:test
[INFO]    |  \- org.objenesis:objenesis:jar:1.0:test
[INFO]    +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO]    +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO]    \- org.springframework:spring-test:jar:4.0.7.RELEASE:test

共有1个答案

华鹭洋
2023-03-14

我怀疑spring-data-cassandra(或其他)依赖项有一个父项,该父项覆盖了所有其他DB,它们被拉了出来。您可能可以使用 标记排除那些您知道不需要的

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-cassandra</artifactId>
    <version>1.1.0.RELEASE</version>
    <exclusions>
      <exclusion>
         <groupId>..</groupId>
         <artifactId>..</artifactId>
      </exclusion>
    </exclusions>
</dependency>
 类似资料:
  • 我正在使用Gradle构建并打包一个非常简单的Spring启动应用程序(99%的静态内容)到带有嵌入式tomcat的jar中。 我尝试创建该jar,起初结果86k,没有启动,因为它缺少一些Spring Boot类。我得出结论,我制作的这个jar不包含应用程序的任何依赖项,既然我确实想要一个完全独立的jar,我应该做更多的研究。 这时,我发现建议将{configurations.compile.co

  • fat jar工作得很好,我只想使用一个裁剪过的JRE。 我尝试了以下方法: 我得到的结果是: 当您试图避免这种情况时,您可能会尝试解压缩fat jar并将它的lib指向jdeps,但随后您会遇到另一个bug--https://bugs.openjdk.java.net/browse/jdk-8207162,它阻止您将多版本jar(如log4j)与jdeps一起使用。最后,我尝试了每一个依赖项来找

  • 我在Gradle 4.6中有一个简单的项目,我想为它制作一个可执行的JAR。我尝试过,,,插件,但它们都没有添加我声明为的依赖项(我没有任何依赖项)。它与一起工作,例如用于插件,但我希望有依赖项。

  • 我希望基础映像 下载依赖项并仅在依赖项更改时重新生成,而第二个映像 将在代码更改时重新生成。但是,在 上,两个 maven 命令都会下载所有依赖项。我可能误解了堆叠的工作原理或在哪里复制什么。 我尝试过的:将第一个容器中的所有内容显式复制到第二个容器中:< code>COPY / /和各种更具体的< code>COPY目标,如< code>.m2,从maven基本映像构建第二个容器,如第一个容器,

  • 如标题所示,我想知道如何修改以创建一个唯一的,其中包含所有依赖项(包括kotlin lib)。 我在Groovy中找到了这个示例: 但我不知道我怎么能用kotlin写,除了:

  • 问题内容: 我在Gradle 4.6中有一个简单的项目,并希望制作一个可执行的jar。我试过,,,插件,但他们都没有增加我的依赖性声明(我没有任何的)。它与例如插件一起工作,但我希望有依赖关系。 非常感谢你! 问题答案: 您可以使用以下代码。 请务必使用包含的完全合格的类名称替换。 这将打包运行时依赖项。如果需要更多信息,请检查文档。