mvn_dependency_tree和assembly不一致

一、查看有效依赖

  • mvn dependency:tree -f pom.xml
  • mvn dependency:list -f pom.xml

二、查看所有依赖

1) 显示所有依赖:
  • mvn dependency:tree -Dverbose -f pom.xml
2) 过滤依赖:
  • mvn dependency:tree -Dverbose -Dincludes=org.springframework -f pom.xml
3) includes语法:
  • -Dincludes=[groupId]:[artifactId]:[type]:[version]

Note:可使用通配符*

三、有时dependency:tree和assembly的结果不一致

1、原因

maven-assembly-pluginmaven-depdency-plugin插件使用的解析逻辑不一致。插件的依赖解析有两种方式:

1)插件本身实现了依赖解析逻辑,由其是maven3.x(未集成aether)之前的插件。

2)插件使用aether(通过MavenProject类调用)进行依赖解析。

2、解决方案

1)assembly符合预期时无需解决

dependency:tree使用的老的依赖解析逻辑,如果install结果正确则无需关心。另外,可通过mvn dependency:tree -X和maven core的解析结果进行对比。

参考Maven3.x兼容笔记

2)assembly插件升级至使用Aether解析的版本

maven-assembly-plugin升级至3.1.1以上。

引自maven-assembly-plugin:3.1.1版本的修复提交日志

[MASSEMBLY-675] Maven Assembly packaging wildcard-excluded dependencies

Since Maven 3 requirement, there is no need to resolve any dependencies
manually, and we can rely on requiresDependencyResolution. Initial patch
failed when module sets were involved: instead of getting the list of
transitive/direct dependencies on the current project, they need to be
fetched for each project so that it encompasses the possible modules in
module sets.

3)dependency插件进行依赖解析,assembly插件只负责拷贝

pom.xml中增加插件

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>

NOTE:maven-dependency-plugin使用2.8时,SNAPSHOT不会替换为时间戳参考

package.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
<assembly 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/assembly-1.0.0.xsd">
<id>package</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/conf</outputDirectory>
<excludes>
<exclude>**/*.xls</exclude>
<exclude>**/*.cs</exclude>
<exclude>**/autotest/**</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>src/main/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}/dependency</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
</fileSets>
</assembly>

参考:

  1. https://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html
  2. https://stackoverflow.com/questions/32322496/why-does-mvn-dependencytree-list-one-version-but-mvn-clean-install-try-to
  3. https://stackoverflow.com/questions/17876081/maven-dependency-rename-jar-with-timestamp-suffix

评论