gpt4 book ai didi

java - 如何阻止 Spring Boot 构建具有未使用的依赖项的 "fat jar"

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:27 26 4
gpt4 key购买 nike

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

但是,它是我什至不使用的打包依赖项,例如:我正在使用 Cassandra 数据库, 但由于 spring-starter 依赖项,它仍然将 MongoDB 依赖项打包在 100 多个其他数据库中 ,从而产生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

最佳答案

我怀疑 spring-data-cassandra (或其他)依赖项的父项涵盖了所有其他数据库,并且它们被拉取了。您可以使用 <exclusion> 排除那些您知道不需要的内容。标签

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

关于java - 如何阻止 Spring Boot 构建具有未使用的依赖项的 "fat jar",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com