【问题标题】:Compile subset of dependencies into my jar file将依赖项的子集编译到我的 jar 文件中
【发布时间】:2015-09-10 23:18:58
【问题描述】:

我有一个具有多个 Maven 依赖项的项目,并且希望最小化我编译的 JAR 的大小。现在看来,IntelliJ 正在将所有外部依赖源文件编译到我的 JAR 中,尽管我只使用了它们功能的一小部分。

我只想包含我的模块直接使用的文件。

【问题讨论】:

    标签: java maven intellij-idea jar


    【解决方案1】:

    您可以将依赖项声明为可选:

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

    来源:

    Maven - Optional Dependencies and Dependency Exclusions

    Maven - Dependency Scope

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      相关资源
      最近更新 更多