/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven {
            name = 'localPluginRepository'
            url = uri( "${gradle.gradleUserHomeDir}/tmp/plugins" )
        }
    }

    includeBuild 'local-build-plugins'
}

plugins {
    id 'org.hibernate.orm.build.env-settings'
    id 'org.hibernate.orm.build.jdks-settings'
    id 'com.gradle.develocity' version '3.17.6'
    id 'com.gradle.common-custom-user-data-gradle-plugin' version '2.0.2'
}

dependencyResolutionManagement {
    repositories {
        if ( rootProject.hasProperty( "mavenMirror" ) ) {
            url( rootProject.property( "mavenMirror" ) )
        }
        mavenCentral()
        if (System.getProperty('JPA_PREVIEW') != null) {
			maven {
				url "https://jakarta.oss.sonatype.org/content/repositories/releases/"
			}
            // Needed for the SNAPSHOT versions of Jakarta Persistence
            maven {
                url "https://jakarta.oss.sonatype.org/content/repositories/snapshots/"
            }
		}

        mavenLocal()

        //Allow loading additional dependencies from a local path;
        //useful to load JDBC drivers which can not be distributed in public.
        if (System.env['ADDITIONAL_REPO'] != null) {
            flatDir {
                dirs "${System.env.ADDITIONAL_REPO}"
            }
        }
    }

    pluginManagement {
        repositories {
            gradlePluginPortal()
        }
    }

    versionCatalogs {
        jdks {
            // see gradle.properties (or set with -D/-P)
            var String baseJdk = jdkVersions.baseline
            var String maxJdk = jdkVersions.max

            version "baseline", baseJdk
            version "compatible", "17 or 21"
            version "jdbc", "4.2" // Bundled with JDK 11

            // Gradle does bytecode transformation on tests.
            // You can't use bytecode higher than what Gradle supports, even with toolchains.
            version "maxSupportedBytecode", maxJdk
        }
        libs {
            def antlrVersion = version "antlr", "4.13.2"
            // WARNING: When upgrading to a version of bytebuddy that supports a new bytecode version,
            // make sure to remove the now unnecessary net.bytebuddy.experimental=true in relevant CI jobs (Jenkinsfile).
            def byteBuddyVersion = version "byteBuddy", "1.15.10"
            def classmateVersion = version "classmate", "1.7.0"
            def geolatteVersion = version "geolatte", "1.9.1"
            def hibernateModelsVersion = version "hibernateModels", "0.9.3"
            def jandexVersion = version "jandex", "3.2.3"
            def jacksonVersion = version "jackson", "2.18.2"
            def jbossLoggingVersion = version "jbossLogging", "3.6.1.Final"
            def jbossLoggingToolVersion = version "jbossLoggingTool", "3.0.3.Final"

            def agroalVersion = version "agroal", "2.5"
            def c3poVersion = version "c3p0", "0.10.1"
            def hikaricpVersion = version "hikaricp", "6.2.1"
            def ucpVersion = version "ucp", "23.6.0.24.10"

            def jcacheVersion = version "jcache", "1.1.1"
            def ehcache3Version = version "ehcache3", "3.10.8"

            def micrometerVersion = version "micrometer", "1.14.1"

            def antVersion = version "ant", "1.10.15"

            library( "antlr", "org.antlr", "antlr4" ).versionRef( antlrVersion )
            library( "antlrRuntime", "org.antlr", "antlr4-runtime" ).versionRef( antlrVersion)

            library( "byteBuddy", "net.bytebuddy", "byte-buddy" ).versionRef( byteBuddyVersion )
            library( "byteBuddyAgent", "net.bytebuddy", "byte-buddy-agent" ).versionRef( byteBuddyVersion )

            library( "logging", "org.jboss.logging", "jboss-logging" ).versionRef( jbossLoggingVersion )
            library( "loggingAnnotations", "org.jboss.logging", "jboss-logging-annotations" ).versionRef( jbossLoggingToolVersion )
            library( "loggingProcessor", "org.jboss.logging", "jboss-logging-processor" ).versionRef( jbossLoggingToolVersion )

            library( "hibernateModels", "org.hibernate.models", "hibernate-models" ).versionRef( hibernateModelsVersion )
            library( "hibernateModelsJandex", "org.hibernate.models", "hibernate-models-jandex" ).versionRef( hibernateModelsVersion )
            library( "jandex", "io.smallrye", "jandex" ).versionRef( jandexVersion )
            library( "classmate", "com.fasterxml", "classmate" ).versionRef( classmateVersion )

            library( "jackson", "com.fasterxml.jackson.core", "jackson-databind" ).versionRef( jacksonVersion )
            library( "jacksonXml", "com.fasterxml.jackson.dataformat", "jackson-dataformat-xml" ).versionRef( jacksonVersion )
            library( "jacksonJsr310", "com.fasterxml.jackson.datatype", "jackson-datatype-jsr310" ).versionRef( jacksonVersion )

            library( "agroal", "io.agroal", "agroal-api" ).versionRef( agroalVersion )
            library( "agroalPool", "io.agroal", "agroal-pool" ).versionRef( agroalVersion )
            library( "c3p0", "com.mchange", "c3p0" ).versionRef( c3poVersion )
            library( "hikaricp", "com.zaxxer", "HikariCP" ).versionRef( hikaricpVersion )
            library( "ucp", "com.oracle.database.jdbc", "ucp11" ).versionRef( ucpVersion )

            library( "ojdbc11", "com.oracle.database.jdbc", "ojdbc11" ).versionRef( ucpVersion )

            library( "geolatte", "org.geolatte", "geolatte-geom" ).versionRef( geolatteVersion )

            library( "jcache", "javax.cache", "cache-api" ).versionRef( jcacheVersion )
            library( "ehcache3", "org.ehcache", "ehcache" ).versionRef( ehcache3Version )

            library( "micrometer", "io.micrometer", "micrometer-core" ).versionRef( micrometerVersion )

            library( "ant", "org.apache.ant", "ant" ).versionRef( antVersion )
        }
        jakartaLibs {
            // `jakartaJpaVersion` comes from the local-build-plugins to allow for command-line overriding of the JPA version to use
            def jpaVersion = version "jpa", "${jakartaJpaVersion}"

            def annotationVersion = version "annotation", "3.0.0"
            def cdiVersion = version "cdi", "4.1.0"
            def injectVersion = version "inject", "2.0.1"
            def interceptorsVersion = version "interceptors", "2.2.0"
            def dataVersion = version "data", "1.0.1"
            def jaccVersion = version "jacc", "3.0.0"
            def jaxbApiVersion = version "jaxbApi", "4.0.2"
            def jaxbRuntimeVersion = version "jaxbRuntime", "4.0.5"
            def jsonbApiVersion = version "jsonbApi", "3.0.1"
            def jsonbRuntimeVersion = version "jsonbRuntime", "3.0.4"
            def jtaVersion = version "jta", "2.0.1"
            def validationVersion = version "validation", "3.1.0"
            def xjcVersion = version "jaxbRuntime", jaxbRuntimeVersion

            library( "jpa", "jakarta.persistence", "jakarta.persistence-api" ).versionRef( jpaVersion )
            library( "jta", "jakarta.transaction", "jakarta.transaction-api" ).versionRef( jtaVersion )

            library( "validation", "jakarta.validation", "jakarta.validation-api" ).versionRef( validationVersion )
            library( "jacc", "jakarta.authorization", "jakarta.authorization-api" ).versionRef( jaccVersion )
            library( "cdi", "jakarta.enterprise", "jakarta.enterprise.cdi-api" ).versionRef( cdiVersion )
            library( "annotation", "jakarta.annotation", "jakarta.annotation-api" ).versionRef( annotationVersion )
            library( "interceptors", "jakarta.interceptor", "jakarta.interceptor-api" ).versionRef( interceptorsVersion )
            library( "data", "jakarta.data", "jakarta.data-api" ).versionRef( dataVersion )
            library( "jsonbApi", "jakarta.json.bind", "jakarta.json.bind-api" ).versionRef( jsonbApiVersion )
            library( "jsonb", "org.eclipse", "yasson" ).versionRef( jsonbRuntimeVersion )
            library( "inject", "jakarta.inject", "jakarta.inject-api" ).versionRef( injectVersion )
            library( "jaxbApi", "jakarta.xml.bind", "jakarta.xml.bind-api" ).versionRef( jaxbApiVersion )
            library( "jaxb", "org.glassfish.jaxb", "jaxb-runtime" ).versionRef( jaxbRuntimeVersion )
            library( "xjc", "org.glassfish.jaxb", "jaxb-xjc" ).versionRef( xjcVersion )
        }
        testLibs {
            def junit5Version = version "junit5", "5.11.3"
            def junit4Version = version "junit4", "4.13.2"
            def junit5LauncherVersion = version "junit5Launcher", "1.11.3"

            def assertjVersion = version "assertj", "3.26.3"
            def hamcrestVersion = version "hamcrest", "3.0"
            def bytemanVersion = version "byteman", "4.0.23"
            def jbossJtaVersion = version "jbossJta", "7.1.0.Final"
            def jbossTxSpiVersion = version "jbossTxSpi", "8.0.0.Final"
            def log4jVersion = version "log4j", "2.24.2"
            def mockitoVersion = version "mockito", "5.14.2"
            def shrinkwrapVersion = version "shrinkwrap", "1.2.6"
            def shrinkwrapDescriptorsVersion = version "shrinkwrapDescriptors", "2.0.0"
            def weldVersion = version "weld", "5.1.3.Final"
            def wildFlyTxnClientVersion = version "wildFlyTxnClient", "2.0.0.Final"

            def jfrUnitVersion = version "jfrUnit", "1.0.0.Alpha2"

            def hibernateValidatorVersion = version "hibernateValidator", "9.0.0.Beta3"
            def expresslyVersion = version "expressly", "6.0.0-M1"

            library( "validator", "org.hibernate.validator", "hibernate-validator" ).versionRef( hibernateValidatorVersion )
            library( "expressly", "org.glassfish.expressly", "expressly" ).versionRef( expresslyVersion )

            library( "junit5Api", "org.junit.jupiter", "junit-jupiter-api" ).versionRef( junit5Version )
            library( "junit5Engine", "org.junit.jupiter", "junit-jupiter-engine" ).versionRef( junit5Version )
            library( "junit5Params", "org.junit.jupiter", "junit-jupiter-params" ).versionRef( junit5Version )
            library( "junit4Engine", "org.junit.vintage", "junit-vintage-engine" ).versionRef( junit5Version )
            library( "junit5Launcher", "org.junit.platform", "junit-platform-launcher" ).versionRef( junit5LauncherVersion )
            library( "junit4", "junit", "junit" ).versionRef( junit4Version )

            library( "assertjCore", "org.assertj", "assertj-core" ).versionRef( assertjVersion )
            library( "assertjApi", "org.assertj", "assertj-core-api" ).versionRef( assertjVersion )
            library( "hamcrest", "org.hamcrest", "hamcrest" ).versionRef( hamcrestVersion )

            library( "log4j2", "org.apache.logging.log4j", "log4j-core" ).versionRef( log4jVersion )

            library( "shrinkwrap", "org.jboss.shrinkwrap", "shrinkwrap-impl-base" ).versionRef( shrinkwrapVersion )
            library( "shrinkwrapDescriptors", "org.jboss.shrinkwrap.descriptors", "shrinkwrap-descriptors-impl-javaee" ).versionRef( shrinkwrapDescriptorsVersion )

            library( "bytemanBmunit", "org.jboss.byteman", "byteman-bmunit" ).versionRef( bytemanVersion )

            library( "mockito", "org.mockito", "mockito-core" ).versionRef( mockitoVersion )

            library( "jbossJta", "org.jboss.narayana.jta", "narayana-jta" ).versionRef( jbossJtaVersion )
            library( "jbossTxSpi", "org.jboss", "jboss-transaction-spi" ).versionRef( jbossTxSpiVersion )
            library( "wildFlyTxnClient", "org.wildfly.transaction", "wildfly-transaction-client-jakarta" ).versionRef( wildFlyTxnClientVersion )

            library( "weld", "org.jboss.weld.se", "weld-se-shaded" ).versionRef( weldVersion )

            library( "jfrUnit", "org.moditect.jfrunit", "jfrunit-core" ).versionRef( jfrUnitVersion )
        }
        jdbcLibs {
            def h2Version = version "h2", overrideableVersion( "gradle.libs.versions.h2", "2.3.232" )

            def db2Version = version "db2", "11.5.9.0"
            // Latest Derby version 10.16.1.1 only supports JDK 17+, but 10.15.2 should be compatible
            def derbyVersion = version "derby", overrideableVersion( "gradle.libs.versions.derby", "10.15.2.0" )
            def firebirdVersion = version "firebird", "4.0.8.java11"
            def hanaVersion = version "hana", "2.22.12"
            def h2gisVersion = version "h2gis", overrideableVersion( "gradle.libs.versions.h2gis", "2.2.3" )
            def hsqldbVersion = version "hsqldb", overrideableVersion( "gradle.libs.versions.hsqldb", "2.7.4" )
            def informixVersion = version "informix", "4.50.11"
            def mariadbVersion = version "mariadb", "3.5.1"
            def mssqlVersion = version "mssql", "12.8.1.jre11"
            def mysqlVersion = version "mysql", "9.1.0"
            def oracleVersion = version "oracle", "23.4.0.24.05"
            def pgsqlVersion = version "pgsql", "42.7.4"
            def sybaseVersion = version "sybase", "1.3.1"
            def tidbVersion = version "tidb", mysqlVersion
            def altibaseVersion = version "altibase", "7.3.0.0.3"

            library( "h2", "com.h2database", "h2" ).versionRef( h2Version )
            library( "h2gis", "org.orbisgis", "h2gis" ).versionRef( h2gisVersion )
            library( "hsqldb", "org.hsqldb", "hsqldb" ).versionRef( hsqldbVersion )
            library( "derby", "org.apache.derby", "derby" ).versionRef( derbyVersion )
            library( "derbyTools", "org.apache.derby", "derbytools" ).versionRef( derbyVersion )
            library( "postgresql", "org.postgresql", "postgresql" ).versionRef( pgsqlVersion )
            library( "cockroachdb", "org.postgresql", "postgresql" ).versionRef( pgsqlVersion )
            library( "mysql", "com.mysql", "mysql-connector-j" ).versionRef( mysqlVersion )
            library( "tidb", "com.mysql", "mysql-connector-j" ).versionRef( tidbVersion )
            library( "mariadb", "org.mariadb.jdbc", "mariadb-java-client" ).versionRef( mariadbVersion )
            library( "oracle", "com.oracle.database.jdbc", "ojdbc11" ).versionRef( oracleVersion )
            library( "oracleXml", "com.oracle.database.xml", "xdb" ).versionRef( oracleVersion )
            library( "oracleXmlParser", "com.oracle.database.xml", "xmlparserv2" ).versionRef( oracleVersion )
            library( "mssql", "com.microsoft.sqlserver", "mssql-jdbc" ).versionRef( mssqlVersion )
            library( "db2", "com.ibm.db2", "jcc" ).versionRef( db2Version )
            library( "hana", "com.sap.cloud.db.jdbc", "ngdbc" ).versionRef( hanaVersion )
            library( "sybase", "net.sourceforge.jtds", "jtds" ).versionRef( sybaseVersion )
            library( "informix", "com.ibm.informix", "jdbc" ).versionRef( informixVersion )
            library( "firebird", "org.firebirdsql.jdbc", "jaybird" ).versionRef( firebirdVersion )
            library( "altibase", "com.altibase", "altibase-jdbc" ).versionRef( altibaseVersion )
        }
        mavenLibs {
            def mavenCoreVersion = version "mavenCore", "3.9.9"
            def mavenVersion = version "maven", "3.9.9"
            def mavenPluginToolsVersion = version "mavenPluginTools", "3.15.1"

            library( "mavenCore", "org.apache.maven", "maven-core" ).versionRef( mavenCoreVersion )
            library( "mavenArtifact", "org.apache.maven", "maven-artifact" ).versionRef( mavenVersion )
            library( "mavenPlugin", "org.apache.maven", "maven-plugin-api" ).versionRef( mavenVersion )
            library( "mavenPluginTools", "org.apache.maven.plugin-tools", "maven-plugin-annotations" ).versionRef( mavenPluginToolsVersion )
        }
        buildscriptLibs {
            def forbiddenapisversion = version "forbiddenapis", "3.8"

            library( "forbiddenapis", "de.thetaphi", "forbiddenapis" ).versionRef( forbiddenapisversion )
        }
    }
}

String overrideableVersion(String settingName, String defaultVersion) {
    String overridden = settings.ext.find( settingName )
    if ( overridden != null ) {
        return overridden
    }
    return defaultVersion
}

rootProject.name = 'hibernate-orm'

apply from: file( 'gradle/gradle-develocity.gradle' )

if ( !JavaVersion.current().java11Compatible ) {
    throw new GradleException( "Gradle must be run with Java 11 or later" )
}

buildCache {
    local {
        // do not use local build cache for CI jobs, period!
        enabled = !settings.ext.isCiEnvironment
    }
    remote(develocity.buildCache) {
        enabled = settings.ext.useRemoteCache
        // Check access key presence to avoid build cache errors on PR builds when access key is not present
        def accessKey = System.getenv("DEVELOCITY_ACCESS_KEY")
        push = settings.ext.populateRemoteBuildCache && accessKey
    }
}

include 'hibernate-core'
include 'hibernate-testing'

include 'hibernate-envers'
include 'hibernate-spatial'

include 'hibernate-platform'

include 'hibernate-community-dialects'
include 'hibernate-vector'

include 'hibernate-c3p0'
include 'hibernate-hikaricp'
include 'hibernate-agroal'
include 'hibernate-ucp'

include 'hibernate-jcache'

include 'hibernate-micrometer'
include 'hibernate-graalvm'
include 'hibernate-integrationtest-java-modules'

include 'documentation'
include 'release'

include 'annotation-descriptor-generator'

// Not all JDK implementations support JFR
if ( "OpenJDK Runtime Environment".equals( System.getProperty( "java.runtime.name" ) ) ) {
    include 'hibernate-jfr'
}

include 'hibernate-scan-jandex'

include 'metamodel-generator'
project(':metamodel-generator').projectDir = new File(rootProject.projectDir, "tooling/metamodel-generator")
project(':metamodel-generator').name = 'hibernate-processor'

include 'hibernate-gradle-plugin'
project(':hibernate-gradle-plugin').projectDir = new File(rootProject.projectDir, "tooling/hibernate-gradle-plugin")

include 'hibernate-maven-plugin'
project(':hibernate-maven-plugin').projectDir = new File(rootProject.projectDir, "tooling/hibernate-maven-plugin")

include 'hibernate-ant'
project(':hibernate-ant').projectDir = new File(rootProject.projectDir, "tooling/hibernate-ant")

rootProject.children.each { project ->
    project.buildFileName = "${project.name}.gradle"
    assert project.projectDir.isDirectory()
    assert project.buildFile.exists()
    assert project.buildFile.isFile()
}
include 'hibernate-platform'