关于 mvn dependency:analyze 未使用的声明依赖发现问题

About mvn dependency:analyze Unused declared dependencies found problem

提问人:Gavin Gu 提问时间:5/7/2019 最后编辑:Gavin Gu 更新时间:5/4/2023 访问量:1919

问:

我运行命令来检查我的 java 项目中未使用的 jar,部分结果如下:mvn dependency:analyze

[警告]找到未使用的声明依赖项:[警告]
org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:comp ile [警告]
org.springframework.boot:spring-boot-starter-test:jar:2.0.3.RELEASE :编译 [警告]
org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.3.RELEASE :编译 [警告]
org.springframework.boot:spring-boot-starter-actuator:jar:2.0.3.REL EASE:编译 [警告] org.aspectj:aspectjweaver:jar:1.8.9:编译 [警告]

但实际上用在 src/test/java 包中,spring-boot-starter-test

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class TestClass {
...
}

我想知道为什么spring-boot-starter-test:出现在未使用的声明依赖项中找到部分。
有没有办法找到我的 java 项目中未使用的确切 jar

Maven的

评论


答:

0赞 dan1st 5/7/2019 #1

如果声明的依赖项没有标记,则将使用。 如果仅将其用于测试,则应将其声明为 .<scope><scope>compile</scope><scope>test</scope>

评论

0赞 Gavin Gu 5/7/2019
嗨 dan1st:我试过<scope>compile</scope>或<scope>test</scope>,它仍然出现在未使用的声明依赖项中找到,除了单元测试类之外,其他一些jar如spring-boot-starter-jdbc也有这个问题
0赞 Gavin Gu 5/7/2019
文本片段的一部分粘贴如下: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${springboot.version}</version> <scope>compile</scope> </dependency> <!-- --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>${springboot.version}</version> </dependency>
0赞 dan1st 5/7/2019
如果删除依赖项,会发生什么情况?
0赞 Gavin Gu 5/7/2019
代码中有引用,eclipse显示编译错误
0赞 dan1st 5/7/2019
引用是否仅在 src/main/java(或测试目录)中?
0赞 Joel Neukom 5/4/2023 #2

我遇到了同样的问题。从想象上讲,这对我有帮助,尽管为了完整起见,您可以这样做:

Springboot会检查类路径并添加依赖项,因此您不必这样做。对于 maven pedendency 插件,这是不透明的,似乎没有使用这些依赖项。

我做了什么来管理这个问题:

  1. 运行:它将打印出如下内容:mvn dependency:analyze
    [WARNING] Unused declared dependencies found: 
    [WARNING] org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
  1. 删除 .pom 文件中的依赖项。
  2. mvn verify
  3. 如果由于缺少依赖项而导致构建失败,请将此依赖项作为“usedDependency”添加到 .pom 中的 maven-dependency-plugin 设置中。
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
            <executions>
                <execution>
                    <id>analyze</id>
                    <goals>
                        <goal>analyze-only</goal>
                    </goals>
                    <configuration>
                        <ignoreNonCompile>true</ignoreNonCompile>
                        <failOnWarning>false</failOnWarning>
                        <outputXML>true</outputXML>
                        <usedDependencies>
                            <usedDependency>org.springframework.boot:spring-boot-starter</usedDependency>
                        </usedDependencies>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

对 maven 依赖项插件发现的每个警告重复此操作。