apply plugin: "java"
apply plugin: "maven-publish"
apply plugin: "signing"

group = "nl.javadude"
version = "0.10.1-SNAPSHOT"

repositories {
  mavenCentral()
  mavenLocal()
}

configurations {
  compile {
    transitive = false
  }
  pom
}

def bouncycastleVersion = "1.50"

dependencies {
  compile "org.slf4j:slf4j-api:1.7.7"
  compile "org.bouncycastle:bcprov-jdk15on:$bouncycastleVersion"
  compile "org.bouncycastle:bcpkix-jdk15on:$bouncycastleVersion"
  compile "com.jcraft:jzlib:1.1.3"

  testCompile "junit:junit:4.11"
  testCompile "org.mockito:mockito-core:1.9.5"
  testCompile "org.apache.sshd:sshd-core:0.11.0"
  testRuntime "ch.qos.logback:logback-classic:1.1.2"
}

task javadocJar(type: Jar) {
  classifier = 'javadoc'
  from javadoc
}

task sourcesJar(type: Jar) {
  classifier = 'sources'
  from sourceSets.main.allSource
}

task generatePom(type: GenerateMavenPom) {
  destination = file("$buildDir/generated-pom.xml")
}

artifacts {
  archives javadocJar, sourcesJar
  pom generatePom.destination
}

signing {
  sign configurations.archives
}

task signPom(type: Sign) {
  sign configurations.pom
}

def getSignatureFiles = {
  def allFiles = project.tasks.signArchives.signatureFiles.collect { it }
  def signedSources = allFiles.find { it.name.contains('-sources') }
  def signedJavadoc = allFiles.find { it.name.contains('-javadoc') }
  def signedJar = (allFiles - [signedSources, signedJavadoc])[0]
  return [
    [archive: signedSources, classifier: 'sources', extension: 'jar.asc'],
    [archive: signedJavadoc, classifier: 'javadoc', extension: 'jar.asc'],
    [archive: signedJar,     classifier: null,      extension: 'jar.asc']
  ]
}

def getPomSignature = {
    return project.tasks.signPom.signatureFiles.collect{it}[0]
}


publishing {
  publications {
    gpgJars(MavenPublication) {
      getSignatureFiles().each {signature ->
        artifact (signature.archive) {
          classifier = signature.classifier
          extension  = signature.extension
        }
      }
    }
    gpgPom(MavenPublication) {
      artifact(getPomSignature()) {
        classifier = null
        extension = "pom.asc"
      }
    }
    maven(MavenPublication) {
      from components.java
      artifact (javadocJar) {
        classifier = 'javadoc'
      }

      artifact (sourcesJar) {
        classifier = 'sources'
      }

      pom.withXml {
        asNode().children().last() + {
          resolveStrategy = Closure.DELEGATE_FIRST
          name "sshj"
          description "SSHv2 library for Java"
          url "https://github.com/hierynomus/sshj"
          inceptionYear "2009"

          issueManagement {
            system "github"
            url "https://github.com/hierynomus/sshj/issues"
          }

          scm {
            connection "scm:git:git://github.com/hierynomus/sshj.git"
            developerConnection "scm:git:git@github.com:hierynomus/sshj.git"
            url "https://github.com/hierynomus/sshj.git"
          }

          licenses {
            license {
              name "Apache 2"
              url "http://www.apache.org/licenses/LICENSE-2.0.txt"
              distribution "repo"
            }
          }

          developers {
            developer {
              id "hierynomus"
              name "Jeroen van Erp"
              email "jeroen@javadude.nl"
              roles {
                role "Lead developer"
              }
            }
            developer {
              id "shikhar"
              name "Shikhar Bhushan"
              email "shikhar@schmizz.net"
              url "http://schmizz.net"
              roles {
                role "Previous lead developer"
              }
            }
            developer {
              id "iterate"
              name "David Kocher"
              email "dkocher@iterate.ch"
              organization "iterage GmbH"
              organizationUrl "https://iterate.ch"
              roles {
                role "Developer"
              }
            }
          }
        }
      }
    }
  }
  repositories {
    maven {
      url "file:/${project.projectDir}/artifacts"
    }
  }
}

project.afterEvaluate { p ->
  p.tasks.publishGpgPomPublicationToMavenRepository.dependsOn("generatePom", "signPom")
}

generatePom.configure {
  pom = publishing.publications.getByName("maven").pom
}
