【发布时间】:2015-09-10 23:18:58
【问题描述】:
我有一个具有多个 Maven 依赖项的项目,并且希望最小化我编译的 JAR 的大小。现在看来,IntelliJ 正在将所有外部依赖源文件编译到我的 JAR 中,尽管我只使用了它们功能的一小部分。
我只想包含我的模块直接使用的文件。
【问题讨论】:
标签: java maven intellij-idea jar
我有一个具有多个 Maven 依赖项的项目,并且希望最小化我编译的 JAR 的大小。现在看来,IntelliJ 正在将所有外部依赖源文件编译到我的 JAR 中,尽管我只使用了它们功能的一小部分。
我只想包含我的模块直接使用的文件。
【问题讨论】:
标签: java maven intellij-idea jar
您可以将依赖项声明为可选:
<dependencies>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>small-dependency</artifactId>
<version>1.0</version> <!-- Will be packaged in JAR -->
</dependency>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>really-big-dependency</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
</dependencies>
另一种方法是使用provided 范围。区别在于 provided 用于如果您知道依赖项将包含在将运行您的 JAR 的应用程序的类路径中(例如 Web 或 Java EE 容器):
<dependencies>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>small-dependency</artifactId>
<version>1.0</version> <!-- Will be packaged in JAR -->
</dependency>
<dependency>
<groupId>sample.dependency</groupId>
<artifactId>really-big-dependency</artifactId>
<version>1.0</version>
<scope>provided</scope> <!-- Will not be packaged in JAR, needs to be provided in classpath at runtime -->
</dependency>
</dependencies>
来源:
【讨论】: