apply plugin: 'war'
apply plugin: 'maven'

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

repositories {
    flatDir {
        dirs "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

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: uri("pomRepo"))
            snapshotRepository(url: uri("snapshotRepo"))
        }
    }
}

// Customize the contents of the pom

def installer = install.repositories.mavenInstaller
def deployer = uploadArchives.repositories.mavenDeployer

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

[installer, deployer]*.pom*.version = '1.0MVN'
installer.pom.project {
    groupId 'installGroup'
}
deployer.pom.groupId = 'deployGroup'

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")
}

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