// Add some metadata for MavenCentral
def customizePom(pom, project) { pom.withXml {
  asNode().appendNode("name", project.description)
  asNode().appendNode("description", project.description + " – Open-Source Data-Mining Framework with Index Acceleration")
  asNode().children().last() + {
    url 'https://elki-project.github.io/'
    resolveStrategy = Closure.DELEGATE_FIRST
    issueManagement {
      system 'GitHub'
      url 'https://github.com/elki-project/elki/issues'
    }
    licenses {
      license {
        name 'GNU Affero General Public License (AGPL) version 3.0'
        url 'http://www.gnu.org/licenses/agpl-3.0.txt'
      }
    }
    scm {
      url 'https://github.com/elki-project/elki'
      connection 'scm:git:git://github.com/elki-project/elki.git'
      developerConnection 'scm:git:ssh://git@github.com/elki-project/elki.git'
    }
    organization {
      name "ELKI Development Team"
      url "https://elki-project.github.io/team"
    }
    developers {
      developer { name "Erich Schubert" }
      developer { name "Arthur Zimek" }
    }
  }
} }

subprojects {
  jar {
    inputs.property("moduleName", project.name.replace("-","."))
    manifest {
      attributes('Automatic-Module-Name': project.name.replace("-","."))
    }
  }
  processResources {
    from("$rootDir/elki/src/main/resources/META-INF") {
      include "NOTICE"
      into "META-INF"
      duplicatesStrategy DuplicatesStrategy.INCLUDE
    }
    from("$rootDir/elki/src/main/resources/elki/LICENSE.txt") {
      rename "\\.txt\$", ""
      into "license"
      duplicatesStrategy DuplicatesStrategy.INCLUDE
    }
  }
  // Build source jars:
  task sourceJar(type: Jar) {
    archiveClassifier = 'sources'
    from sourceSets.main.allSource
  }
  artifacts.archives sourceJar
  // Build javadoc jars:
  task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier = 'javadoc'
    from javadoc.destinationDir
  }
  artifacts.archives javadocJar
  // Collect dependencies -- FIXME: does not work in Gradle 7 anymore
  tasks.register('collectDependencies', Sync) {
    description = "Collect dependencies"
    group = "Build"
    from configurations.runtimeClasspath
    into "$project.buildDir/libs/lib"
  }
  jar.dependsOn collectDependencies
}

configure(subprojects.findAll { isSnapshot || !project.hasProperty("signing.gnupg.keyName") || it.name != 'elki-bundle'}) {
  publishing {
    publications {
      maven(MavenPublication) {
        from components.java
        artifact(sourceJar)  { classifier = 'sources' }
        artifact(javadocJar) { classifier = 'javadoc' }
        customizePom(pom, project)
      }
    }
    // For publishing to maven central.
    // Add the sonatype data to gradle.properties!
    if (project.hasProperty("sonatypeUsername")) { repositories {
      maven {
        url "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2"
        credentials {
          username sonatypeUsername
          password sonatypePassword
        }
      }
    } }
  }
}

