apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
apply plugin: 'java'

if (!project.hasProperty("artifactory")) return

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    new FileInputStream(secretPropsFile).withCloseable { is -> p.load(is) }
    p.each { name, value -> ext[name] = value }
}

task sourceJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier = "sources"
}

afterEvaluate {
    publishing {
        publications {
            jar(MavenPublication) {
                def projectGroupId = project.ext.get("PUBLISH_GROUP_ID") as String
                def projectArtifactId = project.ext.get("PUBLISH_ARTIFACT_ID") as String
                def projectVersionId = project.ext.get("PUBLISH_VERSION") as String

                project.artifactoryPublish.skip = false
                project.tasks.artifactoryPublish.dependsOn("assemble")
                project.tasks.artifactoryPublish.dependsOn("generatePomFileForJarPublication")
                println("$project.name will be published as $projectArtifactId : $projectVersionId")

                groupId projectGroupId
                artifactId projectArtifactId
                version = projectVersionId
                artifact(sourcesJar)
                artifact("$buildDir/libs/${project.getName()}-${projectVersionId}.jar")

                pom.withXml {
                    def dependencies = asNode().appendNode('dependencies')
                    configurations.implementation.allDependencies.each {
                        if (it.name != "unspecified" && it.version != "unspecified") {
                            executeAppendNode(it, dependencies)
                        }
                    }
                    configurations.api.allDependencies.each {
                        if (it.name != "unspecified" && it.version != "unspecified") {
                            executeAppendNode(it, dependencies)
                        }
                    }
                }
            }
        }
    }
}

private void executeAppendNode(Dependency it, dependencies) {
    def group = it.group
    def name = it.name
    def version = it.version
    if (it.group == "clickstream-android") {
        group = groupId
    }
    def dependency = dependencies.appendNode('dependency')
    dependency.appendNode('groupId', group)
    dependency.appendNode('artifactId', name)
    dependency.appendNode('version', version)
}

project.artifactoryPublish.skip = false

artifactory {
    publish {
        println("artifactory repoKey: ${ext['repoKey']}")
        println("artifactory username: ${ext['username']}")
        println("artifactory password: ${ext['password']}")
        contextUrl = ext['contextUrl']

        repository {
            repoKey = ext['repoKey']
            username = ext['username']
            password = ext['password']
        }
        defaults {
            publications('jar')
            publishArtifacts = true
            publishPom = true
        }
    }
}

java {
    withSourcesJar()
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}