ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 01.01.2026
Просмотров: 1155
Скачиваний: 0
Example 23.3. Accessing a source set
build.gradle
//Various ways to access the main source set println sourceSets.main.output.classesDir println sourceSets['main'].output.classesDir sourceSets {
println main.output.classesDir
}
sourceSets { main {
println output.classesDir
}
}
//Iterate over the source sets sourceSets.all {
println name
}
To configure an existing source set, you simply use one of the above access methods to set the properties of the source set. The properties are described below. Here is an example which configures the main Java and resources directories:
Example 23.4. Configuring the source directories of a source set
build.gradle
sourceSets { main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
23.7.1. 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 for SourceSet.
Table 23.9. Java plugin - source set properties
Property name |
Type |
Default value |
Description |
name |
String (read-only) |
Not null |
The name |
|
|
|
of the |
|
|
|
source set, |
|
|
|
used to |
|
|
|
identify it. |
Page 145 of 343
output |
SourceSetOutput |
Not null |
The output |
|
(read-only) |
|
files of the |
|
|
|
source set, |
|
|
|
containing |
|
|
|
its compiled |
|
|
|
classes and |
|
|
|
resources. |
output.classesDir |
File |
buildDir/classes/nameThe |
|
|
|
|
directory to |
|
|
|
generate |
|
|
|
the classes |
|
|
|
of this |
|
|
|
source set |
|
|
|
into. |
output.resourcesDir |
File |
buildDir/resources/Thename |
|
|
|
|
directory to |
|
|
|
generate |
|
|
|
the |
|
|
|
resources of |
|
|
|
this source |
|
|
|
set into. |
compileClasspath |
FileCollection |
compileSourceSet |
The |
|
|
configuration. |
classpath to |
|
|
|
use when |
|
|
|
compiling |
|
|
|
the source |
|
|
|
files of this |
|
|
|
source set. |
runtimeClasspath |
FileCollection |
output + runtimeSourceSetThe |
|
|
|
configuration. |
classpath to |
|
|
|
use when |
executing the classes of this source set.
Page 146 of 343
java |
SourceDirectorySet |
|
(read-only) |
java.srcDirs |
Set<File>. Can set |
|
using anything |
|
described in |
|
Section 18.5, |
|
“Specifying a set of |
|
input files”. |
resources |
SourceDirectorySet |
|
(read-only) |
Not null |
The Java |
|
source files |
|
of this |
|
source set. |
|
Contains |
|
only .java |
|
files found |
|
in the Java |
|
source |
|
directories, |
|
and |
|
excludes all |
|
other files. |
[projectDir/src/nameThe/java]source
|
directories |
|
containing |
|
the Java |
|
source files |
|
of this |
|
source set. |
Not null |
The |
|
resources of |
|
this source |
|
set. |
|
Contains |
|
only |
|
resources, |
|
and |
|
excludes |
|
any .java |
|
files found |
|
in the |
|
resource |
|
source |
|
directories. |
|
Other |
|
plugins, |
|
such as the |
|
Groovy |
|
plugin, |
|
exclude |
|
additional |
|
types of files |
|
from this |
|
collection. |
Page 147 of 343
resources.srcDirs |
Set<File>. Can set |
|
using anything |
|
described in |
|
Section 18.5, |
|
“Specifying a set of |
|
input files”. |
allJava |
SourceDirectorySet |
|
(read-only) |
allSource |
SourceDirectorySet |
|
(read-only) |
[projectDir/src/nameThe/resource
|
directories |
|
containing |
|
the |
|
resources of |
|
this source |
|
set. |
java |
All .java |
|
files of this |
|
source set. |
|
Some |
|
plugins, |
|
such as the |
|
Groovy |
|
plugin, add |
|
additional |
|
Java source |
|
files to this |
|
collection. |
resources + java |
All source |
|
files of this |
|
source set. |
|
This include |
|
all resource |
|
files and all |
|
Java source |
|
files. Some |
|
plugins, |
|
such as the |
|
Groovy |
|
plugin, add |
|
additional |
|
source files |
|
to this |
|
collection. |
23.7.2. Defining new source sets
To define a new source set, you simply reference it in the sourceSets { } block. Here's a
example:
Page 148 of 343
Example 23.5. Defining a source set
build.gradle
sourceSets { intTest
}
When you define a new source set, the Java plugin adds some dependency configurations for the source set, as shown in Table 23.6, “Java plugin - source set dependency configurations”.You can use these configurations to define the compile and runtime dependencies of the source set.
Example 23.6. Defining source set dependencies
build.gradle
sourceSets { intTest
}
dependencies {
intTestCompile 'junit:junit:4.8.2' intTestRuntime 'asm:asm-all:3.3.1'
}
The Java plugin also adds a number of tasks which assemble the classes for the source set, as shown in Table 23.2, “Java plugin - source set tasks”.For example, for a source set called intTes
, you can run gradle intTestClasses to compile the int test classes.
Example 23.7. Compiling a source set
Output of gradle intTestClasses
> gradle intTestClasses :compileIntTestJava :processIntTestResources :intTestClasses
BUILD SUCCESSFUL
Total time: 1 secs
23.7.3. Some source set examples
Adding a JAR containing the classes of a source set:
Example 23.8. Assembling a JAR for a source set
build.gradle
task intTestJar(type: Jar) {
from sourceSets.intTest.output
}
Page 149 of 343
Generating Javadoc for a source set:
Example 23.9. Generating the Javadoc for a source set
build.gradle
task intTestJavadoc(type: Javadoc) { source sourceSets.intTest.allJava
}
Adding a test suite to run the tests in a source set:
Example 23.10. Running tests in a source set
build.gradle
task intTest(type: Test) {
testClassesDir = sourceSets.intTest.output.classesDir classpath = sourceSets.intTest.runtimeClasspath
}
23.8. Javadoc
The javadoc task is an instance of Javadoc. It supports the core javadoc options and the options of the standard doclet described in the reference documentation of the Javadoc executable. For a complete list of supported Javadoc options consult the API documentation of the following classes:
CoreJavadocOptions and StandardJavadocDocletOptions.
Table 23.10. Java plugin - Javadoc properties
Task Property |
Type |
Default Value |
classpath |
FileCollection |
sourceSets.main.output + sourceSets.ma |
source |
FileTree. Can |
sourceSets.main.allJava |
|
set using anything |
|
|
described in |
|
|
Section 18.5, |
|
|
“Specifying a set |
|
|
of input files”. |
|
destinationDir |
File |
docsDir/javadoc |
title |
String |
The name and version of the project |
23.9. Clean
The clean task is an instance of Delete. It simply removes the directory denoted by its dir property.
Page 150 of 343
Table 23.11. Java plugin - Clean properties
Task Property |
Type |
Default Value |
dir |
File |
buildDir |
23.10. Resources
The Java plugin uses the Copy task for resource handling. It adds an instance for each source set in the project. You can find out more about the copy task in Section 18.6, “Copying files”.
Table 23.12. Java plugin - ProcessResources properties
Task Property |
Type |
srcDirs |
Object. Can set using anything |
|
described in Section 18.5, “Specifying a |
|
set of input files”. |
destinationDir |
File. Can set using anything described |
|
in Section 18.1, “Locating files”. |
Default Value
sourceSet.resources
sourceSet.output.reso
23.11. CompileJava
The Java plugin adds a Compile instance for each source set in the project. Some of the most common configuration options are shown below.
Table 23.13. Java plugin - Compile properties
Task Property |
Type |
classpath |
FileCollection |
source |
FileTree. Can set using anything |
|
described in Section 18.5, “Specifying a set |
|
of input files”. |
destinationDir |
File. |
Default Value
sourceSet.compileCl sourceSet.java
sourceSet.output.cl
The compile task delegates to Ant's javac task. Settingoptions.useAnt to false activates Gradle's direct compiler integration, bypassing the Ant task. In a future Gradle release, this wi become the default.
By default, the Java compiler runs in the Gradle process. Setting options.fork to true causes compilation to occur in a separate process. In the case of the Ant javac task, this means that a new process will be forked for each compile task, which can slow down compilation. Conversely, Gradle's direct compiler integration (see above) will reuse the same compiler process as much a possible. In both cases, all fork options specified with options.forkOptions will be honored.
Page 151 of 343