Maven实战-第八篇(Maven中的依赖查询)

Maven实战-第八篇(Maven中的依赖查询)

在之前的内容中我们介绍了两种查看Maven中依赖关系的方法。一个是通过External Libraries查看。另外一个就是通过Show Deagram ...查看,但不管以上是哪种方法,本质都是依赖IDEA查看的。实际上Maven提供了项目中查看依赖的命令,可以帮助我们非常方便的查询项目的依赖的情况。下面我们分别介绍一下。

一、mvn dependency:list

使用上述命令可以列出我们项目所有的依赖包。我们还是以上一篇的中的项目为例子,通过上面的命令查看一下项目的所有依赖包。

1、mazhe-maven-mall

  • pom.xml
<?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">
    <parent>
        <artifactId>midai-mazhe-maven</artifactId>
        <groupId>cn.ma-zhe</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mazhe-maven-mall</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.ma-zhe</groupId>
            <artifactId>mazhe-maven-payment</artifactId>
            <version>1.0-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba</groupId>
                    <artifactId>fastjson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>cn.ma-zhe</groupId>
            <artifactId>mazhe-maven-order</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

我们进入到这个项目的根目录中,然后执行下面命令:

mvn dependency:list
  • 输出
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:list (default-cli) @ mazhe-maven-mall ---
[INFO] 
[INFO] The following files have been resolved:
[INFO]    cn.ma-zhe:mazhe-maven-payment:jar:1.0-SNAPSHOT:compile
[INFO]    cn.ma-zhe:mazhe-maven-order:jar:1.0-SNAPSHOT:compile
[INFO]    cn.ma-zhe:mazhe-maven-user:jar:1.0-SNAPSHOT:compile
[INFO]    com.alibaba:fastjson:jar:2.0.35:compile
[INFO]    com.alibaba.fastjson2:fastjson2-extension:jar:2.0.35:compile
[INFO]    com.alibaba.fastjson2:fastjson2:jar:2.0.35:compile
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.516 s
[INFO] Finished at: 2023-07-24T22:40:06+08:00
[INFO] ------------------------------------------------------------------------

我们看上面的日志输出了我们这个项目中所有依赖包的信息,并且还包括传递性依赖的信息。这就是mvn dependency:list命令的作用。

二、mvn dependency:tree

下面我们看第二个可以查看项目依赖的命令。此命令和上面的命令一样,也可以列出我们项目中所有的依赖包,不同之处就是这个命令可以按照树的方式展示,方便我们查看依赖关系。而mvn dependency:list命令则是同一级显示的。

  • 输出
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- dependency:3.6.0:tree (default-cli) @ mazhe-maven-mall ---
[INFO] cn.ma-zhe:mazhe-maven-mall:jar:1.0-SNAPSHOT
[INFO] +- cn.ma-zhe:mazhe-maven-payment:jar:1.0-SNAPSHOT:compile
[INFO] \- cn.ma-zhe:mazhe-maven-order:jar:1.0-SNAPSHOT:compile
[INFO]    \- cn.ma-zhe:mazhe-maven-user:jar:1.0-SNAPSHOT:compile
[INFO]       \- com.alibaba:fastjson:jar:2.0.35:compile
[INFO]          \- com.alibaba.fastjson2:fastjson2-extension:jar:2.0.35:compile
[INFO]             \- com.alibaba.fastjson2:fastjson2:jar:2.0.35:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.375 s
[INFO] Finished at: 2023-07-24T22:47:15+08:00
[INFO] ------------------------------------------------------------------------

我们看上面的日志,依然输出了我们项目中所有的依赖信息,并且它是按照树形方式展示的,这样可以让我们非常方便的查看出传递性依赖的关系。通过上面日志我们知道,因为项目依赖了mazhe-maven-order,又因为该项目又依赖了mazhe-maven-user。所以Maven才会将项目mazhe-maven-mall依赖的fastjson引入到项目中。

三、mvn dependency:analyze

下面我们介绍一下非常重要的命令,使用此命令Maven可以分析当前项目的依赖,我们可以通过分析的结果来判断出来我们项目引入了哪些依赖,并且知道哪些依赖我们并没有使用。我们继续在项目的根目录中执行上面的命令查看一下日志输出

  • 日志
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< cn.ma-zhe:mazhe-maven-mall >---------------------
[INFO] Building mazhe-maven-mall 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> dependency:3.6.0:analyze (default-cli) > test-compile @ mazhe-maven-mall >>>
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ mazhe-maven-mall ---
[INFO] skip non existing resourceDirectory /Users/md/Documents/midai-mazhe-maven/mazhe-maven-mall/src/main/resources
[INFO] 
[INFO] --- compiler:3.11.0:compile (default-compile) @ mazhe-maven-mall ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- resources:3.3.1:testResources (default-testResources) @ mazhe-maven-mall ---
[INFO] skip non existing resourceDirectory /Users/md/Documents/midai-mazhe-maven/mazhe-maven-mall/src/test/resources
[INFO] 
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ mazhe-maven-mall ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< dependency:3.6.0:analyze (default-cli) < test-compile @ mazhe-maven-mall <<<
[INFO] 
[INFO] 
[INFO] --- dependency:3.6.0:analyze (default-cli) @ mazhe-maven-mall ---
[WARNING] Unused declared dependencies found:
[WARNING]    cn.ma-zhe:mazhe-maven-payment:jar:1.0-SNAPSHOT:compile
[WARNING]    cn.ma-zhe:mazhe-maven-order:jar:1.0-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.634 s
[INFO] Finished at: 2023-07-24T22:58:45+08:00
[INFO] ------------------------------------------------------------------------

我们看上面的日志,发现有警告的信息,为什么会出现警告信息呢,这是因为我们虽然引入了上述的依赖,但是我们并没有在项目中使用。下面我们在项目中调用一下MazheMavenOrder类。

  • MazheMavenOrder
public class MazheMavenOrder {
    public static void main(String[] args) {
        System.out.println("Hello World Mazhe Maven Order!");
    }
}
  • MazheMavenMall
public class MazheMavenMall {
    public static void main(String[] args) {
        System.out.println("Hello World Mazhe Maven Mall!");
        MazheMavenOrder.main(args);
    }
}

然后我们在执行一下mvn dependency:analyze命令,在观察一下日志输出。

  • 日志
[WARNING] Unused declared dependencies found:
[WARNING]    cn.ma-zhe:mazhe-maven-payment:jar:1.0-SNAPSHOT:compile

我们看日志输出,这次就只会显示mazhe-maven-payment项目未使用了,因为mazhe-maven-order项目已经在项目中引用了,Maven就不会提示这个错误警告了。

给TA打赏
共{{data.count}}人
人已打赏
Maven

Maven实战-第七篇(Maven中的依赖冲突)

2023-7-24 7:16:05

Maven

Maven实战-第九篇(Maven中的仓库)

2023-7-27 6:15:24