【问题标题】:How to stop Spring Boot from building a "fat jar" with dependencies that are not used如何阻止 Spring Boot 构建具有未使用依赖项的“胖 jar”
【发布时间】:2014-10-12 22:11:05
【问题描述】:

我正在使用 Spring Boot 实现一个微服务,它构建了一个“胖 jar”,并打包了所有必需的依赖项。

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

我在这里做错了吗? 是否可以告诉 Maven 排除所有没有实际使用的依赖项?

编辑:我错了,它只包含必要的依赖项,忽略这个问题。

这里是 pom.xml

<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

【问题讨论】:

  • 请发布mvn dependency:tree的输出。
  • @chrylis,用mvn dependency:tree 的输出更新了问题。
  • 你怎么知道它包含了 MongoDB 依赖?我没有看到它们在任何地方列出。
  • @chrylis 你是对的,它们不包括在内。我在pom.xml 的Effective POM 选项卡中看到了它,并认为它们被包括在内,这是我的错误,他们没有。

标签: java spring maven spring-boot


【解决方案1】:

我怀疑 spring-data-cassandra (或其他)依赖项有一个父项,它涵盖了所有其他类型的数据库,它们被拉了。您可能可以使用&lt;exclusion&gt; 标签排除那些您知道不需要的人

<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>

【讨论】:

    猜你喜欢
    • 2016-02-08
    • 2014-05-29
    • 2018-08-23
    • 2021-03-18
    • 2020-04-08
    • 2019-12-23
    • 2019-07-26
    • 2020-03-13
    • 2011-08-18
    相关资源
    最近更新 更多