Search code examples
javamavenjavacmaven-compiler-plugin

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


I read about this the maven-compiler-plugin. On the linked page, it told:

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

According to my understanding, we do not need the local javac anymore, say on my local operating system, mere JRE instead of JDK is enough.

I did try to remove JDK and installed only JRE on my local system. However, when I tried to compile with maven with command mvn clean compile, there was the error message -

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

Could anyone explain why is it so? Probably my understanding is wrong?


Solution

  • From 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.

    So the plexus-compiler-javac artifact has to be considered as a wrapper to compile.
    It is not stated that it contains the javac program.

    To be exact, from the 3.0 version, it is not directly javac any longer that is by default used but javax.tools (more particularly javax.tools.JavaCompiler).
    The maven-compiler-plugin documentation states indeed :

    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

    You have to understand that the Maven core plugin will not reinvent the wheel and guess what you need.
    To compile java classes you need a Java compiler and generally you want to able to ensure that you are using a specific Java compiler as OS and Java version matter.
    So the maven compiler plugin needs and uses a JDK under the hood.