Build a Java project with Gradle

What is a Java plug-in?

To build a Java project in Gradle, you need a Java plugin. To apply the plugin, write the following in build.gradle.


apply plugin: 'java'

Java plug-in has four elements, "task", "convention", "property", and "source set". It is a packaged component. By learning about each element, you will understand how to build with Java plugins.

Source set

A source set is a concept that groups ** objects that should be compiled at the same time **. By default for Java plugins

--main: A source set of production code and production resources --test: A source set of test code and test resources

We provide the above two source sets.

The source code has the following features.

--Can be added freely with the build script --Has a unique name --You can specify the set of "Java source" and "resource" to be compiled. --You can specify the path to output the product of the compilation result (class file, etc.) --You can specify compile-time and run-time classpaths --Tasks and properties can be set for each source set


In Gradle, when you add a plugin, the work required for building is added as a task at the same time. The following tasks are also added to the Java plug-in.

task Description
compileJava Compile the production code. The target is java of the source set main.The extension stored in the directory specified by the srcDirs property file.
processResources Copy the production code resources to the classpath. The target is resources of the source set main.The extension stored in the directory specified by the srcDirs property is.Files other than java.
classes Run the compileJava and processResources tasks.
compileTestJava Behaves the same as the compileJava task for the source set test.
processTestResources Behaves the same as the processResources task for the source set test.
testClasses Run the compileTestJava and processTestResources tasks.
jar Generate a JAR file. The default archive target is the output property of the main source set.(Output of compileJava and processResources task)
javadoc Output Javadoc for the main source set.
test test Runs the tests contained in the source set. The default test target is automatically extracted from the output of the testClasses task.
uploadArchives Artif(Build artifacts)Upload the archive of.
clean Delete the build output. By default the build directory is deleted.
clean <TaskName> Specified task<TaskName>Delete only the output of.
assemble Generate appropriate artifacts depending on the project content without running tests. By default, Java plugins are equivalent to jar tasks.
check Verify the project. The Java plugin defaults to the test task.
build Build the project. Run the check and assemble tasks.
buildNeeded Build not only the project to be built, but also the project for which the dependency is set.
buildDependents Build including the projects that depend on the project to be built.

Add source set and add tasks automatically

In Java plugins, when you add a source set The following tasks are automatically added for each source set.


The convention is "In a Java project, place Java files under the src / main / java directory" It is a promise like that. Java plug-ins have the following conventions.

--Production code source file → Place it under the src / main / java directory --Production code resources → Place it under the src / main / resources directory --Test code source file → Place it under the src / test / java directory --Test code resources → Place it under the src / test / resources directory


Properties are set to control the input / output target of tasks and the directory of conventions.

Java Plugin Properties (Common)

Properties provided by the Project object.

Property Description Default
testResultDir XML file output destination of test results build/test-results
testReportDir Test report(HTML)Output destination build/reports/tests
libsDir Library(JAR file)Output destination build/libs
distDir Distribution(Archives other than JAR files)Output destination build/distributions
docsDir Document output destination such as Javadoc build/docs
sourceSets Project source set main, test source set container
sourceCompatibility Java version when compiling Java source JVM version of the build script execution environment
targetCompatibility Java version to target for compile-time class generation value of sourceCompatibility
archivesBaseName Base name of archive files such as JAR project.The value of the name property(The default is the root directory name of the project)
manifest Manifest to include in all JAR files None

Access to properties in build scripts

println project.docsDir
println docsDir

Java Plugin Properties (Provided by Source Set)

Properties provided by the sourceSets property of the Project object.

Property Description Default
name Source set name. Must be unique within the project None
output Source set output output.classDir、output.value of the resourceDir property
output.classDir Source set class file output destination build/classes/<sourceSet>
output.resourcesDir Source set resource output destination build/resources/<sourceSet>
compileClasspath Source set compile-time classpath Configuration compile<SourceSet>Setting value of
runtimeClasspath Source set runtime classpath output property, configuration runtime<sourceSet>Setting value of
java All Java sources in the source set src/<sourceSet>/The extension under java file
java.srcDirs Directory containing Java sources for the source set src/<sourceSet>/java
resources All resources in the source set src/<sourceSet>/The extension under resources is.Non-java files
resources.srcDirs Directory containing resources for the source set src/<sourceSet>/resources
allJava All Java sources in the source set java property settings(Subject to change due to plug-in application)
allSource All Java sources and resources in the source set java, resources property settings(Subject to change due to plug-in application)

Access to properties in build scripts

println project.sourceSets.test.compileClasspath
println sourceSets.test.compileClasspath
println sourceSets['test'].compileClasspath


This article is written with reference to the following books.

Reference book: Thorough introduction to Gradle Building an automation platform with next-generation build tools

Recommended Posts

Build a Java project with Gradle
[Gradle] Build a Java project with a configuration different from the convention
CICS-Run Java applications-(3) Build management with Gradle
java build a triangle
First gradle build (Java)
Build Java with Wercker
Build and test Java + Gradle applications with Wercker
Using Gradle with VS Code, build Java → run
Build Spring Boot project by environment with Gradle
Java tips-Create a Spring Boot project in Gradle
Add a project in any folder with Gradle
Automate Java (Maven) project build with CircleCI + Orbs
Build a Java development environment with VS Code
Java multi-project creation with Gradle
[Environment construction] Build a Java development environment with VS Code!
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Create a Java (Gradle) project with VS Code and develop it on a Docker container
[Rails] Creating a new project with rails new
Build a web application with Javalin
Create a Java project using Eclipse
Java build with mac vs code
[Java] [Play Framework] Until the project is started with Gradle
Create a Maven project with a command
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Spring Boot gradle build with Docker
[Note] A story about changing Java build tools with VS Code
How to create a new Gradle + Java + Jar project in Intellij 2016.03
[Java] Create an executable module with Gradle
Build a PureScript development environment with Docker
Build a Java development environment on Mac
Build a Wordpress development environment with Docker
De-cron! Build a job scheduler with Rundeck
CICS-Run Java applications-(2) Build management with Maven
Split a string with ". (Dot)" in Java
Build OpenCV with Java Wrapper on Ubuntu 18.04
Configure a multi-project with subdirectories in Gradle
Java automated test implementation with JUnit 5 + Gradle
With [AWS] CodeStar, you can build a Spring (Java) project running on Lambda in just 3 minutes! !!
vagrant java build
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
Create a website with Spring Boot + Gradle (jdk1.8.x)
Read a string in a PDF file with Java
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
[Gradle] Build operations often performed in Java projects
Static code analysis with Checkstyle in Java + Gradle
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
[Java] Create a collection with only one element
Build Java x Spring x VSCode x Gradle on Docker (1)
Build an E2E test environment with Selenium (Java)
[Win10] Build a JSF development environment with NetBeans
Prepare a scraping environment with Docker and Java
Build a development environment for Docker, java, vscode
Build a Java runtime environment on Sakura VPS
Try debugging a Java program with VS Code
[Jakarta EE 8 application development with Gradle] 2. Project creation