Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gradle.pdf
Скачиваний:
9
Добавлен:
24.03.2015
Размер:
1.4 Mб
Скачать

Table 23.8. Java plugin - other properties

 

 

Property name

Type

Default value

Descriptio

sourceSets

SourceSetContainer

Not null

Contains

 

(read-only)

 

the project

 

 

 

source set

sourceCompatibility JavaVersion. Can also set using a String or a Number, eg '1.5' or 1.5.

Value of the current used

Java

JVM

version

 

compatibili

 

to use whe

 

compiling

 

Java

 

source.

targetCompatibility JavaVersion. Can also set using a String or Number, eg '1.5' or 1.5.

archivesBaseName String

sourceCompatibility

Java

 

version to

 

generate

 

classes for

projectName

The

 

basename

 

to use for

 

archives,

 

such as

 

JAR or ZIP

 

files.

manifest

Manifest

an empty manifest

The

 

 

 

manifest to

 

 

 

include in

 

 

 

all JAR file

These properties are provided by convention objects of type JavaPluginConvention,

BasePluginConvention and ReportingBasePluginConvention.

23.7. Working with source sets

You can access the source sets of a project using the sourceSets property. This is a container for the project's source sets, of typeSourceSetContainer. There is also a sourceSets { } script block, which you can pass a closure to configure the source set container. The source set container works pretty much the same way as other containers, such as tasks.

Page 144 of 343

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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]