简体   繁体   中英

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" -> ...

What I see is that this operation brings all the jars inside .m2/repository to the classpath of the project.

What I want is that only the dependencies that are mentioned in pom.xml file will be used in classpath. 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.

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. 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:

    <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). My local repository contains about 5000 artifacts ...

The concept of the local maven repository is a replacement for that what is called "Libraries" in IntelliJ and Eclipse, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM