提问人:Gavin Gu 提问时间:5/7/2019 最后编辑:Gavin Gu 更新时间:5/4/2023 访问量:1919
关于 mvn dependency:analyze 未使用的声明依赖发现问题
About mvn dependency:analyze Unused declared dependencies found problem
问:
我运行命令来检查我的 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
答:
如果声明的依赖项没有标记,则将使用。
如果仅将其用于测试,则应将其声明为 .<scope>
<scope>compile</scope>
<scope>test</scope>
评论
我遇到了同样的问题。从想象上讲,这对我有帮助,尽管为了完整起见,您可以这样做:
Springboot会检查类路径并添加依赖项,因此您不必这样做。对于 maven pedendency 插件,这是不透明的,似乎没有使用这些依赖项。
我做了什么来管理这个问题:
- 运行:它将打印出如下内容:
mvn dependency:analyze
[WARNING] Unused declared dependencies found:
[WARNING] org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
- 删除 .pom 文件中的依赖项。
- 跑
mvn verify
- 如果由于缺少依赖项而导致构建失败,请将此依赖项作为“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 依赖项插件发现的每个警告重复此操作。
评论