plugins {
    id 'java-library'
    id 'maven-publish'
    id 'signing'
    id 'jacoco'
}

group = 'org.dom4j'
base {
    archivesName = 'dom4j'
}

// Split optional dependencies into Gradle "features", which can be requested individually
// by Gradle users, and are converted into plain optional dependencies in the Maven POM.
def featureNames = ["jaxen", "xsdlib", "jaxb", "pullParser", "xpp"]

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
    withJavadocJar()
    withSourcesJar()

    featureNames.forEach { f ->
        registerFeature("${f}Support") {
            usingSourceSet(sourceSets["main"])
        }
    }
}

compileJava {
    options.encoding = 'UTF-8'
}

jar {
    manifest {
        attributes('Automatic-Module-Name': 'org.dom4j')
    }
}

test {
    useTestNG()
}

jacocoTestReport {
    reports {
        xml.required = true
    }
}

check.dependsOn 'jacocoTestReport'

repositories {
    mavenCentral()
}

dependencies {
    jaxenSupportApi 'jaxen:jaxen:1.1.6'
    xsdlibSupportApi 'net.java.dev.msv:xsdlib:2022.7'
    jaxbSupportApi 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.2'
    pullParserSupportApi 'pull-parser:pull-parser:2.1.10'
    xppSupportApi 'xpp3:xpp3:1.1.4c'

    testImplementation(
            'org.testng:testng:7.10.2',

            'xerces:xercesImpl:2.12.2',
            'xalan:xalan:2.7.2',
    )
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            featureNames.forEach { f ->
                // Gradle variants get converted into plain "optional" POM dependencies,
                // which is losing information but it's what we want to happen, so be quiet please
                suppressPomMetadataWarningsFor("${f}SupportApiElements")
                suppressPomMetadataWarningsFor("${f}SupportRuntimeElements")
            }

            pom {
                name = 'dom4j'
                description = 'flexible XML framework for Java'
                url = 'http://dom4j.github.io/'
                licenses {
                    license {
                        name = 'BSD 3-clause New License'
                        url = 'https://github.com/dom4j/dom4j/blob/master/LICENSE'
                    }
                }
                developers {
                    developer {
                        name = 'Filip Jirsák'
                        email = 'filip@jirsak.org'
                        url = 'https://github.com/FilipJirsak'
                    }
                }
                scm {
                    connection = 'scm:git:git@github.com:dom4j/dom4j.git'
                    developerConnection = 'scm:git:git@github.com:dom4j/dom4j.git'
                    url = 'git@github.com:dom4j/dom4j.git'
                }
            }
        }
    }
}

if (project.hasProperty('ossrhUsername') && project.hasProperty('ossrhPassword')) {
    publishing {
        repositories {
            maven {
                url = 'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
                authentication {
                    basic(BasicAuthentication)
                }
                credentials {
                    username = ossrhUsername
                    password = ossrhPassword
                }
            }

//		mavenSnapshot{
//		url "https://oss.sonatype.org/content/repositories/snapshots/"
//			authentication(userName: ossrhUsername, password: ossrhPassword)
//		}
        }
    }
}

if (project.hasProperty('signing.keyId')) {
    signing {
        sign publishing.publications.mavenJava
    }
}
