// Documentation: https://jreleaser.org/guide/latest/examples/maven/maven-central.html#_gradle
// Secrets stored in ~/.jreleaser/config.toml

apply plugin: 'maven-publish'
apply plugin: 'signing'

publishing {
    repositories {
        maven {
            def snapshotsRepoUrl = 'http://oss.sonatype.org/content/repositories/snapshots/'
            def releasesRepoUrl = 'http://oss.sonatype.org/service/local/staging/deploy/maven2/'
            url = project.hasProperty('release') && !version.endsWith('SNAPSHOT') ? releasesRepoUrl : snapshotsRepoUrl

            credentials {
                username System.getenv('SONATYPE_NEXUS_USERNAME')
                password System.getenv('SONATYPE_NEXUS_PASSWORD')
            }
        }
    }

    publications {
        mavenJava(MavenPublication) {
            groupId = 'se.koc'
            artifactId = 'zutil'
            from components.java

            pom {
                name = 'Zutil'
                description = 'A library containing utility classes and code snippets.'
                url = 'https://github.com/Ziver/zutil'

                licenses {
                    license {
                        name = 'MIT License'
                        url = 'http://www.opensource.org/licenses/mit-license.php'
                    }
                }
                developers {
                    developer {
                        id = 'Ziver Koc'
                        name = 'dev@koc.se'
                    }
                }
                scm {
                    connection = 'scm:git:https://github.com/Ziver/zutil.git'
                    developerConnection = 'scm:git:https://repo.koc.se/zutil-java.git'
                    url = 'https://github.com/Ziver/zutil'
                }
            }
        }
    }
}

// Signing

signing {
    useGpgCmd()
    sign publishing.publications.mavenJava
}

// Generate version.txt

ext.genOutputDir = file("${buildDir}/generated-resources")

task generateVersionTxt()  {
    ext.outputFile = file("${genOutputDir}/version.txt")
    outputs.file(outputFile)
    doLast {
        outputFile.text = """GroupId: ${project.group}
Name: ${project.name}
Version: ${version}
Build-time: ${java.time.LocalDateTime.now()}
"""
    }
}

sourceSets.main.output.dir genOutputDir, builtBy: generateVersionTxt