Maven Compiler Plugin – Maven编译器插件

最后修改: 2018年 4月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

本快速教程介绍了compiler插件,这是Maven构建工具的核心插件之一。

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

关于其他核心插件的概述,请参考这篇文章

2. Plugin Goals

2.插件目标

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:

compiler插件用于编译Maven项目的源代码。该插件有两个目标,它们已经与默认生命周期的特定阶段绑定。

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

Here’s the compiler plugin in the POM:

这里是POM中的compiler插件。

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        ...
    </configuration>
</plugin>

We can find the latest version of this plugin here.

我们可以找到这个插件的最新版本这里

3. Configuration

3.配置

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兼容。我们可以在配置元素中修改这些设置。

<configuration>
    <source>1.8</source>
    <target>1.8</target>
    <-- other customizations -->
</configuration>

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

为了方便起见,我们可以将Java版本设置为POM的属性。

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

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

有时我们想向javac编译器传递参数。这就是compilerArgs参数派上用场的地方。

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

例如,我们可以指定以下配置,让编译器对未检查的操作发出警告。

<configuration>
    <!-- other configuration -->
    <compilerArgs>
        <arg>-Xlint:unchecked</arg>
    </compilerArgs>
</configuration>

When compiling this class:

当编译这个类时。

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

    public void addText(String text) {
        textList.add(text);
    }

    public List getTextList() {
        return this.textList;
    }
}

we’ll see an unchecked warning on the console:

我们会在控制台看到一个未选中的警告。

[WARNING] ... Data.java:[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

4.java 9的更新

4.1. Configuration

4.1.配置

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及以上版本,我们可以直接使用版本号。

<configuration>
    <source>9</source>
    <target>9</target>
</configuration>

Similarly, we can define the version using properties as:

同样地,我们可以使用properties来定义版本。

<properties>
    <maven.compiler.source>9</maven.compiler.source>
    <maven.compiler.target>9</maven.compiler.target>
</properties>

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>。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

4.2. Build

4.2.构建

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.

首先,让我们创建一个MavenCompilerPlugin类,在这个类中,我们要从另一个模块中导入一个包

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
  -Dexec.mainClass="com.baeldung.maven.java9.MavenCompilerPlugin"

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

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

[ERROR] COMPILATION ERROR :
[ERROR] .../MavenCompilerPlugin.java:[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] .../MavenCompilerPlugin.java:[3,1]
  static import only from classes and interfaces
[ERROR] .../MavenCompilerPlugin.java:[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 module-info.java class and indicating that we require the java.xml module:

解决这个问题的最简单方法是创建一个module-info.java类,并指出我们需要java.xml模块。

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

Now we can try again:

现在我们可以再试试。

mvn -q clean compile exec:java
  -Dexec.mainClass="com.baeldung.maven.java9.MavenCompilerPlugin"

And our output will be:

而我们的产出将是。

The XML namespace prefix is: xml

5. Conclusion

5.结论

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.

本教程的完整源代码可以在GitHub上找到over

Next »

Quick Guide to the Maven Install Plugin

« Previous

Maven Resources Plugin