Maven Compiler Plugin – Maven编译器插件

最后修改: 2018年 4月 19日

1. Overview


This quick tutorial introduces the compiler plugin, one of the core plugins of the Maven build tool.


For an overview of the other core plugins, refer to this article.


2. Plugin Goals


The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle:


  • compile compile main source files
  • testCompile compile test source files

Here’s the compiler plugin in the POM:



We can find the latest version of this plugin here.


3. Configuration


By default, the compiler plugin compiles source code compatible with Java 5, and the generated classes also work with Java 5 regardless of the JDK in use. We can modify these settings in the configuration element:

默认情况下,编译器插件编译的源代码与Java 5兼容,无论使用何种JDK,生成的类也能与Java 5兼容。我们可以在配置元素中修改这些设置。

    <-- other customizations -->

For convenience, we can set the Java version as properties of the POM:



Sometimes we want to pass arguments to the javac compiler. This is where the compilerArgs parameter comes in handy.


For instance, we can specify the following configuration for the compiler to warn about unchecked operations:


    <!-- other configuration -->

When compiling this class:


public class Data {
    List<String> textList = new ArrayList();

    public void addText(String text) {

    public List getTextList() {
        return this.textList;

we’ll see an unchecked warning on the console:


[WARNING] ...[7,29] unchecked conversion
  required: java.util.List<java.lang.String>
  found:    java.util.ArrayList

As both goals of the compiler plugin are automatically bound to phases in the Maven default lifecycle, we can execute these goals with the commands mvn compile and mvn test-compile.

由于compiler插件的两个目标都被自动绑定到Maven默认生命周期的各个阶段,我们可以用mvn compilemvn test-compile命令执行这些目标。

4. Java 9 Updates 9的更新

4.1. Configuration


Until Java 8, we used the version number as 1.x where x represents Java’s version, like 1.8 for Java 8.

在Java 8之前,我们使用版本号为1.x,其中x代表Java的版本,如1.8代表Java 8。

For Java 9 and above, we can just use the version number directly:

对于Java 9及以上版本,我们可以直接使用版本号。


Similarly, we can define the version using properties as:



Maven added its support for Java 9 in 3.5.0, so we’ll need at least that version. We’ll also need at least 3.8.0 of the maven-compiler-plugin:

Maven在3.5.0中增加了对Java 9的支持,所以我们至少需要这个版本。我们还需要至少3.8.0maven-compiler-plugina>。


4.2. Build


Now it’s time to test our configuration.


First, let’s create a MavenCompilerPlugin class in which we’re importing a package from another module.


A simple one is javax.xml.XMLConstants.XML_NS_PREFIX:

一个简单的例子是 javax.xml.XMLConstants.XML_NS_PREFIX:

public class MavenCompilerPlugin {
    public static void main(String[] args) {
        System.out.println("The XML namespace prefix is: "
          + XML_NS_PREFIX);

Next, let’s compile it:


mvn -q clean compile exec:java

When using Java 9 defaults, though, we’ll get an error:

不过,当使用Java 9的默认值时,我们会得到一个错误。

[ERROR] .../[3,20]
  package javax.xml is not visible
  (package javax.xml is declared in module java.xml,
  but module com.baeldung.maven.java9 does not read it)
[ERROR] .../[3,1]
  static import only from classes and interfaces
[ERROR] .../[7,62]
  cannot find symbol
symbol:   variable XML_NS_PREFIX
location: class com.baeldung.maven.java9.MavenCompilerPlugin

The error comes from the fact that this package is in a separate module that we haven’t included yet in our build.


The simplest way to solve this is by creating a class and indicating that we require the java.xml module:


module com.baeldung.maven.java9 {
    requires java.xml;

Now we can try again:


mvn -q clean compile exec:java

And our output will be:


The XML namespace prefix is: xml

5. Conclusion


In this article, we went over the compiler plugin and described how to use it. We also learned about Maven’s support for Java 9.

在这篇文章中,我们了解了compiler插件,并介绍了如何使用它。我们还了解了Maven对Java 9的支持。

The complete source code for this tutorial can be found over on GitHub.


Next »

Quick Guide to the Maven Install Plugin

« Previous

Maven Resources Plugin