// configuration of the Maven artifacts
apply plugin: 'maven-publish'

// add javadoc/source jar tasks as artifacts
artifacts {
  archives androidSourcesJar, androidJavadocsJar
}

publishing {
  publications {
    dist(MavenPublication) {
      groupId project.group
      artifactId project.name
      version project.version
      artifact "${project.buildDir}/outputs/aar/${project.name}-release.aar"
      artifact androidJavadocsJar {
        classifier 'javadoc'
      }
      artifact androidSourcesJar {
        classifier 'source'
      }

      pom.withXml {
        def root = asNode()
        root.appendNode('name', project.name)
        root.appendNode('description', project.project_description)
        root.appendNode('url', project.project_url)

        def issues = root.appendNode('issueManagement')
        issues.appendNode('system', 'github')
        issues.appendNode('url', project.project_issues_url)

        def scm = root.appendNode('scm')
        scm.appendNode('url', project.project_scm)
        scm.appendNode('connection', project.project_connection)
        scm.appendNode('developerConnection', project.project_dev_connection)

        def license = root.appendNode('licenses').appendNode('license');
        license.appendNode('name', project.project_licenses)
        license.appendNode('url', project.project_license_url)
        license.appendNode('distribution', 'repo')

        def dev = root.appendNode('developers').appendNode('developer');
        dev.appendNode('id', project.project_vendor)
        dev.appendNode('name', project.project_vendor_name)
        dev.appendNode('organization', project.project_vendor_name)
        dev.appendNode('organizationUrl', project.project_vendor_url)

        def dependenciesNode = asNode().appendNode('dependencies')

        //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
        configurations.compile.allDependencies.each {
          if(it.group != null && (it.name != null || "unspecified".equals(it.name))
              && it.version != null) {

            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)

            if (it.name == 'groovy' && it.group == 'org.codehaus.groovy') {
              dependencyNode.appendNode('classifier', 'grooid')
            }

            if (it.group == 'org.spockframework') {
              dependencyNode.appendNode('exclusions').appendNode('exclusion').with {
                appendNode('groupId', '*')
                appendNode('artifactId', '*')
              }
            }
          }
        }
      }
    }
  }
}
