如何阻止Spring Boot构建“胖罐"?具有未使用的依赖项 [英] How to stop Spring Boot from building a "fat jar" with dependencies that are not used

查看:64
本文介绍了如何阻止Spring Boot构建“胖罐"?具有未使用的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Spring Boot实现微服务,它很好地构建了一个带有所有必需依赖项的胖罐".

I am implementing a microservice with Spring Boot and it is really nice that it builds a "fat jar" with all the required dependencies packaged.

但是,它包装的是我什至不使用的依赖关系,例如:我正在使用Cassandra数据库,但是由于spring-starter依赖关系,它仍然将MongoDB依赖关系封装在100多个其他 ,从而生成一个 24 MB的jar文件

But, it is packaging dependencies I don't even use, for example: I am using a Cassandra database, but it still packages MongoDB depencies among 100+ others because of the spring-starter dependency, resulting in a 24 MB jar file!

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

Am I doing something wrong here? Is it possible to tell Maven to exclude all the dependencies that are not actually used?

编辑:我错了,它只是包括必要的依赖项,请忽略此问题.

EDIT: I was wrong, it is only including the necessary dependencies, disregard this question.

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

推荐答案

我怀疑spring-data-cassandra(或其他)依赖项的父级涵盖了所有其他类型的db,因此它们被拔出了.您可以使用<exclusion>标记

I suspect the spring-data-cassandra (or other) dependency has a parent that covers all sorts of other dbs and they got pulled. You can probably exclude those you know not needed using <exclusion> tag

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

这篇关于如何阻止Spring Boot构建“胖罐"?具有未使用的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆