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

问题描述

最近我一直在进行自动化测试。

我现在已经熟悉了Selenium,JUNits,TestNG等多种工具,框架,依赖项等,可以帮助我管理作为质量检查工程师的日常工作。

我经常使用Selenium和TestNG以及 在最新JDK版本(jdk-15.0.1)上运行的 IntelliJ IDEA 构建我的解决方案。 在macOs Catalina版本10.15.7上运行的Mac

我已经为我的项目配置了mi POM.mxl文件,并且从一开始就在控制台上显示以下错误

Cannot resolve com.sun:tools:0

我总是忽略了这一点,因为我的IDE中的项目始终会编译和运行,并且因为我从未使用过这种依赖关系。但是现在我试图在Mac上的终端机上通过maven支持来运行我的项目,并且由于sun:tools.jar的依赖性,我不允许编译我到目前为止开发的任何工作项目。

我已经阅读了很多关于类似问题的类似主题,它们具有相同的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

解决方法

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

对于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了解更多信息。