Maven依赖范围是编译项目中包含的依赖项?

Rya*_*ach 2 java eclipse dependencies maven m2e

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

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

Erw*_*lec 6

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

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

  1. compile阶段:.java在文件src/main/java/目录中获取编译.classes的文件target目录.compile范围的依赖关系将下载到本地存储库.
  2. package阶段:同1,再加上你会得到一个jar在文件target目录
  3. install阶段:同2,你会得到一个jar在你的本地库文件.

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

那么,我如何在我的"输出"中包含依赖项,这些范围意味着什么呢?

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

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

查看此链接以供参考.

编辑:只需尝试使用不同的范围值的这个pom guice,你会看到依赖关系包含在fake-1.0-SNAPSHOT-jar-with-dependencies.jar范围是什么时(compileruntime此示例不需要任何源文件)

<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>
Run Code Online (Sandbox Code Playgroud)