ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 01.01.2026
Просмотров: 1156
Скачиваний: 0
22.6. Third party plugins
You can find a list of external plugins on the wiki.
Page 135 of 343
23
The Java Plugin
The Java plugin adds Java compilation, testing and bundling capabilities to a project. It serves as the basis for many of the other Gradle plugins.
23.1. Usage
To use the Java plugin, include in your build script:
Example 23.1. Using the Java plugin
build.gradle
apply plugin: 'java'
23.2. Source sets
The Java plugin introduces the concept of a source set. A source set is simply a group of source files which are compiled and executed together. These source files may include Java source files and resource files. Other plugins add the ability to include Groovy and Scala source files in a source set. A source set has an associated compile classpath, and runtime classpath.
One use for source sets is to group source files into logical groups which describe their purpose. For example, you might use a source set to define an integration test suite, or you might use separate source sets to define the API and implementation classes of your project.
The Java plugin defines two standard source sets, called main and test. The main source set contains your production source code, which is compiled and assembled into a JAR file. The test source set contains your unit test source code, which is compiled and executed using JUnit or TestNG.
23.3. Tasks
The Java plugin adds a number of tasks to your project, as shown below.
Table 23.1. Java plugin - tasks
Page 136 of 343
Task name |
Depends on |
Type |
Description |
compileJava |
All tasks which produce |
Compile |
Compiles production |
|
the compile classpath. |
|
Java source files |
|
This includes the jar task |
|
using javac. |
|
for project dependencies |
|
|
|
included in the compile |
|
|
|
configuration. |
|
|
processResources |
- |
Copy |
Copies production |
|
|
|
resources into the |
|
|
|
production classes |
|
|
|
directory. |
classes |
compileJava and processRTaskesources |
Assembles the |
|
|
. Some plugins add |
|
production classes |
|
additional compilation |
|
directory. |
|
tasks. |
|
|
compileTestJava |
compile, plus all tasks |
Compile |
Compiles test Java |
|
which produce the test |
|
source files using |
|
compile classpath. |
|
javac. |
processTestResources |
- |
Copy |
Copies test |
|
|
|
resources into the |
|
|
|
test classes |
|
|
|
directory. |
testClasses |
compileTestJava and procesTaskTestResourcesAssembles the test |
||
|
. Some plugins add |
|
classes directory. |
|
additional test compilation |
|
|
|
tasks. |
|
|
jar |
compile |
Jar |
Assembles the JAR |
|
|
|
file |
javadoc
test
compile
compile, compileTest, plus all tasks which produce the test runtime classpath.
Javadoc Generates API documentation for the production Java source, using Javadoc
Test Runs the unit tests using JUnit or TestNG.
uploadArchives |
The tasks which produce |
Upload |
Uploads the artifacts |
|
the artifacts in the archives |
|
in the archives |
|
configuration, including jar |
|
configuration, |
|
. |
|
including the JAR |
|
|
|
file. |
|
|
|
Page 137 of 343 |
clean |
- |
Delete |
Deletes the project |
|
|
|
build directory. |
cleanTaskName |
- |
Delete |
Deletes the output |
|
|
|
files produced by |
|
|
|
the specified task. |
|
|
|
For example cleanJa |
|
|
|
will delete the JAR |
|
|
|
file created by the jar |
|
|
|
task, and cleanTest |
|
|
|
will delete the test |
|
|
|
results created by |
|
|
|
the test task. |
For each source set you add to the project, the Java plugin adds the following compilation tasks:
Table 23.2. Java plugin - source set tasks
Task name |
Depends on |
Type |
Description |
|
compileSourceSetAll tasksJavawhich produce the source set's |
Compile |
Compiles the |
||
|
compile classpath. |
|
given source |
|
|
|
|
|
set's Java |
|
|
|
|
source files |
|
|
|
|
using javac. |
processSourceSet- |
Resources |
Copy |
Copies the |
|
|
|
|
|
given source |
|
|
|
|
set's |
resources into the classes directory.
sourceSetClassescompileSourceSetJava and processSourceSetTaskResourcesAssembles
. Some plugins add additional compilation tasks |
the given |
for the source set. |
source set's |
|
classes |
|
directory. |
The Java plugin also adds a number of tasks which form a lifecycle for the project:
Page 138 of 343
Table 23.3. Java plugin - lifecycle tasks |
|
|
Task name |
Depends on |
Type |
assemble |
All archive tasks in the |
Task |
|
project, including jar. |
|
|
Some plugins add |
|
|
additional archive tasks to |
|
|
the project. |
|
check |
All verification tasks in the |
Task |
|
project, including test. |
|
|
Some plugins add |
|
|
additional verification |
|
|
tasks to the project. |
|
build |
check and assemble |
Task |
buildNeeded |
build and build tasks in |
Task |
|
all project lib |
|
|
dependencies of the testRuntime |
|
|
configuration. |
|
buildDependents |
build and build tasks in |
Task |
|
all projects with a project |
|
|
lib dependency on this |
|
|
project in a testRuntime |
|
|
configuration. |
|
buildConfigurationNameThe tasks which produce |
Task |
|
|
the artifacts in |
|
|
configuration |
|
|
ConfigurationName. |
|
uploadConfigurationNameThe tasks which uploads |
Upload |
the artifacts in |
|
configuration |
|
ConfigurationName. |
|
Description
Assembles all the archives in the project.
Performs all verification tasks in the project.
Performs a full build of the project.
Performs a full build of the project and all projects it depends on.
Performs a full build of the project and all projects which depend on it.
Assembles the artifacts in the specified configuration. The task is added by the Base plugin which is implicitly applied by the Java plugin.
Assembles and uploads the artifacts in the specified configuration. The task is added by the Base plugin which is implicitly applied by the Java plugin.
The following diagram shows the relationships between these tasks.
Page 139 of 343