// https://medium.com/viascom/complete-guide-publish-with-gradle-to-maven-central-native-7b128addbb6
// https://github.com/researchgate/gradle-release
plugins {
    id "maven-publish"
    id "signing"
    id "java-library"
    id 'net.researchgate.release' version '3.0.2'
    id 'jacoco'
}

java {
    withJavadocJar()
    withSourcesJar()
}

ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

sourceCompatibility = 1.8
group = 'com.github.drapostolos'
archivesBaseName = 'type-parser'

repositories {
    mavenCentral()
}

dependencies { 
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.assertj:assertj-core:3.23.1'
}

test {
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.enabled = true
        html.required = true
    }
}

jacocoTestCoverageVerification {
    violationRules {
        rule {
            limit {
                minimum = 0.8
            }
        }
    }
}

check.dependsOn jacocoTestCoverageVerification

publishing {
    repositories {
        maven {
            def releaseRepo = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
            def snapshotRepo = "https://oss.sonatype.org/content/repositories/snapshots/"
            
            name = "OSSRH"
            url = isReleaseVersion ? releaseRepo : snapshotRepo
            
            credentials {
                username = findProperty("ossrhUsername") ?: System.getenv("OSSRH_USERNAME")
                password = findProperty("ossrhPassword") ?: System.getenv("OSSRH_PASSWORD")
            }
        }
    }

    publications {
        mavenJava(MavenPublication) {
            from components.java
            
            pom {
                groupId = 'com.github.drapostolos'
                name = 'type-parser'
                description = 'This is a lightweigth library that does nothing but parse a string to a given type. Supports most of the java classes, such as Integer, File, Enum, Float and also generic types as well, such as List&lt;Integer&gt;, Set&lt;File&gt; etc. Also possible to register your own parsers.'
                url = 'https://github.com/drapostolos/type-parser'
                packaging = 'jar'

                licenses {
                    license {
                        name = 'The MIT License (MIT)'
                        url = 'http://opensource.org/licenses/MIT'
                    }
                }

                scm {
                    url = 'https://github.com/drapostolos/type-parser'
                    connection = 'scm:git://github.com:drapostolos/type-parser.git'
                    developerConnection = 'scm:git://github.com:drapostolos/type-parser.git'
                }

                developers {
                    developer {
                        id = 'drapostolos'
                        name = 'Alexander Poulikakos'
                        email = 'github@labelscans.com'
                    }
                }
            }
        }
    }
}

signing {
    sign publishing.publications.mavenJava
}

tasks.withType(Sign) {
    onlyIf { isReleaseVersion }
}

release {
    tagTemplate = '$name-$version'

    git {
        requireBranch.set('master')
    }
}
afterReleaseBuild.dependsOn publish

