https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
a

axeon

03/17/2024, 7:52 AM
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

Michael Paus

03/17/2024, 8:38 AM
Have you tried
publishToMavenLocal
for example? This generates all the
jars
and
klibs
for all targets I need.
a

Adam S

03/17/2024, 9:08 AM
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

axeon

03/17/2024, 11:33 AM
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

Adam S

03/17/2024, 11:41 AM
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

axeon

03/17/2024, 11:45 AM
whoops, got it
let me try again
a

Adam S

03/17/2024, 11:47 AM
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

axeon

03/17/2024, 11:52 AM
thanks! seems to be working now
although, i got this warning for all targets, is this okay?
a

Adam S

03/17/2024, 11:57 AM
ah yeah, that warning You can try this workaround https://github.com/gradle/gradle/issues/26091#issuecomment-1722947958
a

axeon

03/17/2024, 12:03 PM
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

Adam S

03/17/2024, 12:52 PM
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

axeon

03/17/2024, 12:54 PM
oh i see, is it the same with sonatype snapshots?
i was looking for a free public repository hosting with klib support
a

Adam S

03/17/2024, 12:55 PM
a

axeon

03/17/2024, 3:04 PM
thank you Adam :)
a

Adam S

03/17/2024, 3:16 PM
Amazing! Well done! If you want to share your library, send a message in #feed