vanniktech / gradle-maven-publish-plugin

A Gradle plugin that publishes your Android and Kotlin libraries, including sources and javadoc, to Maven Central or any other Nexus instance.
https://vanniktech.github.io/gradle-maven-publish-plugin
Apache License 2.0
1.2k stars 111 forks source link

[Question] How to publish custom artifact to Maven Central? #747

Open overcat opened 3 months ago

overcat commented 3 months ago

Hello, I encountered some difficulties when publishing my custom artifact to Maven Central. Here is my build.gradle.kts file. On line 25, I defined uberJar. When I execute ./gradlew clean build, I can find boilerplate-1.0.0-uber.jar file in the build/libs folder. However, when I execute ./gradlew clean publishToMavenLocal, I cannot find this file in ~/.m2/repository/io/github/exampleuser/boilerplate/1.0.0 folder. What should I do to make it appear here?

import com.vanniktech.maven.publish.SonatypeHost

plugins {
    id("java")
    id("com.vanniktech.maven.publish") version "0.28.0"
}

group = "io.github.exampleuser"
version = "1.0.0"

repositories {
    mavenCentral()
}

tasks {
    test {
        useJUnitPlatform()
    }

    val sourcesJar by creating(Jar::class) {
        archiveClassifier = "sources"
        from(sourceSets.main.get().allSource)
    }

    val uberJar by creating(Jar::class) {
        archiveClassifier = "uber"
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        from(sourceSets.main.get().output)
        dependsOn(configurations.runtimeClasspath)
        from({
            configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
        })
    }

    javadoc {
        isFailOnError = false
        options {
            // https://docs.gradle.org/current/javadoc/org/gradle/external/javadoc/StandardJavadocDocletOptions.html
            this as StandardJavadocDocletOptions
            isSplitIndex = true
            memberLevel = JavadocMemberLevel.PUBLIC
            encoding = "UTF-8"
        }
    }

    val javadocJar by creating(Jar::class) {
        archiveClassifier = "javadoc"
        dependsOn(javadoc)
        from(javadoc.get().destinationDir)
    }
}

artifacts {
    archives(tasks["uberJar"])
    archives(tasks["sourcesJar"])
    archives(tasks["javadocJar"])
}

dependencies {
    implementation("com.google.code.gson:gson:2.10.1")
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

mavenPublishing {
    // or when publishing to https://central.sonatype.com/
    publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
    signAllPublications()
}
> ls -al build/libs  

drwxr-xr-x  6 exampleuser  staff    192 Mar 25 17:08 .
drwxr-xr-x  9 exampleuser  staff    288 Mar 25 17:08 ..
-rw-r--r--  1 exampleuser  staff  94850 Mar 25 17:08 boilerplate-1.0.0-javadoc.jar
-rw-r--r--  1 exampleuser  staff    865 Mar 25 17:08 boilerplate-1.0.0-sources.jar
-rw-r--r--  1 exampleuser  staff    7988 Mar 25 17:08 boilerplate-1.0.0-uber.jar
-rw-r--r--  1 exampleuser  staff    988 Mar 25 17:08 boilerplate-1.0.0.jar

> ls -al ~/.m2/repository/io/github/exampleuser/boilerplate/1.0.0

-rw-r--r--  1 exampleuser  staff  94850 Mar 25 17:11 boilerplate-1.0.0-javadoc.jar
-rw-r--r--  1 exampleuser  staff    865 Mar 25 17:11 boilerplate-1.0.0-sources.jar
-rw-r--r--  1 exampleuser  staff    988 Mar 25 17:11 boilerplate-1.0.0.jar
-rw-r--r--  1 exampleuser  staff   2732 Mar 25 17:11 boilerplate-1.0.0.module
-rw-r--r--  1 exampleuser  staff    766 Mar 25 17:11 boilerplate-1.0.0.pom
-rw-r--r--  1 exampleuser  staff   1279 Mar 25 17:11 maven-metadata-local.xml

Thank you for your time.

gabrielittner commented 3 months ago

Could you share your project to make investigating this easier?

overcat commented 3 months ago

Hi @gabrielittner, since I couldn't find a solution, I am now using nmcp as an alternative.

https://github.com/lightsail-network/java-stellar-sdk/blob/master/build.gradle.kts#L189-L199

zhlinh commented 3 weeks ago

Hi, @gabrielittner

Same issue, any update? Currently only support with or without sources and javadoc, but not for with or without the output jar.

boilerplate-1.0.0-javadoc.jar (support config now, if don't want)
boilerplate-1.0.0-sources.jar (support config now, if don't wan)
boilerplate-1.0.0.jar          【can not config now, if don't want】

Because I wanted to upload an aar with so files, not only jar. I don't want xxx.jar to upload. I found a way to upload custom aar and delete the xxx.jar now, but maybe not an elegant way to modify.

 (publications.getByName("maven") as? MavenPublication)?.apply {
   // filter only javadoc and sources, it worsk
   val arts = artifacts.filter {
            it.file.name.endsWith("javadoc.jar") || it.file.name.endsWith("sources.jar")
   }
   // add and override
   setArtifacts(arts)
   artifact("bin/boilerplate-1.0.0.aar") {
     classifier = "aar"
   }
}

I also tried to modify Platform, and configure(CustomPlatform()) in the MavenPublishBaseExtension, but the Platform is a sealed class, I can not inherit it. And its Subclass like JavaLibrary, AndroidSingleVariantLibrary is data class, I can not modify it too.

So, I can not find a solution to custom the artifact to upload now.