apply plugin: 'java'
apply plugin: 'maven'

group = 'org.gradle'
version = '1.0-SNAPSHOT'

def repoUrl = "file://localhost/$buildDir/pomRepo/"

repositories {
    project.repo = mavenRepo(urls: repoUrl)
}

configurations {
    snapshot1
    snapshot2
    snapshot3
    archives2
}

jar {
    baseName = 'testproject'
}

task jar2(type: Jar) {
    baseName = 'testproject'
    from sourceSets.main.allJava
}

configure([jar, jar2]) {
    doLast { task ->
        ant.checksum(file: archivePath, property: "checksum_$task.name")
        task.checksum = ant.properties."checksum_$task.name"
    }
}

artifacts {
    archives2 jar2
}

dependencies {
    snapshot1 "org.gradle:testproject:1.0-SNAPSHOT"
    snapshot2 "org.gradle:testproject:1.0-SNAPSHOT"
    snapshot3 "org.gradle:testproject:1.0-SNAPSHOT"
}

uploadArchives2.doFirst {
    // Fix for https://issues.apache.org/jira/browse/IVY-1209
    Thread.sleep 1100
}

delete {
    delete "$gradle.gradleUserHomeDir/cache/${group}/testproject"
}

task retrieveWithEmptyCache(dependsOn: uploadArchives) << {
    ant.checksum(file: configurations.snapshot1.singleFile, property: 'checksum1')
    assert jar.checksum == ant.properties.checksum1
}

task retrieveWithSnapshotInCache(dependsOn: [retrieveWithEmptyCache, uploadArchives2]) << {
    repo.setSnapshotTimeout(Long.MAX_VALUE)
    ant.checksum(file: configurations.snapshot2.singleFile, property: 'checksum2')
    assert jar.checksum == ant.properties.checksum2
}

task retrieveWithTimedOutSnapshotInCache(dependsOn: retrieveWithSnapshotInCache) << {
    repo.setSnapshotTimeout(0)
    ant.checksum(file: configurations.snapshot3.singleFile, property: 'checksum3')
    assert jar2.checksum == ant.properties.checksum3
}

configure([uploadArchives, uploadArchives2]) {
    repositories {
        mavenDeployer {
            repository(url: repoUrl)
        }
    }
}
