[英]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.