/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


// Maven publications and configuration.
//
// the 'published' list contains an explicit list of all projects
// which should be published to Maven repositories.

configure(rootProject) {
  ext {
    published = [
        ":solr:api",
        ":solr:cross-dc-manager",
        ":solr:core",
        ":solr:solrj",
        ":solr:solrj-streaming",
        ":solr:solrj-zookeeper",
        ":solr:prometheus-exporter",
        ":solr:test-framework",
    ] + project(":solr:modules").getChildProjects().values().path

    apacheNexusSnapshots = "https://repository.apache.org/content/repositories/snapshots"
  }
}

configure(subprojects.findAll { it.path in rootProject.published }) { prj ->
  apply plugin: 'maven-publish'
  apply plugin: 'signing'

  publishing {
    repositories {
      maven {
        name = "ApacheSnapshots"
        url = apacheNexusSnapshots

        credentials {
          def nexusUserName = rootProject.propertyOrDefault('asfNexusUsername', null)
          def nexusPwd = rootProject.propertyOrDefault('asfNexusPassword', null)
          if (nexusUserName && nexusPwd) {
            username nexusUserName
            password nexusPwd
          }
        }
      }
    }
  }

  // Do not generate gradle metadata files.
  tasks.withType(GenerateModuleMetadata) {
    enabled = false
  }

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

    task javadocJar(type: Jar, dependsOn: javadoc) {
      archiveClassifier = 'javadoc'
      from javadoc.destinationDir
    }

    // Add aliases of convention tasks with shorter names.
    task mavenToApacheSnapshots() {
      group "Publishing"
      description "Publish Maven JARs and POMs to Apache Snapshots repository: ${apacheNexusSnapshots}"

      dependsOn "publishJarsPublicationToApacheSnapshotsRepository"
    }

    publishing {
      def configurePom = {
        name = "Apache Solr (module: ${project.name})"
        description = name
        url = 'https://solr.apache.org/'

        licenses {
          license {
            name = 'Apache 2'
            url = 'https://apache.org/licenses/LICENSE-2.0.txt'
          }
        }

        inceptionYear = "2006"

        issueManagement {
          system = "JIRA"
          url = "https://issues.apache.org/jira/browse/SOLR"
        }

        ciManagement {
          system = "Jenkins"
          url = "https://builds.apache.org/job/Solr/"
        }

        mailingLists {
          mailingList {
            name = "Solr User List"
            subscribe = "users-subscribe@solr.apache.org"
            unsubscribe = "users-unsubscribe@solr.apache.org"
            archive = "https://lists.apache.org/list.html?users@solr.apache.org"
          }

          mailingList {
            name = "Solr Developer List"
            subscribe = "dev-subscribe@solr.apache.org"
            unsubscribe = "dev-unsubscribe@solr.apache.org"
            archive = "https://lists.apache.org/list.html?dev@solr.apache.org"
          }

          mailingList {
            name = "Solr Commits List"
            subscribe = "commits-subscribe@solr.apache.org"
            unsubscribe = "commits-unsubscribe@solr.apache.org"
            archive = "https://lists.apache.org/list.html?commits@solr.apache.org"
          }
        }

        scm {
          connection = 'scm:git:https://gitbox.apache.org/repos/asf/solr.git'
          developerConnection = 'scm:git:https://gitbox.apache.org/repos/asf/solr.git'
          url = 'https://gitbox.apache.org/repos/asf?p=solr.git'
        }
      }

      publications {
        jars(MavenPublication) {
          from components.java
          groupId = project.group
          artifactId = project.archivesBaseName

          artifact sourcesJar
          artifact javadocJar

          pom(configurePom)
        }
      }
    }

    // This moves some configuration after all the scripts of all projects
    // have been evaluated. This is required because we compute some aspects dynamically and
    // maven pom does not accept lazy property providers (so everything must
    // be in its final form).
    //
    // In theory project.afterEvaluate closure should also work but for some reason
    // it fired earlier; don't know whether it's a bug
    // in gradle or just complex relationships between lazy collection hooks.
    gradle.projectsEvaluated {
      signing {
        if (project(":solr:distribution").ext.useGpgForSigning) {
          useGpgCmd()
        }
        required { project(":solr:distribution").ext.withSignedArtifacts }
        sign publishing.publications.jars
      }
    }
  }
}
