import java.nio.charset.StandardCharsets import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse apply plugin: 'maven-publish' // This script work based on https://ossrh-staging-api.central.sonatype.com/swagger-ui/#/default/manual_upload_repository // and getting available open repos and just uploading them if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) { def taskName = "uploadSonatypePublication" if (rootProject.tasks.names.contains(taskName) == false) { rootProject.tasks.register(taskName) { doLast { def username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER') def password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD') def bearer = Base64.getEncoder().encodeToString("$username:$password".getBytes(StandardCharsets.UTF_8)) def client = HttpClient.newHttpClient() def request = HttpRequest.newBuilder() .uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/search/repositories?state=open")) .GET() .header("Content-Type", "application/json") .header("Authorization", "Bearer $bearer") .build() def response = client.send(request, HttpResponse.BodyHandlers.ofString()) def keys = new ArrayList() response.body().findAll("\"key\"[\\s]*:[\\s]*\"[^\"]+\"").forEach { def key = it.find("[^\"]+\"\$").find("[^\"]+") keys.add(key) } keys.forEach { println("Start uploading $it") def uploadRequest = HttpRequest.newBuilder() .uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/upload/repository/$it?publishing_type=user_managed")) .POST(HttpRequest.BodyPublishers.ofString("")) .header("Content-Type", "application/json") .header("Authorization", "Bearer $bearer") .build() def uploadResponse = client.send(uploadRequest, HttpResponse.BodyHandlers.ofString()) if (uploadResponse.statusCode() != 200) { throw IllegalStateException("Faced error of uploading for repo with key $it. Response: $uploadResponse") } } } } } } task javadocJar(type: Jar) { from javadoc archiveClassifier = 'javadoc' } task sourcesJar(type: Jar) { from sourceSets.main.allSource archiveClassifier = 'sources' } publishing { publications { maven(MavenPublication) { from components.java artifact javadocJar artifact sourcesJar pom { resolveStrategy = Closure.DELEGATE_FIRST description = "It is set of projects with micro tools for avoiding of routines coding" name = "${project.name}" url = "https://github.com/InsanusMokrassar/MicroUtils/" scm { developerConnection = "scm:git:[fetch=]https://github.com/InsanusMokrassar/MicroUtils.git[push=]https://github.com/InsanusMokrassar/MicroUtils.git" url = "https://github.com/InsanusMokrassar/MicroUtils.git" } developers { developer { id = "InsanusMokrassar" name = "Aleksei Ovsiannikov" email = "ovsyannikov.alexey95@gmail.com" } developer { id = "000Sanya" name = "Syrov Aleksandr" email = "000sanya.000sanya@gmail.com" } } licenses { license { name = "Apache Software License 2.0" url = "https://github.com/InsanusMokrassar/MicroUtils/blob/master/LICENSE" } } } } } repositories { if ((project.hasProperty('GITHUBPACKAGES_USER') || System.getenv('GITHUBPACKAGES_USER') != null) && (project.hasProperty('GITHUBPACKAGES_PASSWORD') || System.getenv('GITHUBPACKAGES_PASSWORD') != null)) { maven { name = "GithubPackages" url = uri("https://maven.pkg.github.com/InsanusMokrassar/MicroUtils") credentials { username = project.hasProperty('GITHUBPACKAGES_USER') ? project.property('GITHUBPACKAGES_USER') : System.getenv('GITHUBPACKAGES_USER') password = project.hasProperty('GITHUBPACKAGES_PASSWORD') ? project.property('GITHUBPACKAGES_PASSWORD') : System.getenv('GITHUBPACKAGES_PASSWORD') } } } if ((project.hasProperty('INMONEXUS_USER') || System.getenv('INMONEXUS_USER') != null) && (project.hasProperty('INMONEXUS_PASSWORD') || System.getenv('INMONEXUS_PASSWORD') != null)) { maven { name = "InmoNexus" url = uri("https://nexus.inmo.dev/repository/maven-releases/") credentials { username = project.hasProperty('INMONEXUS_USER') ? project.property('INMONEXUS_USER') : System.getenv('INMONEXUS_USER') password = project.hasProperty('INMONEXUS_PASSWORD') ? project.property('INMONEXUS_PASSWORD') : System.getenv('INMONEXUS_PASSWORD') } } } if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) { maven { name = "sonatype" url = uri("https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/") credentials { username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER') password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD') } } } } } if (project.hasProperty("signing.gnupg.keyName")) { apply plugin: 'signing' signing { useGpgCmd() sign publishing.publications } task signAll { tasks.withType(Sign).forEach { dependsOn(it) } } // Workaround to make android sign operations depend on signing tasks project.getTasks().withType(AbstractPublishToMaven.class).configureEach { def signingTasks = project.getTasks().withType(Sign.class) mustRunAfter(signingTasks) } // Workaround to make test tasks use sign project.getTasks().withType(Sign.class).configureEach { signTask -> def withoutSign = (signTask.name.startsWith("sign") ? signTask.name.minus("sign") : signTask.name) def pubName = withoutSign.endsWith("Publication") ? withoutSign.substring(0, withoutSign.length() - "Publication".length()) : withoutSign // These tasks only exist for native targets, hence findByName() to avoid trying to find them for other targets // Task ':linkDebugTest' uses this output of task ':signPublication' without declaring an explicit or implicit dependency def debugTestTask = tasks.findByName("linkDebugTest$pubName") if (debugTestTask != null) { signTask.mustRunAfter(debugTestTask) } // Task ':compileTestKotlin' uses this output of task ':signPublication' without declaring an explicit or implicit dependency def testTask = tasks.findByName("compileTestKotlin$pubName") if (testTask != null) { signTask.mustRunAfter(testTask) } } }