apply plugin: "idea"
apply plugin: "java"
apply plugin: "groovy"
apply plugin: "maven"
apply plugin: "signing"
apply plugin: "jacoco"

ext {
    versionProperties = new Properties()
    versionProperties.load(file("src/main/resources/version.properties").newReader())
    projectVersion = versionProperties["version"]
    sourceEncoding = "UTF-8"
    spockVersion = "1.1-groovy-2.4"
    groovyVersion = "2.4.11"
    junitVersion = "4.12"
    cglibVersion = "3.2.5"
    objenesisVersion = "2.5.1"
    jaxbVersion = "2.2.11"
    betamaxVersion = "2.0.1"
    libs = [
            jaxb     : "org.glassfish.jaxb:jaxb-runtime:$jaxbVersion",
            junit    : "junit:junit:$junitVersion",
            groovy   : "org.codehaus.groovy:groovy-all:$groovyVersion",
            spock    : "org.spockframework:spock-core:$spockVersion",
            cglib    : "cglib:cglib-nodep:$cglibVersion",
            objenesis: "org.objenesis:objenesis:$objenesisVersion",
            betamax  : "software.betamax:betamax-junit:$betamaxVersion"
    ]
    buildTime = new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
    sharedManifest = manifest {
        attributes(
                "Implementation-Title": "Twingly Search API Java",
                "Implementation-Version": projectVersion,
                "Build-Time": buildTime
        )
    }
}

group = "com.twingly"
version = ext.projectVersion
sourceCompatibility = 1.7
targetCompatibility = 1.7

def isReleaseBuild
if (hasProperty("release")) {
    isReleaseBuild = true
} else {
    isReleaseBuild = false
}

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

dependencies {
    compile libs.jaxb
    testCompile libs.betamax
    testCompile libs.junit
    testCompile libs.groovy
    testCompile libs.spock
    testRuntime libs.cglib
    testRuntime libs.objenesis
}

jacoco {
    toolVersion = "0.7.9"
}

test {
    testLogging.showStandardStreams = true
    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpDir = file("$buildDir/jacoco/classpathdumps")
    }
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}

javadoc {
    options.memberLevel = JavadocMemberLevel.PROTECTED
    classpath = configurations.compile
}

//create a single Jar with all dependencies
task generateOneJar(type: Jar) {
    baseName = project.name + '-all'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

tasks.withType(Jar) { // includes War and Ear
    manifest = project.manifest {
        from sharedManifest
    }
}

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

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

tasks.withType(JavaCompile.class) {
    options.deprecation = false
    options.warnings = false
    options.encoding = sourceEncoding
    options.compilerArgs = ["-Xmaxerrs", "20", "-Xmaxwarns", "0", "-Xlint:all"]
}

artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
}

if (isReleaseBuild) {
    signing {
        sign configurations.archives
    }
} else {
    task signArchives {
        // do nothing
    }
}

uploadArchives {
    repositories {
        if (!isReleaseBuild) {
            mavenLocal()
        } else {
            mavenDeployer {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                    authentication(userName: sonatypeUsername, password: sonatypePassword)
                }

                pom.project {
                    name "twingly-search"
                    packaging "jar"
                    description "Client for Twingly Search API."
                    url "https://app.twingly.com/blog_search?tab=documentation"

                    scm {
                        connection "scm:git@github.com:twingly/twingly-search-api-java.git"
                        developerConnection "scm:git@github.com:twingly/twingly-search-api-java.git"
                        url "scm:git@github.com:twingly/twingly-search-api-java.git"
                    }

                    licenses {
                        license {
                            name "MIT License"
                            url "http://www.opensource.org/licenses/mit-license.php"
                            distribution "repo"
                        }
                    }

                    developers {
                        developer {
                            id "twingly"
                            name "Twingly AB"
                            email "support@twingly.com"
                        }
                    }
                }
            }
        }
    }
}

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