Gradle: sourceCompatiblity vs targetCompatibility – Gradle: sourceCompatiblity vs targetCompatibility

最后修改: 2022年 2月 16日

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

1. Overview

1.概述

In this article, we’ll look at the difference between sourceCompatbility and targetCompatibility Java configurations as well as their usage in Gradle.

在这篇文章中,我们将看看sourceCompatbilitytargetCompatibilityJava配置的区别,以及它们在Gradle中的用法。

You can check our Introduction to Gradle article to learn more about the basics.

您可以查看我们的Gradle简介文章,以了解更多的基础知识。

2. Handling Versions in Java

2.在Java中处理版本

When we compile a Java program using javac, we can provide compilation options for version handling. There are two options available:

当我们使用javac编译一个Java程序时,我们可以为版本处理提供编译选项。有两个选项可用。

  • -source with values that match the Java versions, up to the JDK we are using for compilation (for example, 1.8 for JDK8). The version value we provide will restrict the language features that we can use in our source code to its respective Java version.
  • -target is similar but controls the version of the generated class files. This means that the version value we provide will be the lowest Java version our program can run on.

For example:

比如说。

javac HelloWorld.java -source 1.6 -target 1.8

This will generate a class file that requires Java 8 or above to run. Additionally, the source code cannot contain lambda expressions or any feature not available in Java 6.

这将产生一个需要Java 8或以上版本才能运行的类文件。此外,源代码不能包含lambda表达式或任何Java 6中没有的功能

3. Handling Versions With Gradle

3.用Gradle处理版本

Gradle, along with the Java plugin, lets us set the source and target options with the sourceCompatibility and targetCompatibility configurations of the java task. Similarly, we use the same values as we do with javac.

Gradle与Java插件一起,让我们用sourceCompatibilitytargetCompatibility任务的java配置来设置source target选项。同样地,我们使用与javac相同的值。

Let’s set up the build.gradle file:

我们来设置build.gradle文件。

plugins {
    id 'java'
}

group 'com.baeldung'

java {
    sourceCompatibility = "1.6"
    targetCompatibility = "1.8"
}

4. HelloWorldApp Example Compilation

4.HelloWorldApp 编译实例

We can create a Hello World! console app and demonstrate the functionality by building it using the above script.

我们可以创建一个Hello World!控制台应用程序,并通过使用上述脚本构建它来演示功能。

Let’s create a very simple class:

让我们创建一个非常简单的类。

public class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

When we build it using the gradle build command, Gradle will generate a class file with the name HelloWorldApp.class.

当我们使用gradle build命令构建它时,Gradle将生成一个名为HelloWorldApp.class的类文件。

We can use the javap command-line tool that is packaged with Java to check the generated bytecode version of this class file:

我们可以使用随Java打包的javap命令行工具来检查这个类文件的字节码生成版本。

javap -verbose HelloWorldApp.class

This prints a lot of information, but in the first few lines, we can see:

这打印了很多信息,但在前几行,我们可以看到。

public class com.baeldung.helloworld.HelloWorldApp
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER

The major version field has the value 52, which is the version number for Java 8 class files. This means that our HelloWorldApp.class can only run using Java 8 and above.

主要版本字段的值是52,这是Java 8类文件的版本号。这意味着我们的HelloWorldApp.class只能使用Java 8及以上版本运行

To test the sourceCompatibility configuration, we can change our source code and introduce a feature that is not available in Java 6.

为了测试sourceCompatibility配置,我们可以改变我们的源代码并引入Java 6中没有的功能。

Let’s use a lambda expression:

让我们使用一个lambda表达式。

public class HelloWorldApp {

    public static void main(String[] args) {
        Runnable helloLambda = () -> {
            System.out.println("Hello World!");
        }
        helloLambda.run();
    }

}

If we try to build our code with Gradle, we’ll see a compilation error:

如果我们试图用Gradle来构建我们的代码,我们会看到一个编译错误。

error: lambda expressions are not supported in -source 1.6

The -source option, which is the Java equivalent of the sourceCompatibility Gradle configuration, prevents our code from compiling. Basically, it protects us from using higher version features by mistake if we don’t want to introduce them – for example, we may want our app to be able to run on Java 6 runtimes as well.

-source选项,相当于Java的sourceCompatibilityGradle配置,防止我们的代码被编译。基本上,如果我们不想引入更高版本的功能,它可以保护我们不误用这些功能–例如,我们可能希望我们的应用程序也能在Java 6运行时运行。

5. Conclusion

5.总结

In this article, we explained how to use the -source and -target compilation options to handle the versions of our Java source code and the target runtime. In addition, we learned how these options map to Gradle’s sourceCompatbility and targetCompatibility configurations with the Java plugin and demonstrated their functionality in practice.

在这篇文章中,我们解释了如何使用-source-target编译选项来处理我们的Java源代码和目标运行时的版本。此外,我们了解了这些选项如何与Gradle的sourceCompatbilitytargetCompatibility配置的Java插件相对应,并在实践中演示了其功能。

As always, the source code for this article is available over on GitHub.

一如既往,本文的源代码可在GitHub上获得over