Maven 项目中无法解析 com.sun:tools:0?

6

最近我一直在从事测试自动化的工作。

现在我已经熟悉了几种工具、框架、依赖项等,比如Selenium、JUNits、TestNG等,它们帮助我作为QA工程师管理我的日常工作。

我经常使用Selenium和TestNG来构建我的解决方案,还有在运行最新JDK版本(jdk-15.0.1)的IntelliJ IDEA上运行的MacOS Catalina Version 10.15.7。

我已经为我的项目配置好了POM.xml文件,并且自始至终在控制台上显示以下错误:

Cannot resolve com.sun:tools:0

我一直忽略它,因为我的IDE中的项目总是编译和运行,并且因为我从来没有使用过这个依赖项。但是现在我正在尝试从Mac终端用Maven支持运行我的项目,由于sun:tools.jar依赖关系,我无法编译到目前为止开发的任何工作项目。

我阅读了很多类似问题的相似线程,例如:

我已经尝试了所有方法,但许多解决方案都是在jdk安装目录中找到tools.jar文件。由于我正在使用JDK 15,此依赖项已被删除:

已删除:rt.jar和tools.jar

以前存储在lib/rt.jar、lib/tools.jar、lib/dt.jar和各种其他内部JAR文件中的类和资源文件现在以更高效的格式存储在实现特定的文件中,在lib目录中。这些文件的格式未指定,并且可能随时更改而不另行通知。

我附上终端显示的确切错误信息:

 ----------------------< org.example:YelpExercise >----------------------
[INFO] Building YelpExercise 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.737 s
[INFO] Finished at: 2020-11-04T18:59:47-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project YelpExercise: Could not resolve dependencies for project org.example:YelpExercise:jar:1.0-SNAPSHOT: Could not find artifact com.sun:tools:jar:0 at specified path /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/../lib/tools.jar -> [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

1
tools.jar在JDK9中被删除,正如您所发现的那样。如果您从pom.xml中删除依赖项并运行,会发生什么情况 - 哪些代码无法编译?您可能需要从那里开始。 - Bajal
那就是问题所在!我没有在我的pom.xml中明确添加tools.jar依赖项。我认为我的IDE会这样做,因为它某种方式指向了我的jdk,因此想要解决该依赖关系?我真的不太了解这方面的知识。我猜应该有一种方法来排除特定的依赖项,但我不知道如何在我的pom.xml文件中声明这个显式排除,也不知道如何设置我的IDE,使其不再寻找tools.jar。 - Nicolas Sanchez
看起来你的项目中某个 Maven 目标需要 tools.jar,但自 JDK 9+ 起已将其移除,因此如果在 JDK 9+ 下运行 Maven,则会出现此错误。 - Andrey
确实,这种情况一直在发生。我刚刚找到了解决方案并回答了我的问题。看看吧。 - Nicolas Sanchez
1个回答

7

好的,在经过数小时甚至数天的研究后,我找到了解决方案:

对于 JDK 9 及以上版本,当定义 pom.xml 文件时,您必须在使用包含它的依赖项时排除该子依赖项。 在我的情况下,cobertura 依赖项包含 sun.com:tools

我在 pom.xml 文件中进行了编辑:

<!-- https://mvnrepository.com/artifact/net.sourceforge.cobertura/cobertura -->
    <dependency>
        <groupId>net.sourceforge.cobertura</groupId>
        <artifactId>cobertura</artifactId>
        <version>2.1.1</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>com.sun</groupId>
                <artifactId>tools</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

对于一些Maven依赖项,这似乎仍然是一个未解决的问题。请查看:issues1issues2或Google搜索:<dependency_you_are_using> sun tools了解更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接