The Java Plugin
The Java plugin adds Java compilation along with testing and bundling capabilities to a project.It serves as the basis for many of the other JVM language Gradle plugins.You can find a comprehensive introduction and overview to the Java Plugin in theBuilding Java Projects chapter.
As indicated above, this plugin adds basic building blocks for working with JVM projects.Its feature set has been superseded by other plugins, offering more features based on your project type.Instead of applying it directly to your project, you should look into the |
Usage
To use the Java plugin, include the following in your build script:
plugins { java}plugins { id 'java'}Tasks
The Java plugin adds a number of tasks to your project, as shown below.
compileJava—JavaCompileDepends on: All tasks which contribute to the compilation classpath, including
jartasks from projects that are on the classpath via project dependenciesCompiles production Java source files using the JDK compiler.
processResources—ProcessResourcesCopies production resources into the production resources directory.
classesDepends on:
compileJava,processResourcesThis is an aggregate task that just depends on other tasks. Other plugins may attach additional compilation tasks to it.
compileTestJava—JavaCompileDepends on:
classes, and all tasks that contribute to the test compilation classpathCompiles test Java source files using the JDK compiler.
processTestResources—CopyCopies test resources into the test resources directory.
testClassesDepends on:
compileTestJava,processTestResourcesThis is an aggregate task that just depends on other tasks. Other plugins may attach additional test compilation tasks to it.
jar—JarDepends on:
classesAssembles the production JAR file, based on the classes and resources attached to the
mainsource set.javadoc—JavadocDepends on:
classesGenerates API documentation for the production Java source using Javadoc.
test—TestDepends on:
testClasses, and all tasks which produce the test runtime classpathRuns the unit tests using JUnit or TestNG.
clean—DeleteDeletes the project build directory.
cleanTaskName—DeleteDeletes files created by the specified task. For example,
cleanJarwill delete the JAR file created by thejartask andcleanTestwill delete the test results created by thetesttask.
SourceSet Tasks
For each source set you add to the project, the Java plugin adds the following tasks:
compileSourceSetJava—JavaCompileDepends on: All tasks which contribute to the source set’s compilation classpath
Compiles the given source set’s Java source files using the JDK compiler.
processSourceSetResources—CopyCopies the given source set’s resources into the resources directory.
sourceSetClasses—TaskDepends on:
compileSourceSetJava,processSourceSetResourcesPrepares the given source set’s classes and resources for packaging and execution. Some plugins may add additional compilation tasks for the source set.
Lifecycle Tasks
The Java plugin attaches some of its tasks to the lifecycle tasks defined by theBase Plugin — which the Java Plugin applies automatically — and it also adds a few other lifecycle tasks:
assembleDepends on:
jarAggregate task that assembles all the archives in the project. This task is added by the Base Plugin.
checkDepends on:
testAggregate task that performs verification tasks, such as running the tests. Some plugins add their own verification tasks to
check. You should also attach any customTesttasks to this lifecycle task if you want them to execute for a full build. This task is added by the Base Plugin.buildDepends on:
check,assembleAggregate tasks that performs a full build of the project. This task is added by the Base Plugin.
buildNeededDepends on:
build, andbuildNeededtasks in all projects that are dependencies in thetestRuntimeClasspathconfiguration.Performs a full build of the project and all projects it depends on.
buildDependentsDepends on:
build, andbuildDependentstasks in all projects that have this project as a dependency in theirtestRuntimeClasspathconfigurationsPerforms a full build of the project and all projects which depend upon it.
buildConfigName— task ruleDepends on: all tasks that generate the artifacts attached to the named — ConfigName — configuration
Assembles the artifacts for the specified configuration. This rule is added by the Base Plugin.
The following diagram shows the relationships between these tasks.

Project layout
The Java plugin assumes the project layout shown below. None of these directories need to exist or have anything in them. The Java plugin will compile whatever it finds, and handles anything which is missing.
src/main/javaProduction Java source.
src/main/resourcesProduction resources, such as XML and properties files.
src/test/javaTest Java source.
src/test/resourcesTest resources.
src/sourceSet/javaJava source for the source set namedsourceSet.
src/sourceSet/resourcesResources for the source set namedsourceSet.
Changing the project layout
You configure the project layout by configuring the appropriate source set. This is discussed in more detail in the following sections. Here is a brief example which changes the main Java and resource source directories.
sourceSets { main { java { setSrcDirs(listOf("src/java")) } resources { setSrcDirs(listOf("src/resources")) } }}sourceSets { main { java { srcDirs = ['src/java'] } resources { srcDirs = ['src/resources'] } }}Source sets
The plugin adds the followingsource sets:
mainContains the production source code of the project, which is compiled and assembled into a JAR.
testContains your test source code, which is compiled and executed using JUnit or TestNG. These are typically unit tests, but you can include any test in this source set as long as they all share the same compilation and runtime classpaths.
Source set properties
The following table lists some of the important properties of a source set. You can find more details in the API documentation forSourceSet.
name— (read-only)StringThe name of the source set, used to identify it.
output— (read-only)SourceSetOutputThe output files of the source set, containing its compiled classes and resources.
output.classesDirs— (read-only)FileCollectionDefault value:
layout.buildDirectory.dir("classes/java/$name"), e.g.build/classes/java/mainThe directories to generate the classes of this source set into. May contain directories for other JVM languages, e.g.build/classes/kotlin/main.
output.resourcesDir—FileDefault value:
layout.buildDirectory.dir("resources/$name"), e.g.build/resources/mainThe directory to generate the resources of this source set into.
compileClasspath— FileCollectionDefault value:
${name}CompileClasspathconfigurationThe classpath to use when compiling the source files of this source set.
annotationProcessorPath— FileCollectionDefault value:
${name}AnnotationProcessorconfigurationThe processor path to use when compiling the source files of this source set.
runtimeClasspath— FileCollectionDefault value:
$output,${name}RuntimeClasspathconfigurationThe classpath to use when executing the classes of this source set.
java— (read-only)SourceDirectorySetThe Java source files of this source set. Contains only
.javafiles found in the Java source directories, and excludes all other files.java.srcDirs—Set<File>Default value:
src/$name/java, e.g.src/main/javaThe source directories containing the Java source files of this source set. You can set this to any value that is described inthis section.
java.destinationDirectory—DirectoryPropertyDefault value:
layout.buildDirectory.dir("classes/java/$name"), e.g.build/classes/java/mainThe directory to generate compiled Java sources into. You can set this to any value that is described inthis section.
resources— (read-only)SourceDirectorySetThe resources of this source set. Contains only resources, and excludes any
.javafiles found in the resource directories. Other plugins, such as theGroovy Plugin, exclude additional types of files from this collection.resources.srcDirs—Set<File>Default value:
[src/$name/resources]The directories containing the resources of this source set. You can set this to any type of value that is described inthis section.
allJava— (read-only)SourceDirectorySetDefault value: Same as
javapropertyAll Java files of this source set. Some plugins, such as the Groovy Plugin, add additional Java source files to this collection.
allSource— (read-only)SourceDirectorySetDefault value: Sum of everything in the
resourcesandjavapropertiesAll source files of this source set of any language. This includes all resource files and all Java source files. Some plugins, such as the Groovy Plugin, add additional source files to this collection.
Defining new source sets
See theintegration test example in theTesting in Java & JVM projects chapter.
Some other simple source set examples
Adding a JAR containing the classes of a source set:
tasks.register<Jar>("intTestJar") { from(sourceSets["intTest"].output)}tasks.register('intTestJar', Jar) { from sourceSets.intTest.output}Generating Javadoc for a source set:
tasks.register<Javadoc>("intTestJavadoc") { source(sourceSets["intTest"].allJava) classpath = sourceSets["intTest"].compileClasspath}tasks.register('intTestJavadoc', Javadoc) { source sourceSets.intTest.allJava classpath = sourceSets.intTest.compileClasspath}Adding a test suite to run the tests in a source set:
tasks.register<Test>("intTest") { testClassesDirs = sourceSets["intTest"].output.classesDirs classpath = sourceSets["intTest"].runtimeClasspath}tasks.register('intTest', Test) { testClassesDirs = sourceSets.intTest.output.classesDirs classpath = sourceSets.intTest.runtimeClasspath}Dependency management
The Java plugin adds a number ofdependency configurations to your project, as shown below.Tasks such ascompileJava andtest then use one or more of those configurations to get the corresponding files and use them, for example by placing them on a compilation or runtime classpath.
Dependency configurations
The role of each configuration is described in the following tables.
For information on the For information on the |
Declarable (Bucket) Configurations
| Configuration name | Role | Description |
|---|---|---|
| Declarable | Represents dependencies that are required both at compile time and runtime for the main source set of a project (i.e., implementation only dependencies). |
| Declarable | Represents dependencies that are required only at compile time and are not included in the runtime classpath (i.e., compile time only dependencies, not used at runtime). |
| Declarable | Represents dependencies that are required only at runtime and are not included in the compile classpath (i.e., dependencies required only at runtime). |
| Declarable | Represents dependencies that are required both at compile time and runtime for the test source set of a project (i.e., implementation only dependencies for tests). |
| Declarable | Represents dependencies that are required only at compile time for the test source set of a project and are not included in the runtime classpath (i.e., additional dependencies only for compiling tests, not used at runtime). |
| Declarable | Represents dependencies that are required only at runtime for the test source set of a project (i.e., runtime only dependencies for running tests). |
| Declarable | Represents annotation processors used during the compilation of a project’s source code (i.e., annotation processors used during compilation). |
Resolvable Configurations
| Configuration name | Role | Description |
|---|---|---|
| Resolvable | Represents the classpath used when compiling the main sources, which includes dependencies from both |
| Resolvable | Represents the classpath used to run the main sources, which includes dependencies from both |
| Resolvable | Represents the classpath used to compile the test sources, which includes dependencies from both |
| Resolvable | Represents the classpath used to run the test sources, which includes dependencies from both |
The following diagrams show the dependency configurations for themain source set. You can use this legend to interpret the colors:
blue background — you can declare dependencies against the configuration.
green background — the configuration is for consumption by tasks, not for you to declare dependencies.
grey background — a task.

And the next graph describes thetest source set:

For each source set you add to the project, the Java plugins adds the following dependency configurations:
SourceSet dependency configurations
sourceSetImplementationCompile time dependencies for the given source set. Used by
sourceSetCompileClasspath,sourceSetRuntimeClasspath.sourceSetCompileOnlyCompile time only dependencies for the given source set, not used at runtime.
sourceSetCompileClasspathextendssourceSetCompileOnly,sourceSetImplementationCompile classpath, used when compiling source. Used by
compileSourceSetJava.sourceSetAnnotationProcessorAnnotation processors used during compilation of this source set.
sourceSetRuntimeOnlyRuntime only dependencies for the given source set.
sourceSetRuntimeClasspathextendssourceSetRuntimeOnly,sourceSetImplementationRuntime classpath contains elements of the implementation, as well as runtime only elements.
Contributed extension
The Java plugin adds thejava extension to the project.This allows to configure a number of Java related properties inside a dedicated DSL block.
java { toolchain { languageVersion = JavaLanguageVersion.of(17) }}java { toolchain { languageVersion = JavaLanguageVersion.of(17) }}Below is the list of properties and DSL functions with short explanations available inside thejava extension.
Toolchain and compatibility
toolchainJava toolchain to be used by tasks using JVM tools, such as compilation and execution. Default value: build JVM toolchain.
JavaVersion sourceCompatibilityJava version compatibility to use when compiling Java source. Default value: language version of the toolchain from this extension.
Note that using atoolchain is preferred to using a compatibility setting for most cases.JavaVersion targetCompatibilityJava version to generate classes for. Default value:
sourceCompatibility.
Note that using atoolchain is preferred to using a compatibility setting for most cases.
Packaging
withJavadocJar()Automatically packages Javadoc and creates a variant
javadocElementswith an artifact-javadoc.jar, which will be part of the publication.withSourcesJar()Automatically packages source code and creates a variant
sourceElementswith an artifact-sources.jar, which will be part of the publication.
Directory properties
String reporting.baseDirThe name of the directory to generate reports into, relative to the build directory. Default value:
reports(read-only) File reportsDirThe directory to generate reports into. Default value:
reporting.baseDirectoryString testResultsDirNameThe name of the directory to generate test result .xml files into, relative to the build directory. Default value:
test-results(read-only) File testResultsDirThe directory to generate test result .xml files into. Default value:
layout.buildDirectory.dir(testResultsDirName)String testReportDirNameThe name of the directory to generate the test report into, relative to the reports directory. Default value:
tests(read-only) File testReportDirThe directory to generate the test report into. Default value:
reportsDir/testReportDirNameString libsDirNameThe name of the directory to generate libraries into, relative to the build directory. Default value:
libs(read-only) File libsDirThe directory to generate libraries into. Default value:
layout.buildDirectory.dir(libsDirName)String distsDirNameThe name of the directory to generate distributions into, relative to the build directory. Default value:
distributions(read-only) File distsDirThe directory to generate distributions into. Default value:
layout.buildDirectory.dir(distsDirName)String docsDirNameThe name of the directory to generate documentation into, relative to the build directory. Default value:
docs(read-only) File docsDirThe directory to generate documentation into. Default value:
layout.buildDirectory.dir(docsDirName)String dependencyCacheDirNameThe name of the directory to cache source dependency information relative to the build directory. Default value:
dependency-cache.
Other properties
(read-only)SourceSetContainer sourceSetsContains the project’s source sets. Default value: Not nullSourceSetContainer
String archivesBaseNameThe basename to use for archives, such as JAR or ZIP files. Default value:
projectNameManifest manifestThe manifest to include in all JAR files. Default value: an empty manifest.
Testing
See theTesting in Java & JVM projects chapter for more details.
Publishing
components.javaASoftwareComponent forpublishing the production JAR created by the
jartask.This component includes the runtime dependency information for the JAR.
See also thejava extension.
Incremental Java compilation
Gradle comes with a sophisticated incremental Java compiler that is active by default.
This gives you the following benefits
Incremental builds are much faster.
The smallest possible number of class files are changed.Classes that don’t need to be recompiled remain unchanged in the output directory.An example scenario when this is really useful is using JRebel — the fewer output classes are changed the quicker the JVM can use refreshed classes.
To help you understand how incremental compilation works, the following provides a high-level overview:
Gradle will recompile all classesaffected by a change.
A class isaffected if it has been changed or if it depends on another affected class.This works no matter if the other class is defined in the same project, another project or even an external library.
A class’s dependencies are determined from type references in its bytecode or symbol analysis via a compiler plugin.
Since source-retention annotations are not visible in bytecode, changes to a source-retention annotation will result in full recompilation.
You can improve incremental compilation performance by applying good software design principles like loose coupling.For instance, if you put an interface between a concrete class and its dependents, the dependent classes are only recompiled when the interface changes, but not when the implementation changes.
The class analysis is cached in the project directory, so the first build after a clean checkout can be slower. Consider turning off the incremental compiler on your build server.
The class analysis is also an output stored in the build cache, which means that if a compilation output is fetched from the build cache, then the incremental compilation analysis will be too and the next compilation will be incremental.
Known issues
If you are using an annotation processor that reads resources (e.g. a configuration file), you need to declare those resources as an input of the compile task.
If a resource file is changed, Gradle will trigger a full recompilation.
Using a custom
executableorjavaHomedeactivates some optimizations.The compile task does not use incremental build immediately after a compile error or if a Java constant changes.Usetoolchains instead if possible.Having a source structure that does not match the package names, while legal for compilation, might end up causing trouble in the toolchain.Even more if annotation processing andcaching are involved.
Incremental annotation processing
Starting with Gradle 4.7, the incremental compiler also supports incremental annotation processing.All annotation processors need to opt in to this feature, otherwise they will trigger a full recompilation.
As a user you can see which annotation processors are triggering full recompilations in the--info log.Incremental annotation processing will be deactivated if a customexecutable orjavaHome is configured on the compile task.
Making an annotation processor incremental
Please first have a look atincremental Java compilation, as incremental annotation processing builds on top of it.
Gradle supports incremental compilation for two common categories of annotation processors: "isolating" and "aggregating".Please consult the information below to decide which category fits your processor.
You can then register your processor for incremental compilation using a file in the processor’s META-INF directory.The format is one line per processor, with the fully qualified name of the processor class and its case-insensitive category separated by a comma.
Example: Registering incremental annotation processors
org.gradle.EntityProcessor,isolatingorg.gradle.ServiceRegistryProcessor,dynamic
If your processor can only decide at runtime whether it is incremental or not, you can declare it as "dynamic" in the META-INF descriptorand return its true type at runtime using theProcessor#getSupportedOptions() method.
Example: Registering incremental annotation processors dynamically
@Overridepublic Set<String> getSupportedOptions() { return Collections.singleton("org.gradle.annotation.processing.aggregating");}Both categories have the following limitations:
They can only read
CLASSorRUNTIMEretention annotations.They can only read parameter names if the user passes the
-parameterscompiler argument.They must generate their files using theFiler API.Writing files any other way will result in silent failures later on, as these files won’t be cleaned up correctly.If your processor does this, it cannot be incremental.
They must not depend on compiler-specific APIs like
com.sun.source.util.Trees.Gradle wraps the processing APIs, so attempts to cast to compiler-specific types will fail.If your processor does this, it cannot be incremental, unless you have some fallback mechanism.If they useFiler#createResource,the
locationargument must be one of these values fromStandardLocation:CLASS_OUTPUT,SOURCE_OUTPUT, orNATIVE_HEADER_OUTPUT.Any other argument will disable incremental processing.
"Isolating" annotation processors
The fastest category, these look at each annotated element in isolation, creating generated files or validation messages for it.For instance anEntityProcessor could create a<TypeName>Repository for each type annotated with@Entity.
Example: An isolated annotation processor
Set<? extends Element> entities = roundEnv.getElementsAnnotatedWith(entityAnnotation);for (Element entity : entities) { createRepository((TypeElement) entity);}"Isolating" processors have the following additional limitations:
They must make all decisions (code generation, validation messages) for an annotated type based on information reachable from its AST.This means you can analyze the types' super-class, method return types, annotations etc., even transitively.But you cannot make decisions based on unrelated elements in the RoundEnvironment.Doing so will result in silent failures because too few files will be recompiled later.If your processor needs to make decisions based on a combination of otherwise unrelated elements, mark it as "aggregating" instead.
They must provide exactly one originating element for each file generated with the
FilerAPI.If zero or many originating elements are provided, Gradle will recompile all source files.
When a source file is recompiled, Gradle will recompile all files generated from it.When a source file is deleted, the files generated from it are deleted.
"Aggregating" annotation processors
These can aggregate several source files into one or more output files or validation messages.For instance, aServiceRegistryProcessor could create a singleServiceRegistry with one method for each type annotated with@Service.
Example: An aggregating annotation processor
JavaFileObject serviceRegistry = filer.createSourceFile("ServiceRegistry");Writer writer = serviceRegistry.openWriter();writer.write("public class ServiceRegistry {");for (Element service : roundEnv.getElementsAnnotatedWith(serviceAnnotation)) { addServiceCreationMethod(writer, (TypeElement) service);}writer.write("}");writer.close();Gradle will always reprocess (but not recompile) all annotated files that the processor was registered for.Gradle will always recompile any files the processor generates.
State of support in popular annotation processors
| Many popular annotation processors support incremental annotation processing (see the table below).Check with the annotation processor project directly for the most up-to-date information and documentation. |
| Annotation Processor | Supported since | Details |
|---|---|---|
N/A | ||
N/A | ||
Partly supported. | ||
N/A | ||
N/A | ||
DataBinding | Hidden behind a feature toggle | |
Dagger | 2.18 Feature toggle support, 2.24 Enabled by default | |
kapt | Hidden behind a feature toggle | |
Toothpick | N/A | |
Glide | N/A | |
Android-State | N/A | |
Parceler | N/A | |
Dart and Henson | N/A | |
N/A | ||
N/A | ||
N/A | ||
Requery | N/A | |
N/A | ||
EclipseLink | N/A | |
N/A | ||
Immutables | N/A | |
2.2.0 Feature toggle support, 2.3.0-alpha02 Enabled by default | ||
N/A | ||
N/A | ||
DBFlow | N/A | |
AndServer | N/A | |
N/A | ||
N/A | ||
N/A | ||
N/A | ||
Hidden behind a feature toggle | ||
N/A |
Compilation avoidance
If a dependent project has changed in anABI-compatible way (only its private API has changed), then Java compilation tasks will be up-to-date. This means that if projectA depends on projectB and a class inB is changed in an ABI-compatible way (typically, changing only the body of a method), then Gradle won’t recompileA.
Some of the types of changes that do not affect the public API and are ignored:
Changing a method body
Changing a comment
Adding, removing or changing private methods, fields, or inner classes
Adding, removing or changing a resource
Changing the name of jars or directories in the classpath
Renaming a parameter
Since implementation details matter for annotation processors, they must be declared separately on the annotation processor path.Gradle ignores annotation processors on the compile classpath.
dependencies { // The dagger compiler and its transitive dependencies will only be found on annotation processing classpath annotationProcessor("com.google.dagger:dagger-compiler:2.44") // And we still need the Dagger library on the compile classpath itself implementation("com.google.dagger:dagger:2.44")}dependencies { // The dagger compiler and its transitive dependencies will only be found on annotation processing classpath annotationProcessor 'com.google.dagger:dagger-compiler:2.44' // And we still need the Dagger library on the compile classpath itself implementation 'com.google.dagger:dagger:2.44'}Variant aware selection
The whole set of JVM plugins leveragevariant aware resolution for the dependencies used.They also install a set of attributes compatibility and disambiguation rules toconfigure the Gradle attributes for the specifics of the JVM ecosystem.