【问题标题】:For which Maven dependency scopes is the dependency included in the compiled project?编译项目中包含哪些 Maven 依赖范围?
【发布时间】:2012-11-11 22:26:03
【问题描述】:

例如如果我将 BukkitApi jar 设置为依赖范围设置为提供、编译、系统、运行时或测试的 maven 项目的依赖项

bukkitAPI 将在哪些范围内包含在编译输出中?

【问题讨论】:

  • 什么编译输出?你说的是哪个阶段?例如,如果您将范围设置为 test 而没有别的,则测试编译的输出显然会使用依赖项。这并不意味着它将被“包含”在任何东西中,只要您没有在任何输出中明确包含依赖项(例如,package 阶段的输出)
  • 老实说,我不知道哪个阶段,我对 maven 完全陌生,IRC 频道上的某个人让我感到困惑,我怀疑他们错了,我用谷歌搜索无济于事,所以我问了 stackoverflow。

标签: java eclipse maven dependencies m2e


【解决方案1】:

这不是 Maven 的工作方式。依赖项只指定类路径(用于编译、运行时、测试)。但默认情况下,依赖项不包含在输出中。您将必须交付所有依赖项 jar(至少具有范围编译和运行时的那些)。

看看依赖插件。它提供了复制依赖项的目标。

要创建要装运的捆绑包,请查看组装插件(例如,创建 zip 文件)。它甚至提供了一种创建一体式罐子的方法,如果您想要的话。

【讨论】:

    【解决方案2】:

    短版:默认情况下,maven 输出(在默认的target 目录中)除了当前项目/模块的编译代码之外不包含任何内容。也就是说,没有任何依赖关系。

    Long(er) 版本:默认 jar 包装,没有自定义阶段配置。这是 maven 在 java 项目中的行为方式:

    1. compile 阶段:src/main/java/ 目录中的.java 文件被编译为.classes 目录中的.classes 文件。 compile 范围的依赖项会下载到您的本地存储库。
    2. package 阶段:与 1 相同,另外您将在 target 目录中获得一个 jar 文件
    3. install 阶段:与 2 相同,您将在本地存储库中获得一个 jar 文件。

    因此,默认情况下,依赖项中的.jar 文件不包含在任何内容中!

    那么,我如何在“输出”中包含依赖项,这些范围是什么意思?

    现在,例如,使用assembly 插件在package 阶段的输出中包含依赖项(请参阅Including dependencies in a jar with Maven),您通常会得到以下默认行为:

    • provided:不包括
    • compile(默认):包括
    • system:不包括
    • runtime:包括
    • test:不包括

    结帐this link 以供参考。

    编辑: 只需在guice 上使用不同范围值尝试这个 pom,您会看到当范围为 compile 和 @987654349 时,依赖项包含在 fake-1.0-SNAPSHOT-jar-with-dependencies.jar 中@(本例不需要任何源文件)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                          http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.linagora</groupId>
        <artifactId>fake</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>fake</name>
        <dependencies>
            <dependency>
                <groupId>com.google.inject</groupId>
                <artifactId>guice</artifactId>
                <version>2.0</version>
                <scope>compile</scope>
                <!-- system scope needs 'systemPath' attribute as well
                    <systemPath>/path/to/guice/guice-3.0.jar</systemPath>
                    <scope>system</scope>
                -->
                <!-- <scope>runtime</scope> -->
                <!-- <scope>test</scope> -->
                <!-- <scope>provided</scope> -->
    
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    【讨论】:

    • 人们声称 compile 不包括组装输出中的依赖项,以 [worldguard]{github.com/sk89q/worldguard/blob/master/src/main/assembly/…} 为例,它的 [pom]{github.com/sk89q/worldguard/blob/master/pom.xml} 是不是在做一些非默认?
    • 您可以通过上面的 pom.file 查看“默认”是什么。只需使用编译范围解压缩fake-1.0-SNAPSHOT-jar-with-dependencies.jar 并查看包含 guice 文件。
    • 看看你的例子。他们没有使用“jar-with-dependencies”配置,所以它不是“the-same-defaults-as-mine”。程序集本身不是 maven 中的默认值,它是一个插件,具有自定义配置 - 因此,当您已经覆盖默认值时,很难分辨什么是“默认”以及什么不是!我用jar-with-dependencies汇编来说明依赖scope的效果,这里给你...
    猜你喜欢
    • 2011-01-15
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2013-03-25
    相关资源
    最近更新 更多