Hello everyone, I've recently made a library in pu...
# kotlin-native
a
Hello everyone, I've recently made a library in pure Kotlin/Native and would like to release it. But when I call
gradle publish
, only a
.jar
file gets published. Afaik, it's necessary to include a
.klib
as well for it to be used in other Native projects, and the problem is that publishing does not generate/upload that
.klib
file. Build file: https://github.com/reblast/KPresence/blob/master/build.gradle.kts
m
Have you tried
publishToMavenLocal
for example? This generates all the
jars
and
klibs
for all targets I need.
a
hi 👋 When debugging publishing I find it's helpful to add a project-local repository.
Copy code
publishing {
  repositories {
    maven(file("build/dev-maven")) {
      name = "DevMaven"
    }
}
Then you can run
gradle publishAllToDevMaven
and check the directory. It's nicer because you can view the files in IJ rather than digging through
~/.m2
, and you can more easily delete & retry.
a
Running
publishToMavenLocal
did generate the klib files.
And after that I ran
publish
but faced an error, seems like its not creating separate packages for each target:
a
ahh I see, you're manually specifying the GAV for the Maven POM https://github.com/reblast/KPresence/blob/72cc3ebbd9e08b1eabafb4518b56e80180bcf8c7/build.gradle.kts#L101-L103
Copy code
publications {
        withType<MavenPublication> {
            artifact(javadocJar)
            
            pom {
                groupId = "me.blast"
                artifactId = "kpresence"
                version = project.version.toString()
Kotlin needs to set the artifact ID for each variant, so you'll end up with
me.blast:kpresence-macos:1.2.3
,
me.blast:kpresence-linux:1.2.3
,
me.blast:kpresence-mingw:1.2.3
, etc
so, remove manually specifying those in the POM, and instead, either set the group and version in
build.gradle.kts
Copy code
project.version = "1.2.3"
project.group = "me.blast"
Or in
gradle.properties
Copy code
version=1.2.3
group=me.blast
And Gradle will automatically pick those up, and use them for the Maven publications
a
whoops, got it
let me try again
a
you're already setting the group and version in
build.gradle.kts
https://github.com/reblast/KPresence/blob/72cc3ebbd9e08b1eabafb4518b56e80180bcf8c7/build.gradle.kts#L13-L14, so you can just remove the manual override from the MavenPublications :)
a
thanks! seems to be working now
although, i got this warning for all targets, is this okay?
a
ah yeah, that warning You can try this workaround https://github.com/gradle/gradle/issues/26091#issuecomment-1722947958
a
that did the job, thanks once again
by the way, has anyone got any experience with publishing to sonatype? github packages does not seem to be a reliable option since it requires authentication for others to be able to use.
i couldnt understand anything from the docs, does it require any more additional steps other than creating a namespace?
Copy code
maven {
            name = "OSS"
            val releasesRepoUrl = uri("<https://oss.sonatype.org/service/local/staging/deployByRepositoryId/${project.findProperty(>"sonatype.repository")}/")
            val snapshotsRepoUrl = uri("<https://oss.sonatype.org/content/repositories/snapshots/>")
            url = if (version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl

            credentials {
                username = project.findProperty("sonatype.username") as String? ?: System.getenv("SONATYPE_USERNAME")
                password = project.findProperty("sonatype.password") as String? ?: System.getenv("SONATYPE_PASSWORD")
            }
        }
Copy code
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishKotlinMultiplatformPublicationToOSSRepository'.
Failed to publish publication 'kotlinMultiplatform' to repository 'OSS'
   > Could not PUT '<https://oss.sonatype.org/service/local/staging/deployByRepositoryId/kpresence/io/github/reblast/kpresence/0.2.1/kpresence-0.2.1.jar>'. Received status code 401 from server: Unauthorized
should i use
maven2
instead of
deployByRepositoryId
? is there any difference?
a
publishing to Maven Central is, indeed, difficult :( Although they did completely change their upload process recently, so maybe it's improved.
I believe that the new way of publishing to Maven Central isn't compatible with the standard
maven-publish
plugin.
a
oh i see, is it the same with sonatype snapshots?
i was looking for a free public repository hosting with klib support
a
a
thank you Adam :)
a
Amazing! Well done! If you want to share your library, send a message in #feed