
allprojects {
    apply plugin: 'java'
}

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

dependencies {
   compile 'junit:junit:4.7', project(':subproject')
}

ant {
    importBuild "$projectDir/build.xml"
    buildDir = owner.buildDir.toString()
}

String ivyPattern = '/[module]/[revision]/ivy.xml'
String artifactPattern = '/[module]/[revision]/[artifact](.[ext])'
File localPublicationsDir = file("$buildDir/repo")

repositories {
    add(new org.apache.ivy.plugins.resolver.FileSystemResolver()) {
        name = 'repo'
        validate = false
        addIvyPattern(localPublicationsDir.absolutePath + ivyPattern)
        addArtifactPattern(localPublicationsDir.absolutePath + artifactPattern)
    }
    mavenCentral()
}

task sourceJar(type: Jar) {
    baseName = 'ivypublishSource'
    from sourceSets.main.java
    classifier = 'src'
}

artifacts {
    archives sourceJar
}

uploadArchives {
    uploadDescriptor = true
    repositories {
        add project.repositories.repo
    }
    doLast {
        File repoDir = new File("$buildDir/repo/ivypublish/1.0/")
        assert ["ivypublish.jar", "ivy.xml", "ivypublishSource.jar"] as Set == repoDir.listFiles().collect { it.name } as Set
        assert jar.archivePath.size() == new File(repoDir, 'ivypublish.jar').size()
        sourceJar.archivePath.size() == new File(repoDir, 'ivypublishSource.jar').size()

        def ns = new groovy.xml.Namespace("http://ant.apache.org/ivy/maven", 'm')
        def root = new XmlParser().parse(new File(repoDir, 'ivy.xml'))
        assert root.publications.artifact.find { it.@name == 'ivypublishSource' }.attribute(ns.classifier) == 'src'
        assert (root.configurations.conf.collect { it.@name } as Set) == ['archives', 'compile', 'default', 'runtime','testCompile', 'testRuntime']  as Set
        assert root.dependencies.dependency.find { it.@org == 'junit' }.attributes() == [org: 'junit', name: 'junit', rev: '4.7', conf: 'compile->default']
        assert root.dependencies.dependency.find { it.@org == 'ivypublish' }.attributes() == [org: 'ivypublish', name: 'subproject', rev: 'unspecified',
                conf: 'compile->default']
    }
}

// It would be nice to test if the stuff published by Gradle can be read by a native ivy ant script.
// The tasks below are supposed to do this. But due to a serious Ivy bugs we can't do it at the
// moment (e.g. https://issues.apache.org/jira/browse/IVY-1110). As soon as this bug is fixed, we
// should uncomment the below.

//ivyConfigure.doLast {
//    def cacheDir = ant.properties['ivy.cache.dir'] + '/org.gradle.test'
//    println cacheDir
//    ant.delete(dir: cacheDir, verbose: true)
//}
//
//retrieveFromAnt {
//    dependsOn uploadArchives
//    doLast {
//        File retrieveDir = new File("$buildDir/antRetrieve")
//        Assert.assertEquals(retrieveDir.listFiles().collect { it.name } as Set,
//                ["ivypublish.jar", "ivypublishSource.jar"] as Set)
//        Assert.assertEquals(jar.archivePath.size(), new File(retrieveDir, 'ivypublish.jar').size())
//        Assert.assertEquals(sourceJar.archivePath.size(), new File(retrieveDir, 'ivypublishSource.jar').size())
//    }
//}





