buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:3.1.0'
        classpath 'com.netflix.nebula:gradle-aggregate-javadocs-plugin:2.2.+'
        classpath 'com.github.nwillc:vplugin:1.+'
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.5.3"
        classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1'
    }
}

plugins {
    id "com.github.hierynomus.license" version "0.12.1"
    id 'me.champeau.gradle.jmh' version '0.3.0'
    id "com.jfrog.bintray" version "1.7"
    id 'net.researchgate.release' version '2.3.4'
    id 'com.palantir.git-version' version '0.5.1'
}

apply plugin: 'me.champeau.gradle.jmh' // performance benchmarking
apply plugin: 'nebula-aggregate-javadocs'
apply plugin: 'io.codearte.nexus-staging'

ext {
    guavaVersion = '20.0'
}

if (hasProperty("snapshot") && gitVersion().toString().contentEquals("unspecified")) {
    throw new GradleException('snapshot version `unspecified');
}

def gitVsn = gitVersion()

println("Git version ${gitVsn}")

release {
    tagTemplate = 'v${version}'
    failOnPublishNeeded = false
}

allprojects {
    apply plugin: 'idea'
    apply plugin: 'license'
    apply plugin: 'com.github.nwillc.vplugin'

    tasks.withType(JavaCompile) {
        options.compilerArgs << "-parameters"
    }

    license {
        header rootProject.file('LICENSE-HEADER')
    }

    repositories {
        mavenCentral()
    }

}

subprojects {
    version = rootProject.hasProperty('snapshot') ? gitVsn : project.version

    apply plugin: 'java'
    apply plugin: 'osgi'
    apply plugin: 'java-library-distribution'
    apply plugin: 'biz.aQute.bnd.builder'
    apply plugin: 'com.jfrog.bintray'
    apply plugin: 'maven'
    apply plugin: 'maven-publish'
    apply plugin: 'com.bmuschko.nexus'

    jar {
        manifest {
            instruction 'Require-Capability', 'osgi.extender'
        }
    }

    task delombok(type: JavaExec) {
        inputs.files(file('src/main/java'))
        outputs.dir(file('src/main/delombok'))
        classpath = configurations.compile
        main = "lombok.launch.Main"
        args("delombok", "--quiet", "src/main/java", "--target", "src/delombok/java")
    }

    javadoc {
        inputs.files(file("src/delombok/java"))
        source = file("src/delombok/java")
        classpath = configurations.compile
    }

    javadoc.dependsOn(delombok)

    task sourcesJar(type: Jar) {
        group "Build"
        description "An archive of the source code"
        classifier "sources"
        from sourceSets.main.allJava
    }

    task javadocJar(type: Jar) {
        group "Build"
        description "An archive of the javadoc"
        classifier "javadoc"
        from javadoc
    }

    jar.finalizedBy sourcesJar
    jar.finalizedBy javadocJar

    artifacts {
        sourcesJar
        javadocJar
    }

    publishing {
        publications {
            MyPublication(MavenPublication) {
                from components.java
                artifact sourcesJar
                artifact javadocJar
                groupId 'com.eventsourcing'
                artifactId project.name
                version project.version
            }
        }
    }

    bintray {
        user = System.getenv('BINTRAY_USER')
        key = System.getenv('BINTRAY_KEY')
        publications = ['MyPublication']
        publish = true
        pkg {
            repo = !rootProject.hasProperty('snapshot') ? 'maven' : 'maven-snapshots'
            name = project.name
            userOrg = 'eventsourcing'
            licenses = ['MPL-2.0']
            vcsUrl = 'https://github.com/eventsourcing/es4j'
            version {
                name = project.version
            }
        }
    }

    dependencies {
        // The production code uses the SLF4J logging API at compile time
        compile 'org.slf4j:slf4j-api:1.7.21'
        compile 'org.slf4j:slf4j-simple:1.7.21'

        // Unit testing
        testCompile 'org.testng:testng:6.9.10'

        // Remove boilerplate
        compile 'org.projectlombok:lombok:1.16.8'

        // COSS
        compile 'org.unprotocols:coss:1.0.0'

        // OSGi
        compile 'org.osgi:org.osgi.core:6.0.0'
        compile 'org.osgi:org.osgi.service.cm:1.5.0'
        compile 'org.osgi:org.osgi.service.component:1.3.0'
        compile 'biz.aQute.bnd:biz.aQute.bndlib:3.2.0'

    }

    test.useTestNG()

    tasks.withType(Test) {
      testLogging {
          events "passed", "failed"
          showExceptions true
          exceptionFormat "full"
          showCauses true
          showStackTraces true

      }
    }

    extraArchive {
        javadoc false
        sources false
    }

    modifyPom {
        project {
            name project.name
            description 'Event capture and querying framework for Java'
            url 'https://eventsourcing.com'
            inceptionYear '2016'

            scm {
                url 'https://github.com/eventsourcing/es4j'
                connection 'scm:git:git://github.com/eventsourcing/es4j.git'
                developerConnection 'scm:git:git@github.com:eventsourcing/es4j.git'
            }

            licenses {
                license {
                    name 'Mozilla Public License, v. 2.0'
                    url 'http://mozilla.org/MPL/2.0/'
                    distribution 'repo'
                }
            }

            developers {
                developer {
                    id 'yrashk'
                    name 'Yurii Rashkovskii'
                    email 'yrashk@gmail.com'
                }
            }

            contributors {
                contributor {
                    name 'Benjamin Gudehus'
                    email 'hastebrot@gmail.com'
                }
            }
        }
    }
}

idea {
    project {
        languageLevel = '1.8'
        vcs = 'Git'
        ipr.withXml { xmlFile ->
            // enable 'Annotation Processors'
            xmlFile.asNode().component.find {
                it.@name == 'CompilerConfiguration'
            }['annotationProcessing'][0].replaceNode {
                annotationProcessing {
                    profile(default: true, name: 'Default', useClasspath: 'true', enabled: true)
                }
            }
        }
    }
}

dependencies {
    compile project(':eventsourcing-core')
    compile project(':eventsourcing-inmem')
    compile project(':eventsourcing-postgresql')
    compile project(':eventsourcing-repository')

    // Remove boilerplate
    compile 'org.projectlombok:lombok:1.16.8'

    // Connection pool
    testCompile 'com.zaxxer:HikariCP:2.4.7'

}
