buildscript {
    repositories {
        mavenCentral()
        maven {
          url "https://plugins.gradle.org/m2/"
        }
        eventuateMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
    }
    dependencies {
        classpath "io.eventuate.plugins.gradle:eventuate-plugins-gradle-publish:$eventuatePluginsGradleVersion"
        classpath "com.avast.gradle:gradle-docker-compose-plugin:$dockerComposePluginVersion"
    }
}

plugins {
}

apply plugin: 'docker-compose'
apply plugin: "io.eventuate.plugins.gradle.publish.EventuatePublish"

allprojects {
    group = "io.eventuate.local.java"
}

projects {

}

allprojects {

    repositories {
        mavenCentral()
        eventuateMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
    }

    if (!project.name.endsWith("-bom"))
      dependencies {
          testImplementation "org.mockito:mockito-core:$mockitoVersion"
          testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
          testRuntimeOnly "org.junit.platform:junit-platform-launcher:$junitPlatformLauncherVersion"
      }

}

subprojects { project ->
    java {
        toolchain {
            languageVersion.set(JavaLanguageVersion.of(17))
        }
    }
}

task aggregatedTestReport(type: TestReport) {
    destinationDir = file("$buildDir/reports/allTests")
    // Include the results from the 'test' task in all subprojects
    reportOn subprojects.findAll{!it.name.endsWith("-bom")}*.test
}

gradle.projectsEvaluated {
    task aggregateJavaDocs(type: Javadoc) {
        description = 'Aggregated Javadoc API documentation of all subprojects.'
        group = JavaBasePlugin.DOCUMENTATION_GROUP

        destinationDir file("$buildDir/docs/javadoc")
    }

    task("aggregateJavaDocsJar", type: org.gradle.api.tasks.bundling.Jar, dependsOn: project.aggregateJavaDocs) {
        classifier = 'javadoc'
        from 'build/docs/javadoc'
    }

    artifacts {
        archives project.aggregateJavaDocsJar
    }

}

dockerCompose {
    environment.put "EVENTUATE_COMMON_VERSION", eventuateCommonImageVersion
    environment.put "EVENTUATE_MESSAGING_KAFKA_IMAGE_VERSION", eventuateMessagingKafkaImageVersion
    environment.put "EVENTUATE_CDC_VERSION", eventuateCdcImageVersion

    removeOrphans = true

    mysql {
        projectName = null
        useComposeFiles = ["docker-compose-mysql.yml"]
        removeContainers = Boolean.valueOf(project.ext.removeContainers)
    }

    mssql {
        projectName = null
        useComposeFiles = ["docker-compose-mssql.yml"]
        removeContainers = Boolean.valueOf(project.ext.removeContainers)
    }

    postgres {
        projectName = null
        useComposeFiles = ["docker-compose-postgres.yml"]
        removeContainers = Boolean.valueOf(project.ext.removeContainers)
    }
}
tasks.withType(Test).configureEach {
    useJUnitPlatform()
}
