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-versions:$eventuatePluginsGradleVersion"
        classpath "io.eventuate.plugins.gradle:eventuate-plugins-gradle-publish:$eventuatePluginsGradleVersion"
        classpath "com.avast.gradle:gradle-docker-compose-plugin:$dockerComposePluginVersion"
    }
}

apply plugin: 'docker-compose'

allprojects {
    group = "io.eventuate.tram.core"
}

//apply plugin: WaitForCdcPlugin
apply plugin: "io.eventuate.plugins.gradle.publish.EventuatePublish"

allprojects {

    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/milestone" }
        eventuateMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
    }


    if (!project.name.endsWith("-bom")) {

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

            constraints {
                annotationProcessor("io.micronaut.configuration:micronaut-openapi:1.5.3")
                implementation("io.micronaut.configuration:micronaut-openapi:1.5.3")
            }
      }

    }
}

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

    configurations.all {
        resolutionStrategy.cacheChangingModulesFor 300, 'seconds'
        resolutionStrategy.cacheDynamicVersionsFor 300, 'seconds'
    }
}

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
    retainContainersOnStartupFailure = true

    mysqlbinlogkafka {
        projectName = null
        useComposeFiles = ["docker-compose-mysql-binlog-kafka.yml"]
    }

    mysqlbinlogredis {
        projectName = null
        useComposeFiles = ["docker-compose-mysql-binlog-redis.yml"]
    }

    mssqlpollingactivemq {
        projectName = null
        useComposeFiles = ["docker-compose-mssql-polling-activemq.yml"]
    }

    postgreswalrabbitmq {
        projectName = null
        useComposeFiles = ["docker-compose-postgres-wal-rabbitmq.yml"]
    }
}
tasks.withType(Test).configureEach {
    useJUnitPlatform()
}

task refreshAndBuildTestClasses(type: Exec) {
    description = 'Refresh dependencies and compile test classes in parallel'
    group = 'build'
    commandLine './gradlew', '--refresh-dependencies', 'testClasses', '--parallel'
}
