java - 编译项目中包含哪些 Maven 依赖范围?

标签 java eclipse maven dependencies m2e

例如。如果我将 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默认情况下,依赖项中的文件不包含在任何内容中!

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

    现在,例如使用 assemblypackage 的输出中包含依赖项的插件阶段(见 Including dependencies in a jar with Maven ),你通常会得到这个默认行为:
  • provided : 不包括
  • compile (默认):包括
  • system : 不包括
  • runtime :包括
  • test : 不包括

  • 结帐this link以供引用。

    编辑:只需在 guice 上尝试使用不同范围值的这个 pom ,您会看到依赖项包含在 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>
    

    关于java - 编译项目中包含哪些 Maven 依赖范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13531050/

    相关文章:

    java - 外观和 DAO 之间适合什么模式?

    java - 性能问题 System.out.println ("log information ")或使用 log4j 记录器文件?

    java - ListView 导致 NullPointerException

    maven - Jenkins 管道 - 使用执行 Maven 发布按钮

    maven - 在本地找不到元数据

    java - 如果从关闭 Hook 调用 System.exit 会发生什么?

    java - 获取 Windows 7 中当前壁纸文件名

    javascript - 自动化 Eclipse "Yui Compressor..."

    eclipse - eclipse中 `JUnit Plug-in Test`和 `JUnit Test`的区别

    maven - 在 Maven 项目中分析 TypeScript SonarQube