例如。如果我将 BukkitApi jar 设置为依赖范围设置为提供、编译、系统、运行时或测试的 Maven 项目的依赖项
bukkitAPI 将在哪些范围内包含在编译输出中?
最佳答案
短版:默认情况下,maven 输出(在默认的 target
目录中)除了当前项目/模块的编译代码之外不包含任何内容。也就是说,没有任何依赖关系。
Long(er) 版本:默认 jar
包装并且没有自定义阶段配置。这是 maven 在 java 项目中的行为方式:
compile
阶段:.java
src/main/java/
中的文件目录编译为 .classes
target
中的文件目录。 compile
的依赖项范围被下载到您的本地存储库。 package
阶段:与 1 相同,另外您将获得 jar
target
中的文件目录 install
阶段:与 2 相同,您将获得 jar
本地存储库中的文件。 所以,
.jar
默认情况下,依赖项中的文件不包含在任何内容中!那么,如何在“输出”中包含依赖项,这些范围是什么意思?
现在,例如使用
assembly
在 package
的输出中包含依赖项的插件阶段(见 Including dependencies in a jar with Maven ),你通常会得到这个默认行为:provided
: 不包括 compile
(默认):包括 system
: 不包括 runtime
:包括test
: 不包括 结帐this link以供引用。
编辑:只需在
guice
上尝试使用不同范围值的这个 pom ,您会看到依赖项包含在 fake-1.0-SNAPSHOT-jar-with-dependencies.jar
中。当范围为 compile
和 runtime
(这个例子不需要任何源文件)<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>
关于java - 编译项目中包含哪些 Maven 依赖范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13531050/