apply plugin: 'maven'

configurations {
    publishCompile
    publishRuntime
    compile {
        extendsFrom publishCompile
    }
}

task sourceJar(type: Jar) {
    classifier = 'sources'
    from { sourceSets.main.java.srcDirs + sourceSets.main.groovy.srcDirs }
}

task generatePom {
    pomFile = new File(temporaryDir, 'pom.xml')
    doLast {
        dependencies {
            configurations.publishCompile.getAllDependencies(ProjectDependency).each {
                publishRuntime "org.gradle:gradle-${it.dependencyProject.name}:${version}"
            }
            configurations.publishCompile.getAllDependencies(ExternalModuleDependency).each {
                publishRuntime it
            }
        }

        def localDeployer = install.repositories.mavenInstaller

        configure(localDeployer) {
            pom.scopeMappings.mappings.clear()
            pom.scopeMappings.addMapping(300, configurations.publishRuntime, Conf2ScopeMappingContainer.RUNTIME)
            pom.groupId = project.group
            pom.artifactId = archivesBaseName
            pom.version = version
            pom.writeTo(pomFile)
        }

    }
}

artifacts {
    publishRuntime jar
    publishRuntime sourceJar
    publishRuntime new org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact(archivesBaseName, 'pom', 'pom', null, new Date(), generatePom.pomFile, generatePom)
}

uploadArchives {
    configuration = configurations.publishRuntime
    dependsOn generatePom
    uploadDescriptor = false
    doFirst {
        repositories {
            ivy {
                name = 'gradleLibs'
                artifactPattern "${version.libsUrl}/${project.group.replaceAll('\\.', '/')}/${archivesBaseName}/[revision]/[artifact]-[revision](-[classifier]).[ext]"
                userName = artifactoryUserName
                password = artifactoryUserPassword
            }
        }
    }
}

task publishLocalArchives(type: Upload) {
    configuration = configurations.publishRuntime
    dependsOn generatePom
    uploadDescriptor = false
    repositories.add(new org.apache.ivy.plugins.resolver.FileSystemResolver()) {
        name = 'gradleReleases'
        addArtifactPattern("${rootProject.file('build/repo')}/${project.group.replaceAll('\\.', '/')}/${archivesBaseName}/[revision]/[artifact]-[revision](-[classifier]).[ext]" as String)
    }
}