apply plugin: 'java'
apply plugin: 'ivy-publish'

group = 'org.gradle.sample'
version = '1.0'

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allJava
}

publishing {
    publications {
        binary(IvyPublication) {
            from components.java
        }
        binaryAndSources(IvyPublication) {
            from components.java
            artifact sourcesJar
        }
    }
    repositories {
        // change URLs to point to your repos, e.g. http://my.org/repo
        ivy {
            name "external"
            url "$buildDir/repos/external"
        }
        ivy {
            name "internal"
            url "$buildDir/repos/internal"
        }
    }
}

tasks.withType(PublishToIvyRepository) {
    onlyIf {
        if (repository == publishing.repositories.external)
            return publication == publishing.publications.binary
        if (repository == publishing.repositories.internal)
            return publication == publishing.publications.binaryAndSources
        return false
    }
}

task publishToExternalRepository {
    group "publishing"
    description "Publishes all Ivy publications to Ivy repository 'external'."
    dependsOn tasks.withType(PublishToIvyRepository).matching {
        it.repository == publishing.repositories.external
    }
}
task publishToInternalRepository {
    group "publishing"
    description "Publishes all Ivy publications to Ivy repository 'internal'."
    dependsOn tasks.withType(PublishToIvyRepository).matching {
        it.repository == publishing.repositories.internal
    }
}
