Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle, "sourceCompatibility" vs "targetCompatibility"?

Tags:

java

gradle

People also ask

What is sourceCompatibility and targetCompatibility in Gradle?

According to Gradle documentation: sourceCompatibility is "Java version compatibility to use when compiling Java source." targetCompatibility is "Java version to generate classes for."

What is compileJava in Gradle?

compileJava — JavaCompile. Depends on: All tasks which contribute to the compilation classpath, including jar tasks from projects that are on the classpath via project dependencies. Compiles production Java source files using the JDK compiler.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is Gradle Annotationprocessor?

An annotation processor is a class which extends AbstractProcessor (in the package javax. annotation. processing ) providing the functionality needed to generate whatever it needs to generate, such as classes in the case of mapstruct.


targetCompatibility and sourceCompatibility maps to -target release and -source release in javac. Source is basically the source language level and target is the level of the bytecode that is generated.

More details can be found in the javac the cross compilation section.


Be careful when you use these; we've been bitten by people making assumptions.

Just because you use sourceCompability (or targetCompatibility) of 1.5 doesn't mean you can always compile your code with JDK 1.6 and expect it to work under JDK 1.5. The issue is the available libraries.

If your code happens to call some method that is only available in JDK 1.6 it will still compile with the various Compatibility options for the target VM. But when you run it, it will fail because the offending method is not present (you'll get a MethodNotFoundException or ClassNotFoundException).

For this reason, I always compare the Compatibility setting to the actual Java version I'm building under. If they don't match, I fail the build.


sourceCompatibility = specifies that version of the Java programming language be used to compile .java files. e.g sourceCompatibility 1.6 =specifies that version 1.6 of the Java programming language be used to compile .java files.

By default sourceCompatibility = "version of the current JVM in use" and targetCompatibility = sourceCompatibility

targetCompatibility = The option ensures that the generated class files will be compatible with VMs specified by targetCompatibility . Note that in most cases, the value of the -target option is the value of the -source option; in that case, you can omit the -target option.

Class files will run on the target specified by targetCompatibility and on later versions, but not on earlier versions of the VM