/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/5.6.2/userguide/java_library_plugin.html
 */

plugins {
  id 'jacoco'
  id 'signing'
}

repositories {
  jcenter()
  mavenCentral()
}

// hardcoded -- could be redefined via env variables and project properties (see below)
def parameterizedVersion = findProperty('appMapAgentVersion')
def travisVersion = System.getenv("TRAVIS_BRANCH")

def versionLikeRegexp = /^\d+\.\d+.*/
def travisVersionValid = travisVersion && (travisVersion ==~ versionLikeRegexp)

def defaultGitSlug = "applandinc/appmap-java"
def currentGitSlug = System.getenv("TRAVIS_REPO_SLUG") ?: defaultGitSlug

def defaultGroupId      = 'com.appland'
def defaultArtifactId   = 'appmap-annotation'
def publishGroupId      = findProperty('publishGroupId') ?: defaultGroupId
def publishArtifactId   = findProperty('publishArtifactId') ?: defaultArtifactId

def defaultDescription = "Inspect and record the execution of Java for use with App Land"
def parameterizedDescription = findProperty('appMapAgentDescription') ?: defaultDescription


// these two are required by Gradle-Nexus-Publishing
version = parameterizedVersion ?: ( travisVersionValid ? travisVersion : defaultVersion )
group = publishGroupId

compileJava {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

check {
  dependsOn assemble
}
// extra artifacts used in publishing
task sourcesJar(type: Jar) {
  from sourceSets.main.allJava
  classifier = 'sources'
}

// for some reason this block generates empty Javadoc
// which we use as a workaround to bypass javadoc errors issue
javadoc {
  exclude 'com/appland/**'
}
task mockJavadocJar(type: Jar) {
  classifier = 'javadoc'
  from javadoc.destinationDir
}

apply plugin: 'maven-publish'
publishing {
  publications {

    appMapAnnotation(MavenPublication) {
      from components["java"]
      
      // requirements: https://central.sonatype.org/pages/requirements.html

      // 1. coordinates (parameterized)

      groupId publishGroupId
      artifactId publishArtifactId

      // version defined globally

      // 2. artifacts
      // inclusion of javadoc and source jars is Maven-Central requirement

      // artifact jar.output
      artifact sourcesJar
      artifact mockJavadocJar // empty javadoc generated until errors are fixed

      //// the artifacts below added automatically by `java { with... }` block above
      // artifact sourcesJar
      // artifact javadocJar

      // metadata
      // TBD: parameterize more values?

      pom {
        name = "$publishGroupId:$publishArtifactId"
        description = "Inspect and record the execution of Java for use with App Land"
        url = "https://appland.com"

        licenses {
          license {
            name = "MIT"
            url = "https://raw.githubusercontent.com/$currentGitSlug/master/LICENSE.txt"
          }
        }
        developers {
          developer {
            // id = "kgilpin"
            name = "Kevin Gilpin"
            email = "kevin@appland.com"
            organization = "AppLand Inc."
            url = "https://dev.to/kgilpin"
          }
        }
        scm {
          connection = "scm:git:git://github.com/${currentGitSlug}.git"
          developerConnection = "scm:git:ssh://github.com:${currentGitSlug}.git"
          url = "https://github.com/${currentGitSlug}/tree/master"
        }
      }
    }
  }
}

if (project.hasProperty("signingKey")) {
  apply plugin: 'signing'
  signing {
    def signingKey = findProperty("signingKey")
    def signingPassword = findProperty("signingPassword")
    useInMemoryPgpKeys(signingKey, signingPassword)
    sign publishing.publications.appMapAnnotation
  }
}
