buildscript {
    repositories {
        maven {
        credentials {
        username "${mavenUser}"
        password "${mavenPassword}"
        }
            url "https://devstore.*****.com/artifactory/jcenter"
        }
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.11.0"
        classpath "de.undercouch:gradle-download-task:3.4.3" // Download task
    }
}

apply plugin: "groovy"
apply plugin: "com.jfrog.artifactory"
apply plugin: "maven-publish"
apply plugin: "de.undercouch.download" //Download task

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    maven {
                credentials {
        username "${mavenUser}"
        password "${mavenPassword}"
        }
        url "https://devstore.******.com/artifactory/jcenter"
    }
}

configurations.all {
    canBeResolved = true
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
    resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
}

ext.environment = 'production'
if(project.hasProperty('envCode')) {
    ext.environment = envCode
}
if(environment.equalsIgnoreCase('test')) {
    ext.artifactory_contextUrl = artifactory_TESTcontextUrl
}
if(environment.equalsIgnoreCase('acceptance')) {
    ext.artifactory_contextUrl = artifactory_ACCEPTANCEcontextUrl
}

ext.artifactory_user = ''
ext.artifactory_pwd = ''
if(project.hasProperty('artifactoryUser')) {
    ext.artifactory_user = artifactoryUser
}
if(project.hasProperty('artifactoryPwd')) {
    ext.artifactory_pwd = artifactoryPwd
}

group = currentGroup
version = currentVersion

sourceSets.main.groovy.srcDirs = ["../code/src"]
processResources {
    from "../code/src/main/resources"
}

dependencies {
    implementation localGroovy()
    implementation gradleApi()
    implementation group: 'org.codehaus.groovy.modules.http-builder', name: 'http-builder', version: '0.7.2'
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = "${artifactory_repo}"
            username = "${artifactory_user}"
            password = "${artifactory_pwd}"
        }
        defaults {
            publishArtifacts = true
            publishPom = true
            publishIvy = false
            publishBuildInfo = true
            publishArtifacts = true
            publications('mavenJava')
        }
    }

    resolve {
        repository {
            repoKey = "${artifactory_repo}"
            username = "${artifactory_user}"
            password = "${artifactory_pwd}"
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId group
            version version
            artifactId project.name
            from components.java

            pom.withXml {
                def resolvedArtifacts = configurations.default.getResolvedConfiguration().getResolvedArtifacts()
                resolvedArtifacts.each {
                    if(it.moduleVersion.id.group.startsWith("com.*****")) {
                        asNode().dependencies.'*'.findAll() { dep->
                            it.moduleVersion.id.name == dep.artifactId.text()
                        }.each () { dep->
                            dep.version*.value = it.moduleVersion.id.version
                        }
                    }
                }
            }
        }
    }
}
tasks.withType(GenerateModuleMetadata) {
    enabled = false
}