apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'

apply plugin: 'com.jfrog.bintray'

def buildDirectory = "$buildDir/libs"
def pomFilePath = "$buildDirectory/$project.artifact-$project.version" + ".pom"

if (JavaVersion.current().isJava8Compatible()) {
  allprojects {
    tasks.withType(Javadoc) {
      options.addStringOption('Xdoclint:none', '-quiet')
    }
  }
}

//https://maven.apache.org/guides/mini/guide-central-repository-upload.html
//http://central.sonatype.org/pages/requirements.html#a-complete-example-pom
//https://medium.com/@vyarus/the-hard-way-to-maven-central-c9e16d163acc#.t0evkiaet
def pomConfig = {
  licenses {
    license {
      name "The Apache Software License, Version 2.0"
      url "http://www.apache.org/licenses/LICENSE-2.0.txt"
      distribution "repo"
    }
  }
  developers {
    developer {
      name "$project.fullname"
      email "$project.email"
      organization "$project.organization"
      organizationUrl "$project.organizationUrl"
    }
  }
  scm {
    connection "$project.website"
    developerConnection "$project.userwebsite"
    url "$project.website"
  }
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      groupId "$project.group"
      artifactId "$project.artifact"
      version "$project.version"
      from components.java
      artifact sourcesJar
      artifact javadocJar
      pom.withXml {
        def pomNode = asNode()
        //Clean up useless stuff in order to organize it
        pomNode.remove(pomNode.get("packaging"))
        //Add the information
        pomNode.appendNode('packaging', 'jar')
        pomNode.appendNode('name', "$project.name")
        pomNode.appendNode('description', "$project.description")
        pomNode.appendNode('url', "$project.website")
        pomNode.children().last() + pomConfig
      }
      artifact "$pomFilePath"
    }
  }
}

bintray {
  user = System.getenv('BINTRAY_USERNAME')
  key = System.getenv('BINTRAY_KEY')
  publications = ['mavenJava']
  pkg {
    repo = "$project.repository"
    name = "$project.codename"
    desc = "$project.description"
    websiteUrl = "$project.website"
    issueTrackerUrl = "$project.issues"
    licenses = ["$project.license"]
    vcsUrl = "$project.website"
    labels = ['android', 'java', 'arrow']
    //noinspection GroovyAssignabilityCheck
    version {
      name = "$project.version"
      desc = "$project.description"
      released = new Date()
      vcsTag = "$project.website"
      mavenCentralSync {
        sync = true
        user = System.getenv('OSS_USERNAME')
        password = System.getenv('OSS_KEY')
        close = '1'
      }
    }
  }
}

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

task javadocJar(type: Jar, dependsOn: javadoc) {
  classifier 'javadoc'
  //noinspection GrUnresolvedAccess
  from javadoc.destinationDir
}

artifacts {
  archives sourcesJar
  archives javadocJar
}

task installToLocalRepo(dependsOn: ['publishMavenJavaPublicationToMavenLocal']) {}
model {
  tasks.generatePomFileForMavenJavaPublication {
    pomDir = file("$pomFilePath")
    destination = file("$pomFilePath")
  }
}