zno2

依赖分析:无用依赖,循环依赖 (dependency:analyze)(一)

1. dependency:analyze

 这是 maven-dependency-plugin 插件的goal

https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html

使用时 mvn clean dependency:analyze

2. 依赖分析

项目示例:

.
│  pom.xml
│  
│      
├─z-1
│  │  pom.xml
│  │  
│  │      
│  └─z-11
│      │  pom.xml
│      │  
│      │      
│      └─src
│          └─main
│              └─java
│                      Z11.java
│                      
└─z-2
    │  pom.xml
    │  
    │      
    ├─z-21
    │  │  pom.xml
    │  │  
    │  │      
    │  └─src
    │      └─main
    │          └─java
    │                  Z21.java
    │                  
    └─z-22
        │  pom.xml
        │  
        │      
        └─src
            └─main
                └─java
                        Z22.java
                        

 

<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>cn.zno</groupId>
    <artifactId>z-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>z-1</module>
        <module>z-2</module>
    </modules>
</project>
z-test 的pom.xml
<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>
    <parent>
        <groupId>cn.zno</groupId>
        <artifactId>z-test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>z-1</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>z-11</module>
    </modules>
</project>
z-1 的 pom.xml
<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>
    <parent>
        <groupId>cn.zno</groupId>
        <artifactId>z-test</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>z-2</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>z-21</module>
        <module>z-22</module>
    </modules>
</project>
z-2 的pom.xml
<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>
  <parent>
    <groupId>cn.zno</groupId>
    <artifactId>z-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>z-11</artifactId>
</project>
z-11 的pom.xml
public class Z11 {

}
Z11.java
<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>
    <parent>
        <groupId>cn.zno</groupId>
        <artifactId>z-2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>z-21</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.zno</groupId>
            <artifactId>z-11</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
z-21 的pom.xml
public class Z21 {

}
Z21.java
<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>
    <parent>
        <groupId>cn.zno</groupId>
        <artifactId>z-2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>z-22</artifactId>

    <dependencies>
        <dependency>
            <groupId>cn.zno</groupId>
            <artifactId>z-21</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
z-22 的pom.xml
public class Z22 {

    private Z11 z11;
    private Z21 z21;
}
Z22.java

 

z-test 是base pom ,其下包含两个模块z-1 和 z-2

z-1 包含一个模块 z-11

z-2 包含两个模块z-21 和 z-22

其中z-21 引入了z-11 但在java代码中未使用(UnusedDeclared)

其中z-22 引入了z-21 ,使用了z-11 的代码 (UsedUndeclared)

 

执行 mvn clean dependency:analyze

D:\e\workspaces\MYWORKSPACE\z-test>mvn clean  dependency:analyze
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] z-test
[INFO] z-1
[INFO] z-11
[INFO] z-2
[INFO] z-21
[INFO] z-22
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder wit
h a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building z-test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ z-test ---
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) @ z-test >>>
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) @ z-test <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ z-test ---
[INFO] Skipping pom project
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building z-1 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ z-1 ---
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) @ z-1 >>>
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) @ z-1 <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ z-1 ---
[INFO] Skipping pom project
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building z-11 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ z-11 ---
[INFO] Deleting D:\e\workspaces\MYWORKSPACE\z-test\z-1\z-11\target
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) @ z-11 >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ z-11 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\e\workspaces\MYWORKSPACE\z-test\z-1\z-11\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ z-11 ---
[INFO] Compiling 1 source file to D:\e\workspaces\MYWORKSPACE\z-test\z-1\z-11\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ z-11 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\e\workspaces\MYWORKSPACE\z-test\z-1\z-11\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ z-11 ---
[INFO] No sources to compile
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) @ z-11 <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ z-11 ---
[INFO] No dependency problems found
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building z-2 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ z-2 ---
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) @ z-2 >>>
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) @ z-2 <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ z-2 ---
[INFO] Skipping pom project
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building z-21 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ z-21 ---
[INFO] Deleting D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-21\target
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) @ z-21 >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ z-21 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-21\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ z-21 ---
[INFO] Compiling 1 source file to D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-21\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ z-21 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-21\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ z-21 ---
[INFO] No sources to compile
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) @ z-21 <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ z-21 ---
[WARNING] Unused declared dependencies found:
[WARNING]    cn.zno:z-11:jar:0.0.1-SNAPSHOT:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building z-22 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ z-22 ---
[INFO] Deleting D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-22\target
[INFO]
[INFO] >>> maven-dependency-plugin:2.8:analyze (default-cli) @ z-22 >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ z-22 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-22\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ z-22 ---
[INFO] Compiling 1 source file to D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-22\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ z-22 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\e\workspaces\MYWORKSPACE\z-test\z-2\z-22\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ z-22 ---
[INFO] No sources to compile
[INFO]
[INFO] <<< maven-dependency-plugin:2.8:analyze (default-cli) @ z-22 <<<
[INFO]
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ z-22 ---
[WARNING] Used undeclared dependencies found:
[WARNING]    cn.zno:z-11:jar:0.0.1-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] z-test ............................................ SUCCESS [  1.496 s]
[INFO] z-1 ............................................... SUCCESS [  0.011 s]
[INFO] z-11 .............................................. SUCCESS [  1.642 s]
[INFO] z-2 ............................................... SUCCESS [  0.013 s]
[INFO] z-21 .............................................. SUCCESS [  0.116 s]
[INFO] z-22 .............................................. SUCCESS [  0.089 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.880 s
[INFO] Finished at: 2017-08-29T17:50:30+08:00
[INFO] Final Memory: 19M/260M
[INFO] ------------------------------------------------------------------------

 

两处红字,

第一处 z21 声明了 z11 但是没有使用z11

第二处 z22 通过 z21 的依赖传递 直接使用z11 

 

3. 智能构建顺序

正常顺序

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] z-test
[INFO] z-1
[INFO] z-11
[INFO] z-2
[INFO] z-21
[INFO] z-22

 

非正常顺序(类似创建java对象,用到了引用就去找,找到了先建父类,再建子类)

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] z-test
[INFO] z-2
[INFO] z-21
[INFO] z-1
[INFO] z-11
[INFO] z-22

NOTE: 非正常的存在隐患,就是不会超出范围(范围内智能调整顺序),一旦范围内不存在所需依赖,同时本地仓库中有没有所需依赖,就会构建失败。

上例中z-11 依赖 z-21 , 父pom 范围包含z-test,z-1,z-2,z-11,z-21,z-22  

假如 构建的pom此时换做 z-1 ,(同时清除本地repository) ,则会构建失败

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] z-1
[INFO] z-11

此处内容省略。。。

[ERROR] Failed to execute goal on project z-11: Could not resolve dependencies for project cn.zno:z-11:jar:0.
0.1-SNAPSHOT: Could not find artifact cn.zno:z-21:jar:0.0.1-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :z-11

 

如何较快速度添加 used undeclared

 [WARNING] 这块的格式是固定的,写个小程序,入参是缺失的依赖列表,通过 BufferReader 读取每一行

mvn -fn -o clean dependency:analyze > E://maven.log

当模块特别多的时候,可以指定-fn (构建失败也会继续执行),可以指定 -o (离线)

4. 循环依赖

分析循环依赖之前可以通过 dependency:analyze 去除无用的

[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.bull.framework:framework-entities:mfoit-latest-SNAPSHOT'}' and 'Vertex{label='com.bull.framework:framework-dmd2-generator:mfoit-latest-SNAPSHOT'}' introduces to cycle in the graph com.bull.framework:framework-dmd2-generator:mfoit-latest-SNAPSHOT --> com.bull.framework:framework-dmd2-vengine:mfoit-latest-SNAPSHOT --> com.bull.framework:framework-entities:mfoit-latest-SNAPSHOT --> com.bull.framework:framework-dmd2-generator:mfoit-latest-SNAPSHOT -> [Help 1]

com.bull.framework:framework-dmd2-generator:mfoit-latest-SNAPSHOT -->
com.bull.framework:framework-dmd2-vengine:mfoit-latest-SNAPSHOT -->
com.bull.framework:framework-entities:mfoit-latest-SNAPSHOT -->
com.bull.framework:framework-dmd2-generator:mfoit-latest-SNAPSHOT ->

posted on 2023-06-01 16:36  zno2  阅读(1037)  评论(0编辑  收藏  举报

导航