subprojects {
  apply plugin: 'java'
  apply plugin: 'checkstyle'
  apply plugin: 'jacoco'

  repositories {
    mavenCentral()
    maven {
      url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
  }

  compileJava {
    options.compilerArgs << "-Xlint:all" << "-Werror"
  }

  compileTestJava {
    options.compilerArgs << "-Xlint:all" << "-Xlint:-processing" << "-Werror"
  }

  plugins.withType(JavaPlugin) {
    checkstyle.sourceSets = [sourceSets.main]
  }

  test {
    maxParallelForks = 2
    jacoco {
      excludes = ['**/package-info**','**/*Test']
      destinationFile = file("$buildDir/reports/jacoco/test.exec")
    }
    getReports().getJunitXml().setDestination(file("$buildDir/reports/tests/xml"))
    getReports().getHtml().setDestination(file("$buildDir/reports/tests/html"))
    setBinaryResultsDirectory(file("$buildDir/reports/tests/bin"))
  }

  test.finalizedBy("jacocoTestReport")

  jacocoTestReport {
    reports {
      csv.required = false
      xml.required = true
      xml.destination = file("$buildDir/reports/jacoco/jacoco.xml")
      html.required = true
      html.destination = file("$buildDir/reports/jacoco/html")
    }
    doLast {
      println "Test results available at:"
      println "html - $buildDir/reports/tests/html/index.html"
      println "Test coverage reports available at:"
      println "html - $buildDir/reports/jacoco/html/index.html"
    }
  }

  jar {
    manifest {
      attributes 'Implementation-Title': 'auroraArc', 'Implementation-Version': archiveVersion
    }
  }

  javadoc {
    source = sourceSets.main.allJava
    options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PUBLIC
  }

  task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from 'build/docs/javadoc'
  }

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

  task copyLibs(type: Copy) {
    into "$buildDir/dependencies/"
    from configurations.testRuntimeClasspath
  }

  build.finalizedBy("copyLibs")

  artifacts {
    archives jar
    archives javadocJar
    archives sourcesJar
  }
}

project(':arcCommon') {
  archivesBaseName = 'arcCommon'

  dependencies {
    testImplementation (
      "org.threadly:threadly-test:$threadlyTestVersion",
      "junit:junit:$junitVersion",
      "org.mockito:mockito-core:$mockitoVersion",
      "org.openjdk.jmh:jmh-core:$jmhVersion",
      "org.openjdk.jmh:jmh-generator-annprocess:$jmhVersion",
      "org.jdbi:jdbi3-core:$jdbiVersion",
      "org.jdbi:jdbi3-sqlobject:$jdbiVersion",
      "com.zaxxer:HikariCP:$hikariVersion",
      "ch.qos.logback:logback-core:$logbackVersion",
      "ch.qos.logback:logback-classic:$logbackVersion"
    )
    testAnnotationProcessor "org.openjdk.jmh:jmh-generator-annprocess:$jmhVersion"

    implementation (
      "org.threadly:threadly:$threadlyVersion"
    )
  }
}

project(':mysqlAuroraArc') {
  archivesBaseName = 'auroraArc-mysql'

  dependencies {
    testImplementation (
      project(':arcCommon').sourceSets.test.output,
      "junit:junit:$junitVersion",
      "org.mockito:mockito-core:$mockitoVersion",
      "org.jdbi:jdbi3-core:$jdbiVersion",
      "org.jdbi:jdbi3-sqlobject:$jdbiVersion",
      "com.zaxxer:HikariCP:$hikariVersion",
      "ch.qos.logback:logback-core:$logbackVersion",
      "ch.qos.logback:logback-classic:$logbackVersion"
    )

    implementation (
      project(":arcCommon"),
      "org.threadly:threadly:$threadlyVersion",
      "mysql:mysql-connector-java:$mysqlVersion"
    )
  }
}

project(':psqlAuroraArc') {
  archivesBaseName = 'auroraArc-psql'

  dependencies {
    testImplementation (
      project(':arcCommon').sourceSets.test.output,
      "junit:junit:$junitVersion",
      "org.mockito:mockito-core:$mockitoVersion"
    )

    implementation (
      project(":arcCommon"),
      "org.threadly:threadly:$threadlyVersion",
      "org.postgresql:postgresql:$psqlVersion"
    )
  }
}