Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
overcat opened this issue Mar 25, 2024 · 3 comments
Open

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

overcat opened this issue Mar 25, 2024 · 3 comments

Comments

@overcat
Copy link

overcat commented Mar 25, 2024

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.

@overcat overcat changed the title How to publish custom artifact to Maven Central? [Question] How to publish custom artifact to Maven Central? Mar 25, 2024
@gabrielittner
Copy link
Collaborator

Could you share your project to make investigating this easier?

@overcat
Copy link
Author

overcat commented Apr 19, 2024

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
Copy link

zhlinh commented Jun 24, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants