apply plugin: "maven"

def basePom = {
  project {
    name project.displayName
    description project.description
    url "http://spockframework.org"

    licenses {
      license {
        name "The Apache Software License, Version 2.0"
        url "http://www.apache.org/licenses/LICENSE-2.0.txt"
        distribution "repo"
      }
    }

    scm {
      connection "scm:git:git://github.com/spockframework/spock.git"
      developerConnection "scm:git:ssh://git@github.com/spockframework/spock.git"
      url "http://github.spockframework.org/spock"
    }

    developers {
      developer {
        id "pniederw"
        name "Peter Niederwieser"
        email "peter@pniederw.com"
      }

      developer {
        id "ldaley"
        name "Luke Daley"
        email "ld@ldaley.com"
      }
    }
  }
}

def deployers = []

project.afterEvaluate {
  configure(deployers) {
    pom basePom
  }
}

install {
  deployers << repositories.mavenInstaller
}

uploadArchives {
  deployers << repositories.mavenDeployer {
    snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
      authentication(userName: "pniederw", password: System.getenv("SONATYPE_OSS_PASSWORD"))
    }
    repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
      authentication(userName: "pniederw", password: System.getenv("SONATYPE_OSS_PASSWORD"))
    }
  }
}

def poms = deployers*.pom
def optionalDeps = []
def providedDeps = []
def internalDeps = []

ext {
    modifyPom = { Closure modification ->
      poms.each {
        it.whenConfigured(modification)
      }
    }
    optional = { optionalDeps << it; it }
    provided = { providedDeps << it; it }
    internal = { internalDeps << it; it }
}

modifyPom { pom ->
  optionalDeps.each { dep ->
    pom.dependencies.find { it.artifactId == dep.name }.optional = true
  }
  providedDeps.each { dep ->
    pom.dependencies.find { it.artifactId == dep.name }.scope = "provided"
  }
  internalDeps.each { dep ->
    pom.dependencies.removeAll { it.artifactId == dep.name }
  }
  // no need to publish test dependencies
  pom.dependencies.removeAll { it.scope == "test" }
}

deployers*.beforeDeployment { signing.signPom(it) }
