【问题标题】:IntelliJ: How to make Maven project use only the dependencies show in pom.xml fileIntelliJ:如何使 Maven 项目仅使用 pom.xml 文件中显示的依赖项
【发布时间】:2017-05-28 16:23:38
【问题描述】:

我通过导航到文件 -> 新建 -> 项目 -> Maven -> 标记“从原型创建”-> org.apache.maven.archetypes: maven-archetype-quickstart 来打开一个新的 Maven 项目 -> .. .

我看到的是这个操作把 .m2/repository 里面的所有 jars 都带到了项目的类路径中。

我想要的是只有 pom.xml 文件中提到的依赖项才会在类路径中使用。例如,如果 pom.xml 对包 A 和 B 有依赖关系,并且 .m2/repository 路径包含一个 C 包(可能来自另一个项目),那么我的项目将无法解析包 C 的符号。

你知道怎么做吗?

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

【问题讨论】:

  • 你确定没有从项目 A 或 B 到 C 的传递依赖吗?那就是 A pom.xml 或 B pom.xml 是否对 C 有依赖关系?
  • 这不是它的工作原理,每个依赖都有传递依赖(即它们本身依赖于其他库)。 Maven 的本质是处理那些传递依赖,以便您的应用程序正常工作。如果这些依赖项需要传递依赖项才能运行,那么您也必须拥有它们。
  • 好的。但是,如果我编写代码并且忘记在 pom.xml 文件中提及我需要的依赖项怎么办?也许我的代码有效,因为我已经在我以前的一个项目中安装了这个依赖项(所以它在 .m2 中),但是一旦我将此项目部署到远程计算机,该项目就会出错,
  • @CrazySynthax “如果我写代码而忘记提及依赖项怎么办” 只要您不手动更改 IDE 的“构建路径”,maven 就会正确处理这个问题.它不仅仅包含本地存储库中的所有可用 jar。它可能看起来像这样,因为您只有一个项目并且 maven 刚刚下载了所有需要的 jar。试试这个:复制你的项目,更改artefactId,完全删除&lt;dependencies&gt;并检查副本“构建路径”,

标签: java maven intellij-idea pom.xml dependency-management


【解决方案1】:

Maven 的核心功能是解决传递依赖。这些工件会自动下载并添加到类路径中。

但在某些情况下,您不希望获取传递依赖项。看这张图:

在这里,您可以使用 &lt;exclusions&gt; 元素防止工件 C2 被 maven 获取:

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>DepB</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.example</groupId>
                <artifactId>DepC2</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

【讨论】:

    【解决方案2】:

    本地存储库中的工件(默认为~/.m2/repository在您的每个项目中使用,不会自动包含在项目类路径中 ,但在需要时会包含它们(可能作为传递依赖项)。我的本地存储库包含大约 5000 个工件...

    本地 maven 存储库的概念也是 IntelliJ 和 Eclipse 中所谓的“库”的替代品。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 2020-08-06
      • 1970-01-01
      • 2014-02-19
      • 2018-01-02
      • 1970-01-01
      相关资源
      最近更新 更多