version = "1.1"

task copyReport(type: Copy) {
    from file("${buildDir}/reports/my-report.pdf")
    into file("${buildDir}/toArchive")
}

task copyReport2(type: Copy) {
    from "${buildDir}/reports/my-report.pdf"
    into "${buildDir}/toArchive"
}

task myReportTask {
    ext.outputFile = file("${buildDir}/reports/my-report.pdf")

    outputs.files outputFile
    doLast {
        outputFile.touch()
    }
}

task archiveReportsTask {
    ext.dirToArchive = file("${buildDir}/toArchive")
    inputs.dir dirToArchive
}

task copyReport3(type: Copy) {
    from myReportTask.outputFile
    into archiveReportsTask.dirToArchive
}

task copyReportsForArchiving(type: Copy) {
    from "${buildDir}/reports/my-report.pdf", "src/docs/manual.pdf"
    into "${buildDir}/toArchive"
}

task copyPdfReportsForArchiving(type: Copy) {
    from "${buildDir}/reports"
    include "*.pdf"
    into "${buildDir}/toArchive"
}

task copyAllPdfReportsForArchiving(type: Copy) {
    from "${buildDir}/reports"
    include "**/*.pdf"
    into "${buildDir}/toArchive"
}

task copyReportsDirForArchiving(type: Copy) {
    from "${buildDir}/reports"
    into "${buildDir}/toArchive"
}

task copyReportsDirForArchiving2(type: Copy) {
    from("${buildDir}") {
        include "reports/**"
    }
    into "${buildDir}/toArchive"
}

task packageDistribution(type: Zip) {
    archiveName = "my-distribution.zip"
    destinationDir = file("${buildDir}/dist")

    from "${buildDir}/toArchive"
}

task copyFromStaging(type: Copy) {
    from "src/main/webapp"
    into "${buildDir}/explodedWar"

    rename '(.+)-staging(.+)', '$1$2'
}

task copyWithTruncate(type: Copy) {
    from "${buildDir}/reports"
    rename { String filename ->
        if (filename.size() > 10) {
            return filename[0..7] + "~" + filename.size()
        }
        else return filename
    }
    into "${buildDir}/toArchive"
}







task copyTask(type: Copy) {
    from 'src/main/webapp'
    into "${buildDir}/explodedWar"
}

task copyTaskWithPatterns(type: Copy) {
    from 'src/main/webapp'
    into "${buildDir}/explodedWar"
    include '**/*.html'
    include '**/*.jsp'
    exclude { FileTreeElement details ->
        details.file.name.endsWith('.html') &&
            details.file.text.contains('DRAFT')
    }
}

task anotherCopyTask(type: Copy) {
    // Copy everything under src/main/webapp
    from 'src/main/webapp'
    // Copy a single file
    from 'src/staging/index.html'
    // Copy the output of a task
    from copyTask
    // Copy the output of a task using Task outputs explicitly.
    from copyTaskWithPatterns.outputs
    // Copy the contents of a Zip file
    from zipTree('src/main/assets.zip')
    // Determine the destination directory later
    into { getDestDir() }
}

def getDestDir() {
    file('some-dir')
}

task copyMethod {
    doLast {
        copy {
            from 'src/main/webapp'
            into "${buildDir}/explodedWar"
            include '**/*.html'
            include '**/*.jsp'
        }
    }
}

task copyMethodWithExplicitDependencies{
    // up-to-date check for inputs, plus add copyTask as dependency
    inputs.files copyTask
    outputs.dir 'some-dir' // up-to-date check for outputs
    doLast{
        copy {
            // Copy the output of copyTask
            from copyTask
            into 'some-dir'
        }
    }
}

configurations { runtime }

task rename(type: Copy) {
    from 'src/main/webapp'
    into "${buildDir}/explodedWar"
    // Use a closure to convert all file names to upper case
    rename { String fileName ->
        fileName.toUpperCase()
    }
    // Use a regular expression to map the file name
    rename '(.+)-staging-(.+)', '$1$2'
    rename(/(.+)-staging-(.+)/, '$1$2')
}

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

task filter(type: Copy) {
    from 'src/main/webapp'
    into "${buildDir}/explodedWar"
    // Substitute property tokens 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]"
    }
    // Use a closure to remove lines
    filter { String line ->
        line.startsWith('-') ? null : line
    }
    filteringCharset = 'UTF-8'
}

task test {
    dependsOn tasks.withType(Copy)
    dependsOn copyMethod
    dependsOn copyMethodWithExplicitDependencies
}

def appClasses = layout.files("$buildDir/classes")

CopySpec webAssetsSpec = copySpec {
    from 'src/main/webapp'
    include '**/*.html', '**/*.png', '**/*.jpg'
    rename '(.+)-staging(.+)', '$1$2'
}

task copyAssets(type: Copy) {
    into "${buildDir}/inPlaceApp"
    with webAssetsSpec
}

task distApp(type: Zip) {
    archiveName = 'my-app-dist.zip'
    destinationDir = file("${buildDir}/dists")

    from appClasses
    with webAssetsSpec
}

def webAssetPatterns = {
    include '**/*.html', '**/*.png', '**/*.jpg'
}

task copyAppAssets(type: Copy) {
    into "${buildDir}/inPlaceApp"
    from 'src/main/webapp', webAssetPatterns
}

task archiveDistAssets(type: Zip) {
    archiveName = 'distribution-assets.zip'
    destinationDir = file("${buildDir}/dists")

    from 'distResources', webAssetPatterns
}

task forcedCopy(type: Copy) {
    into "${buildDir}/inPlaceApp"
    from 'src/main/webapp'

    doFirst {
        ant.defaultexcludes remove: "**/.git"
        ant.defaultexcludes remove: "**/.git/**"
        ant.defaultexcludes remove: "**/*~"
    }

    doLast {
        ant.defaultexcludes default: true
    }
}
