apply plugin: 'maven'

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

artifacts {
    archives sourceJar
    archives javadocJar
}

// Configuration for SpringSource s3 maven deployer
configurations {
    deployerJars
}
dependencies {
    deployerJars "org.springframework.build.aws:org.springframework.build.aws.maven:3.0.0.RELEASE"
}

install {
    customizePom(repositories.mavenInstaller.pom, project)
}

def customizePom(pom, gradleProject) {
    pom.whenConfigured { p ->
        p.dependencies.findAll{ it.scope == "optional" }.each {
            it.scope = "compile"
            it.optional = true
        }
        // sort to make pom dependencies order consistent to ease comparison of older poms
        p.dependencies = p.dependencies.sort { dep ->
            "$dep.scope:$dep.optional:$dep.groupId:$dep.artifactId"
        }
    }
    def isWar = project.hasProperty('war')
    pom.project {
        name = gradleProject.name
        if(isWar) {
            packaging = "war"
        }
        description = gradleProject.name
        url = 'http://springsource.org/spring-security'
        organization {
            name = 'SpringSource'
            url = 'http://springsource.org/'
        }
        licenses {
            license {
                name 'The Apache Software License, Version 2.0'
                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                distribution 'repo'
            }
        }
        scm {
            url = 'https://github.com/SpringSource/spring-security'
            connection = 'scm:git:git://github.com/SpringSource/spring-security'
            developerConnection = 'scm:git:git://github.com/SpringSource/spring-security'
        }
        developers {
            developer {
                id = 'rwinch'
                name = 'Rob Winch'
                email = 'rwinch@vmware.com'
            }
        }
        if(isWar) {
            properties {
                'm2eclipse.wtp.contextRoot' '/' + project.war.baseName
            }
        }
        if(!project.releaseBuild) {
            repositories {
                repository {
                    id 'spring-snasphot'
                    url 'http://repo.springsource.org/libs-snapshot'
                }
            }
        }
        build {
            plugins {
                plugin {
                    groupId = 'org.apache.maven.plugins'
                    artifactId = 'maven-compiler-plugin'
                    configuration {
                        source = '1.7'
                        target = '1.7'
                    }
                }
            }
        }
    }
}

task generatePom {
    group = 'Build'
    description = 'Generates a Maven pom.xml'

    ext.generatedPomFileName = "pom.xml"
    onlyIf { install.enabled }

    inputs.files(fileTree(project.rootProject.rootDir).include("**/*.gradle").files)
    inputs.files(new File(project.rootProject.rootDir, Project.GRADLE_PROPERTIES))
    outputs.files(generatedPomFileName)

    doLast() {
        def p = pom {}
        customizePom(p, project)
        p.writeTo(generatedPomFileName)
    }
}

build.dependsOn generatePom
