apply plugin: 'maven-publish'
apply plugin: 'signing'

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

task('javadocJar', type: Jar, dependsOn: javadoc) {
  archiveClassifier = 'javadoc'
  from javadoc.getDestinationDir()
}
tasks.withType(Javadoc).configureEach {
  javadocTool = javaToolchains.javadocToolFor {
    // Use latest JDK for javadoc generation
    languageVersion = JavaLanguageVersion.of(17)
  }
}

javadoc {
  // Specify the Java version that the project will use
  options.addStringOption('-release', "8")
}
artifacts {
  archives sourcesJar, javadocJar
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java

      artifact sourcesJar
      artifact javadocJar

      groupId = GROUP
      artifactId = POM_ARTIFACT_ID
      version = getVersionName()

      pom {
        name = POM_NAME
        packaging = POM_PACKAGING
        description = POM_DESCRIPTION
        url = POM_URL

        licenses {
          license {
            name = POM_LICENCE_NAME
            url = POM_LICENCE_URL
            distribution = POM_LICENCE_DIST
          }
        }

        developers {
          developer {
            id = POM_DEVELOPER_ID
            name = POM_DEVELOPER_NAME
            email = POM_DEVELOPER_EMAIL
          }
        }

        scm {
          url = POM_SCM_URL
          connection = POM_SCM_CONNECTION
          developerConnection = POM_SCM_DEV_CONNECTION
        }
      }
    }
  }
}

nexusPublishing {
  repositories {
    sonatype {
      nexusUrl.set(uri('https://ossrh-staging-api.central.sonatype.com/service/local/'))
      snapshotRepositoryUrl.set(uri('https://central.sonatype.com/repository/maven-snapshots/'))
      username.set(System.getenv("MAVEN_USERNAME"))
      password.set(System.getenv("MAVEN_PASSWORD"))
    }
  }
}

signing {
  def signingKey = System.getenv("SIGNING_KEY")
  def signingPassword = System.getenv("SIGNING_PASSWORD")
  useInMemoryPgpKeys(signingKey, signingPassword)

  sign publishing.publications.mavenJava
}

javadoc {
  if(JavaVersion.current().isJava9Compatible()) {
    options.addBooleanOption('html5', true)
  }
}

tasks.named('publish').configure {
  dependsOn tasks.named('assemble')
}


