apply plugin: 'maven'

// Create a source jar for uploading
task sourceJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.java
}

configurations {
    deployerJars
}

artifacts {
    archives sourceJar
}

dependencies {
    deployerJars "org.springframework.build.aws:org.springframework.build.aws.maven:3.0.0.RELEASE"
}

gradle.taskGraph.whenReady {graph ->
    if (graph.hasTask(uploadArchives)) {
        // check properties defined and fail early
        s3AccessKey
        s3SecretAccessKey
    }
}

def deployer = null

uploadArchives {
    deployer = repositories.mavenDeployer {
        configuration = configurations.deployerJars
    }
    doFirst {
        if (releaseBuild) {
            // "mavenSyncRepoDir" should be set in properties
            repository(url: mavenSyncRepoDir)
        } else {
            s3credentials = [userName: s3AccessKey, passphrase: s3SecretAccessKey]
            repository(url: "s3://maven.springframework.org/milestone") {
                authentication(s3credentials)
            }
            snapshotRepository(url: "s3://maven.springframework.org/snapshot") {
                authentication(s3credentials)
            }
        }
    }
}

// Pom Customization

installer = install.repositories.mavenInstaller

def optionalDeps = ['commons-logging', 'ehcache', 'log4j', 'apacheds-core', 'jsp-api', 'jsr250-api', 'ldapsdk']

[installer, deployer]*.pom.collect { pom ->
    pom.scopeMappings.addMapping(10, configurations.provided, 'provided')
}

[installer, deployer]*.pom*.whenConfigured { pom ->
    pom.dependencies.findAll { dep ->
        optionalDeps.contains(dep.artifactId) ||
        dep.groupId.startsWith('org.apache.directory') ||
        dep.groupId.startsWith('org.slf4j')
    }*.optional = true

    if (pom.artifactId == 'spring-security-config') {
        pom.dependencies.find { dep -> dep.artifactId == 'spring-security-web'}.optional = true
        pom.dependencies.find { dep -> dep.artifactId == 'spring-web'}.optional = true
    }
}
