buildscript {
    repositories {
        mavenLocal()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.3.1"

        if (project.hasProperty('coverage')) {
            classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:1.0.2'
            classpath "org.sonarqube.gradle:gradle-sonarqube-plugin:1.0"
        }
    }
}

apply plugin: 'com.jfrog.bintray'
apply plugin: 'groovy'
apply plugin: 'maven-publish'

group = 'com.ofg'
description = 'Core of UpToDate plugin'
version = '1.6.4-SNAPSHOT'
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

dependencies {
    compile gradleApi()
    compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.2'

    testCompile 'junit:junit:4.11'
    testCompile("org.spockframework:spock-core:${getSpockVersion()}") {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile 'com.github.tomakehurst:wiremock:1.47'
    testCompile 'com.google.guava:guava:18.0'
    testRuntime 'cglib:cglib-nodep:2.2.2'
    testRuntime 'org.objenesis:objenesis:1.2'
}

test {
    testLogging {
        exceptionFormat = 'full'
    }
}

String getSpockVersion() {
    String spockGroovyVersion = GroovySystem.version.replaceAll(/\.\d+$/,'')
    return Double.valueOf(gradle.gradleVersion) < 2.0 ? "0.7-groovy-1.8" : "1.0-groovy-$spockGroovyVersion"
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.12'
}

bintrayUpload.dependsOn 'build'

publishing {
    publications {
        plugin(MavenPublication) {
            from components.java
            artifact sourcesJar
            artifact javadocJar
        }
    }
}

bintray {
    user = System.properties['bintrayUser']
    key = System.properties['bintrayKey']
    publications = ['plugin']
    pkg {
        repo = 'uptodate-gradle-plugin'
        userOrg = '4finance'
        name = 'uptodate-gradle-plugin'
        desc = 'Gradle plugin that tells you what libs have new versions in Maven Central / JCenter'
        licenses = ['Apache-2.0']
        labels = ['gradle']
        version {
            attributes = ['gradle-plugin': 'com.ofg.uptodate:com.ofg:uptodate-gradle-plugin']
        }
    }
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: groovydoc) {
    classifier = 'javadoc'
    from groovydoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

if (project.hasProperty('coverage')) {
    apply plugin: 'jacoco'
    apply plugin: 'com.github.kt3k.coveralls'
    apply plugin: 'org.sonarqube'

    jacoco {
        toolVersion = '0.7.1.201405082137'
        excludes = ['com.ofg.uptodate.dependency.maven.*']
    }

    jacocoTestReport {
        reports {
            xml.enabled = true // coveralls plugin depends on xml format report
            html.enabled = true
        }
    }
    test {
        ignoreFailures = true
    }
    sonarqube {
        properties {
            property "sonar.groovy.jacoco.reportPath", "$buildDir/jacoco/test.exec"
        }
    }
}
