简体   繁体   English

IntelliJ:如何使Maven项目仅使用pom.xml文件中显示的依赖项

[英]IntelliJ: How to make Maven project use only the dependencies show in pom.xml file

I open a new Maven Project by navigating to File -> New -> Project -> Maven -> mark "Create from archetype" -> org.apache.maven.archetypes: maven-archetype-quickstart" -> ... 我通过导航到文件->新建->项目-> Maven->标记“从原型创建”-> org.apache.maven.archetypes:maven-archetype-quickstart“->打开一个新的Maven项目。

What I see is that this operation brings all the jars inside .m2/repository to the classpath of the project. 我看到的是,此操作将.m2 /存储库中的所有jar都带到项目的类路径。

What I want is that only the dependencies that are mentioned in pom.xml file will be used in classpath. 我想要的是在classpath中仅使用pom.xml文件中提到的依赖项。 For example, if the pom.xml has dependencies on packages A and B, and the .m2/repository path includes a C package (probably from another project), then my project will not resolve symbols of package C. 例如,如果pom.xml依赖于程序包A和B,并且.m2 /存储库路径包括C程序包(可能来自另一个项目),则我的项目将不解析程序包C的符号。

Do you know how to do it? 你知道怎么做吗?

<?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>

It is a core feature of Maven that it resolves transitive dependencies. 它是Maven的核心功能,它解决了传递依赖项。 Theses artifacts are downloaded and added to the classpath automatically. 这些工件被下载并自动添加到类路径中。

But there may be circumstances, where you don't want a transitive dependency to be fetched. 但是在某些情况下,您可能不想获取可传递的依赖关系。 Look at this diagram: 看一下这个图:

在此处输入图片说明

Here, you can prevent the artifact C2 from getting fetched by maven with the <exclusions> element: 在这里,您可以使用<exclusions>元素防止工件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>

The artifacts in your local repository ( ~/.m2/repository by default) are available for the usage in every of your projects, there are not included automatically in the projects classpath, but they are included when they are needed (maybe as transitive dependencies). 本地存储库(默认为~/.m2/repository )中的工件用于每个项目,项目类路径中未自动包含这些工件,但在需要时将其包括在内(可能是传递依赖项) )。 My local repository contains about 5000 artifacts ... 我的本地存储库包含约5000个工件...

The concept of the local maven repository is a replacement for that what is called "Libraries" in IntelliJ and Eclipse, too. 本地maven存储库的概念也替代了IntelliJ和Eclipse中的“库”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Maven pom.xml构建项目的jar文件及其依赖项? - How to build a jar file of a project along with its dependencies from Maven pom.xml? 如何在 maven 依赖项 pom.xml 文件中添加其他参数 - How to add additional param in maven dependencies pom.xml file 如何在maven pom.xml中配置依赖项? - how to configure dependencies in maven pom.xml? 为什么 Intellij 在 pom.xml 中找不到任何 Maven 依赖项? - Why Intellij do not find any Maven dependencies in pom.xml? Intellij 在 pom.xml 中找不到 Maven 依赖项 - Intellij cannot find Maven Dependencies in pom.xml 如何让 gradle 在项目的根目录为 maven 用户生成一个有效的 pom.xml 文件? - How to make gradle generate a valid pom.xml file at the root of a project for maven users? 如何仅从pom.xml而不是从Maven本身将Maven依赖项下载到本地存储库中 - How to download Maven dependencies into a local repository, only from pom.xml, not from Maven itself 找到非托管的pom.xml文件(无法在IntelliJ中导入Maven项目) - Non-managed pom.xml file found (Unable to import maven project in IntelliJ) 如何参数化Maven文件(pom.xml)? - How to parameterize a Maven file (pom.xml)? Maven 动态 Web 在 pom.xml 文件中定义的项目依赖项不起作用 - Maven Dynamic Web Project dependencies defined in pom.xml file not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM