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

18.6.2. Filtering files

Example 18.15. Filtering files as they are copied

build.gradle

import org.apache.tools.ant.filters.FixCrLfFilter import org.apache.tools.ant.filters.ReplaceTokens

task filter(type: Copy) { from 'src/main/webapp' into 'build/explodedWar'

//Substitute property references in files expand(copyright: '2009', version: '2.3.1') expand(project.properties)

//Use some of the filters provided by Ant filter(FixCrLfFilter)

filter(ReplaceTokens, tokens: [copyright: '2009', version: '2.3.1'])

//Use a closure to filter each line

filter { String line -> "[$line]"

}

}

18.6.3. Using the CopySpec class

Copy specs form a hierarchy. A copy spec inherits its destination path, include patterns, exclude patterns, copy actions, name mappings, filters.

Example 18.16. Nested copy specs

build.gradle

task nestedSpecs(type: Copy) { into 'build/explodedWar' exclude '**/*staging*' from('src/dist') {

include '**/*.html'

}

into('libs') {

from configurations.runtime

}

}

18.7. Using the Sync task

The Sync task extends the Copy task. When it executes, it copies the source files into the destination directory, and then removes any files from the destination directory which it did not copy. This can be useful for doing things such as installing your application, creating an exploded copy of your archives, or maintaining a copy of the project's dependencies.

Here is an example which maintains a copy of the project's runtime dependencies in thebuild/li directory.

Page 107 of 343

Example 18.17. Using the Sync task to copy dependencies

build.gradle

task libs(type: Sync) {

from configurations.runtime into "$buildDir/libs"

}

18.8. Creating archives

A project can have as many as JAR archives as you want. You can also add WAR, ZIP and TAR archives to your project. Archives are created using the various archive tasks: Zip, Tar, Jar, and

War. They all work the same way, so let's look at how you create a ZIP file.

Example 18.18. Creating a ZIP archive build.gradle

apply plugin: 'java'

task zip(type: Zip) { from 'src/dist' into('libs') {

from configurations.runtime

}

}

The archive tasks all work exactly the same way as the Copy task, and implement the same CopySpec interface. As

with the Copy task, you specify the input files using the from()

Why are you using the

Java plugin?

method, and can optionally specify where they end up in

 

the archive using the into() method. You can filter the

The Java plugin adds a

contents of file, rename files, and all the other things you

number of default values for

can do with a copy spec.

the archive tasks. You can

 

use the archive tasks without

18.8.1. Archive naming

using the Java plugin, if you

 

like. You will need to provide

The default name for a generated archive is projectName-version.type

For example:

values for some additional

properties.

 

Page 108 of 343

Example 18.19. Creation of ZIP archive

build.gradle

apply plugin: 'java'

version = 1.0

task myZip(type: Zip) { from 'somedir'

}

println myZip.archiveName

println relativePath(myZip.destinationDir) println relativePath(myZip.archivePath)

Output of gradle -q myZip

> gradle -q myZip zipProject-1.0.zip build/distributions

build/distributions/zipProject-1.0.zip

This adds a Zip archive task with the name myZip which produces ZIP filezipProject-1.0.zip

. It is important to distinguish between the name of the archive task and the name of the archive generated by the archive task. The default name for archives can be changed with the archivesB project property. The name of the archive can also be changed at any time later on.

There are a number of properties which you can set on an archive task. These are listed below in Table 18.1, “Archive tasks - naming properties”.You can, for example, change the name of the archive:

Example 18.20. Configuration of archive task - custom archive name

build.gradle

apply plugin: 'java' version = 1.0

task myZip(type: Zip) { from 'somedir' baseName = 'customName'

}

println myZip.archiveName

Output of gradle -q myZip

> gradle -q myZip customName-1.0.zip

You can further customize the archive names:

Page 109 of 343

Example 18.21. Configuration of archive task - appendix & classifier

build.gradle

apply plugin: 'java' archivesBaseName = 'gradle' version = 1.0

task myZip(type: Zip) { appendix = 'wrapper' classifier = 'src' from 'somedir'

}

println myZip.archiveName

Output of gradle -q myZip

> gradle -q myZip gradle-wrapper-1.0-src.zip

Page 110 of 343

trailing - is not added to the name.
If any of these properties is empty the

Table 18.1. Archive tasks - naming properties

Property name

Type

archiveName String

archivePath File

destinationDir File

baseName String

appendix String

version String

classifier String

extension String

Default value

Description

baseName-appendix-version-classifierThe.extensionbase file name of the generated archive

destinationDir/archiveName

The

 

absolute

 

path of the

 

generated

 

archive.

Depends on the archive type. JARs and

The

WARs are generated into project.buildDir/librariesdirectory to

 

generate

. ZIPs and TARs are generated into project.buildDir/dis

.

the archive

 

into

project.name

The base

 

name

 

portion of

 

the archive

 

file name.

null

The

 

appendix

 

portion of

 

the archive

 

file name.

project.version

The version

 

portion of

 

the archive

 

file name.

null

The

 

classifier

 

portion of

 

the archive

 

file name,

Depends on the archive type, and for TAR

The

files, the compression type as well: zip, jar

extension of

, war, tar, tgz or tbz2.

the archive

 

file name.

Page 111 of 343

18.8.2. Sharing content between multiple archives

Using the Project.copySpec() method to share content between archives.

Often you will want to publish an archive, so that it is usable from another project. This process is described in Chapter 44, Publishing artifacts

Page 112 of 343

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