为什么用maven-compiler-plugin编译时还需要javac

Why is javac still need when compiling with maven-compiler-plugin

我读到了 maven-compiler-plugin。在链接页面上,它告诉:

Currently, the Compiler Plugin is bundled with the javac compiler artifact with artifactId plexus-compiler-javac, which can be seen as one of the dependencies declared inside the Compiler Plugin's POM

根据我的理解,我们不再需要本地javac了,在我的本地操作系统上,仅仅JRE而不是JDK就足够了。

我确实尝试删除 JDK,但只在我的本地系统上安装了 JRE。但是,当我尝试使用 maven 和命令 mvn clean compile 进行编译时,出现了错误消息 -

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

谁能解释一下为什么会这样?可能是我的理解有误?

Maven 不是 java 编译器。 Maven 运行 为您提供 java 编译器并配置所有路径。

当你 运行 mvn -X compile 你会看到编译器的详细信息。

结论:您需要安装 java 编译器来编译 java - 即使 Maven 会为您启动编译器。

来自 the documentation :

Plexus Compiler

Plexus Compiler is a Plexus component to use different compilers through a uniform API.

Contrary to this plugin's name, the Compiler Plugin does not compile the sources of your project by itself.

因此 plexus-compiler-javac 工件必须被视为 wrapper 才能编译。
没有说明它包含 javac 程序。

准确的说,从3.0版本开始,默认使用的不再是直接javac,而是javax.tools(更具体地说是javax.tools.JavaCompiler)。
maven-compiler-plugin 文档确实指出:

Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse

你要明白,Maven核心插件不会重新发明轮子,猜你需要什么
要编译 java 类 你需要一个 Java 编译器并且通常你希望能够确保你使用特定的 Java 编译器作为 OS 和 Java 版本问题。
所以 Maven 编译器插件需要并在后台使用 JDK 。