import org.apache.maven.settings.Settings

apply plugin: 'war'
// START SNIPPET use-plugin
apply plugin: 'maven'
// END SNIPPET use-plugin

group = 'gradle'
version = '1.0'
archivesBaseName = 'mywar'
buildDir = 'target'

repositories {
    flatDir(dirs: "$projectDir/lib")
}

configurations {
  runtime.exclude group: 'excludeGroup2', module: 'excludeArtifact2'
}

dependencies {
    compile("group1:compile:1.0") {
        exclude(group: 'excludeGroup', module: 'excludeArtifact')
    }
    providedCompile "group2:providedCompile:1.0@jar"
    runtime "group3:runtime:1.0"
    providedRuntime("group4:providedRuntime:1.0@zip") {
        artifact {
            name = 'providedRuntime-util'
            type = 'war'
        }
    }
    testCompile "group5:testCompile:1.0"
    testRuntime "group6:testRuntime:1.0"
}

// Include a javadoc zip

task javadocZip(type: Zip, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives javadocZip
}

// Configure the release and snapshot repositories

def deployer = null
uploadArchives {
    repositories {
        deployer = mavenDeployer {
            repository(url: "file://localhost/$projectDir/pomRepo/")
            snapshotRepository(url: "file://localhost/$projectDir/snapshotRepo/")
        }
    }
}

// Customize the contents of the pom

installer = install.repositories.mavenInstaller

if (hasProperty('customVersion')) {
    [installer, deployer]*.pom*.version = customVersion
    installer.pom.project {
        groupId 'installGroup'
    }
    deployer.pom.groupId = 'deployGroup'
}

// START SNIPPET new-pom
task writeNewPom << {
    pom {
        project {
            inceptionYear '2008'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("$buildDir/newpom.xml")
}
// END SNIPPET new-pom

// START SNIPPET when-configured
[installer, deployer]*.pom*.whenConfigured {pom ->
    pom.dependencies.find {dep -> dep.groupId == 'group3' && dep.artifactId == 'runtime' }.optional = true
}
// END SNIPPET when-configured

task writeDeployerPom(dependsOn: uploadArchives) << {
    deployer.pom.writeTo("$buildDir/deployerpom.xml")
}

// For our integration tests

install.doLast {
    Settings settings = installer.settings
    new File(buildDir, "localRepoPath.txt").write(settings.localRepository)
}

clean {
    delete 'pomRepo'
    delete 'snapshotRepo'
}
