plugins {
    id 'java-library'
    id 'maven-publish'
    id 'org.jreleaser' version '1.14.0'
}

String implementationTitle = 'Antithesis SDK for Java'

java {
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        sdk(MavenPublication) {
            groupId = 'com.antithesis'
            artifactId = 'sdk'

            from components.java
            pom {
                name = "${implementationTitle}"
                description = 'This library provides methods for Java programs to configure the Antithesis platform.'
                url = 'https://antithesis.com/docs/using_antithesis/sdk/java/overview/'

                licenses {
                    license {
                        name = 'MIT License'
                        url = 'https://github.com/antithesishq/antithesis-sdk-java/blob/main/LICENSE'
                    }
                }

                developers {
                    developer {
                        id = 'Antithesis'
                        name = 'Antithesis HQ'
                        email = 'noreply@antithesis.com'
                    }
                }

                scm {
                    connection = 'scm:git:git://github.com/antithesishq/antithesis-sdk-java.git'
                    developerConnection = 'scm:git:ssh://github.com/antithesishq/antithesis-sdk-java.git'
                    url = 'https://github.com/antithesishq/antithesis-sdk-java'
                }
            }
        }
    }
    repositories {
        maven {
            url = layout.buildDirectory.dir('staging-deploy')
        }
    }
}

jreleaser {
    gitRootSearch = true
    signing {
        active = 'ALWAYS'
        armored = true
    }
    deploy {
        maven {
            mavenCentral {
                sonatype {
                    applyMavenCentralRules = true
                    active = 'ALWAYS'
                    url = 'https://central.sonatype.com/api/v1/publisher'
                    stagingRepository('build/staging-deploy')
                }
            }
        }
    }
}
jreleaserFullRelease.dependsOn(publish)

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.2.3'

    if (file("libs/antithesis-ffi-${project.version}.jar").isFile()) {
        implementation(files("libs/antithesis-ffi-${project.version}.jar"))
    } else {
        implementation(project(":ffi"))
    }

    compileOnly 'org.projectlombok:lombok:1.18.34'
    annotationProcessor 'org.projectlombok:lombok:1.18.34'

    // Use JUnit Jupiter for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.3'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
    testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.2.3'

    testCompileOnly 'org.projectlombok:lombok:1.18.34'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
}

compileJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    options.compilerArgs += ["-Xlint:-options"]
}

jar {
    archiveClassifier = ''
    archiveBaseName = 'antithesis'
    archiveAppendix = 'sdk'
    manifest {
        attributes 'Implementation-Title': "${implementationTitle}",
                'Implementation-Version': "${project.version}",
                'Created-By': "Gradle ${gradle.gradleVersion}",
                'Build-Jdk': "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
                'Build-OS': "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
    }
    reproducibleFileOrder = true
    preserveFileTimestamps = false
}

tasks.register("copyDependencies", Copy) {
    from(configurations.runtimeClasspath)
    into(layout.buildDirectory.dir("dependencies"))
}

assemble.dependsOn copyDependencies

task generateJavadoc(type: Javadoc) {
    source = sourceSets.main.allJava
    exclude 'com/antithesis/sdk/internal/**'

    // Hack to enable Javadoc to compile while excluding internal functions
    var sourceSetDirectories = sourceSets.main.java.srcDirs.join(":")
    options.addStringOption("sourcepath", sourceSetDirectories)

    classpath = sourceSets.main.compileClasspath
    destinationDir = file("${buildDir}/docs/javadoc")
}
javadocJar.dependsOn generateJavadoc

test {
    testLogging {
        exceptionFormat = 'full'
        showStandardStreams = true
    }
}
test.dependsOn jar

tasks.named('test') {
    useJUnitPlatform()
}
