You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is the barebones library project intended to quickly bootstrap a Kotlin Multiplatform library, that is deployable to Maven Central.
7
+
This repository contains a simple library project, intended to demonstrate a [Kotlin Multiplatform](https://kotlinlang.org/docs/multiplatform.html) library that is deployable to [Maven Central](https://central.sonatype.com/).
8
8
9
-
It has only one function: generate the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence) starting from platform-provided numbers. Also, it has a test for each platform just to be sure that tests run.
9
+
The library has only one function: generate the [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence) starting from platform-provided numbers. Also, it has a test for each platform just to be sure that tests run.
10
10
11
-
Note that no other actions or tools usually required for the library development are set up, such as [tracking of backwards compatibility]
12
-
(https://kotlinlang.org/docs/jvm-api-guidelines-backward-compatibility.html#tools-designed-to-enforce-backward-compatibility), explicit API mode,
13
-
licensing, contribution guideline, code of conduct and others. You can find a guide for best practices for designing Kotlin libraries [here.](https://kotlinlang.org/docs/api-guidelines-introduction.html)
11
+
Note that no other actions or tools usually required for the library development are set up, such as [tracking of backwards compatibility](https://kotlinlang.org/docs/jvm-api-guidelines-backward-compatibility.html#tools-designed-to-enforce-backward-compatibility), explicit API mode, licensing, contribution guideline, code of conduct and others. You can find a guide for best practices for designing Kotlin libraries [here](https://kotlinlang.org/docs/api-guidelines-introduction.html).
14
12
15
13
## How to publish?
16
14
17
15
This guide describes the steps of publishing a library built with Kotlin Multiplatform to the [Maven Central repository](https://central.sonatype.com/). To publish your library, you’ll need to:
18
16
19
-
* Set up credentials, including an account on Maven Central and a PGP key to use for signing
20
-
* Configure the publishing plugin in your library’s project
21
-
* Provide your credentials to the publishing plugin so it can sign and upload your artifacts
22
-
* Run the publication task, either locally or using continuous integration
17
+
* Set up credentials, including an account on Maven Central and a PGP key to use for signing.
18
+
* Configure the publishing plugin in your library’s project.
19
+
* Provide your credentials to the publishing plugin so it can sign and upload your artifacts.
20
+
* Run the publication task, either locally or using continuous integration.
23
21
24
22
This guide assumes that you are:
25
23
@@ -31,11 +29,11 @@ This guide assumes that you are:
31
29
32
30
Most of the steps here are still applicable if you’re using a different setup, but there might be some differences you need to account for. An [important limitation](https://kotlinlang.org/docs/multiplatform-publish-lib.html#host-requirements) is that Apple targets must be built on a machine with macOS.
33
31
34
-
Throughout this guide, we’ll use the [https://github.com/kotlin-hands-on/fibonacci](https://github.com/kotlin-hands-on/fibonacci) repository as an example. You can refer to the code of this repository to see how the publishing setup works. Don’t forget to **replace all example values with your own** as you’re configuring your project.
32
+
Throughout this guide, we’ll use the [https://github.com/kotlin-hands-on/fibonacci](https://github.com/kotlin-hands-on/fibonacci) repository as an example. You can refer to the code of this repository to see how the publishing setup works. You **must replace all example values with your own** as you’re configuring your project.
35
33
36
34
### Prepare accounts and credentials
37
35
38
-
#### Register a namespace {#register-a-namespace}
36
+
#### Register a namespace
39
37
40
38
Artifacts published to Maven repositories are identified by their coordinates, for example `com.example:library:1.0.0`. These coordinates are made up of three parts, separated by colons: the `groupId`, `artifactId`, and `version`.
41
39
@@ -46,27 +44,27 @@ To get started with publishing to Maven Central, sign in (or create a new accoun
46
44
**For a GitHub repository**
47
45
Using your GitHub account to create a namespace is a good option if you don’t own a domain name to use for publication. To create a namespace based on your GitHub account:
48
46
49
-
1. Enter io.github.`<your username>` as your namespace. For example, `io.github.kotlin-hands-on`.
47
+
1. Enter `io.github.<your username>` as your namespace. For example, `io.github.kotlin-hands-on`.
50
48
2. Copy the Verification Key displayed.
51
49
3. On GitHub, create a new repository with your GitHub account with the verification key as the repository’s name. For example, `http://github.com/kotlin-hands-on/ex4mpl3c0d`.
52
-
4. Navigate back to Maven Central, and click on the Verify Namespace button. After a successful verification, you can delete the repository you’ve created.
50
+
4. Navigate back to Maven Central, and click on the Verify Namespace button. After verification succeeds you can delete the repository you’ve created.
53
51
54
52
**For a domain name**
55
53
To use a domain name that you own as your namespace:
56
54
57
-
1. Enter your domain as the namespace using reverse-DNS form. If your domain is `example.com`, enter `com.example`.
55
+
1. Enter your domain as the namespace using a reverse-DNS form. If your domain is `example.com`, enter `com.example`.
58
56
2. Copy the Verification Key displayed.
59
57
3. Create a new DNS TXT record with the verification key as its contents. See [Maven Central’s FAQ](https://central.sonatype.org/faq/how-to-set-txt-record/) for more information on how to do this with various domain registrars.
60
-
4. Navigate back to Maven Central, and click on the Verify Namespace button. After a successful verification, you can delete the TXT record you’ve created.
58
+
4. Navigate back to Maven Central, and click on the Verify Namespace button. After verification succeeds you can delete the TXT record you’ve created.
61
59
62
60
#### Generate a Key Pair
63
61
64
62
Artifacts published to Maven Central [must be signed with a PGP signature](https://central.sonatype.org/publish/requirements/gpg/), which allows users to validate the origin of artifacts.
65
63
66
64
To get started with signing, you’ll need to generate a key pair:
67
65
68
-
* The **private key** is used to sign your artifacts, and should never be shared with others
69
-
* The **public key** can be used by others to validate the signature of the artifacts, and should be published
66
+
* The **private key** is used to sign your artifacts, and should never be shared with others.
67
+
* The **public key** can be used by others to validate the signature of the artifacts, and should be published.
70
68
71
69
The `gpg` tool that can manage signatures for you is available from [their website](https://gnupg.org/download/index.html). You can also install it using package managers such as [Homebrew](https://brew.sh/):
72
70
@@ -88,7 +86,7 @@ Next, you’ll be prompted to set the expiration of the key. If you choose to cr
88
86
89
87
You will be asked for your real name, email, and a comment. You can leave the comment empty.
90
88
91
-
```bash
89
+
```text
92
90
Please select what kind of key you want:
93
91
(1) RSA and RSA
94
92
(2) DSA and Elgamal
@@ -98,6 +96,7 @@ Please select what kind of key you want:
98
96
(10) ECC (sign only)
99
97
(14) Existing key from card
100
98
Your selection? 9
99
+
101
100
Please select which elliptic curve you want:
102
101
(1) Curve 25519 *default*
103
102
(4) NIST P-384
@@ -113,7 +112,7 @@ Please specify how long the key should be valid.
113
112
Key is valid for? (0) 0
114
113
Key does not expire at all
115
114
116
-
`Is this correct? (y/N) y
115
+
Is this correct? (y/N) y
117
116
GnuPG needs to construct a user ID to identify your key.
118
117
```
119
118
@@ -127,7 +126,7 @@ gpg --list-keys
127
126
128
127
The output will look something like this:
129
128
130
-
```bash
129
+
```text
131
130
pub ed25519 2024-10-06 [SC]
132
131
F175482952A225BFC4A07A715EE6B5F76620B385CE
133
132
uid [ultimate] Your name <your email address>
@@ -146,27 +145,30 @@ Run the following command to upload your public key using `gpg`, **substituting
#### Export your private key {#export-your-private-key}
148
+
#### Export your private key
150
149
151
150
To let your Gradle project access your private key, you’ll need to export it to a file. Use the following command, **passing in your own keyid** as a parameter. You will be prompted to enter the passphrase you’ve used when creating the key.
#### Generate the user token {#generate-the-user-token}
171
+
#### Generate the user token
170
172
171
173
Your project will also need to authenticate with Maven Central to upload artifacts. On the Central Portal, navigate to the [Account](https://central.sonatype.com/account) page, and click on *Generate User Token*.
172
174
@@ -176,7 +178,7 @@ The output will look like the example below, containing a username and a passwor
@@ -189,6 +191,8 @@ If you started developing your library from a template project, this is a good t
189
191
If you have an Android target in your project, you should follow the [steps to prepare your Android library release](https://developer.android.com/build/publish-library/prep-lib-release). This, at a minimum, requires you to [specify an appropriate namespace](https://developer.android.com/build/publish-library/prep-lib-release#choose-namespace) for your library, so that a unique R class will be generated when their resources are compiled. Notice that the namespace is different from the Maven namespace created in the [Register a namespace](#register-a-namespace) section above.
190
192
191
193
```kotlin
194
+
// build.gradle.kts
195
+
192
196
android {
193
197
namespace ="io.github.kotlinhandson.fibonacci"
194
198
}
@@ -201,6 +205,8 @@ This guide uses [vanniktech/gradle-maven-publish-plugin](https://github.com/vann
201
205
To add the plugin to your project, add the following line in the plugins block, in your library module’s `build.gradle.kts` file:
202
206
203
207
```kotlin
208
+
// build.gradle.kts
209
+
204
210
plugins {
205
211
id("com.vanniktech.maven.publish") version "0.29.0"
206
212
}
@@ -211,6 +217,8 @@ plugins {
211
217
In the same file, add the following configuration. Customize all these values appropriately for your library.
@@ -264,6 +272,8 @@ You can set up continuous integration which builds and publishes your library fo
264
272
To get started, add the following workflow to your repository, in the `.github/workflows/publish.yml` file.
265
273
266
274
```yaml
275
+
# .github/workflows/publish.yml
276
+
267
277
name: Publish
268
278
on:
269
279
release:
@@ -292,11 +302,12 @@ jobs:
292
302
293
303
After committing and pushing this change, this workflow will run automatically when you create a release (including a pre-release) in the GitHub repository hosting your project. It checks out the current version of your code, sets up a JDK, and then runs the `publishToMavenCentral` Gradle task.
294
304
295
-
\> Alternatively, you could configure the workflow to [trigger when a tag is pushed](https://stackoverflow.com/a/61892639) to your repository.
296
-
297
-
\> The script above disables Gradle [configuration cache](https://docs.gradle.org/current/userguide/configuration_cache.html) for the publication task by adding `--no-configuration-cache` to the Gradle command, as the publication plugin does not support it (see this [open issue](https://github.com/gradle/gradle/issues/22779)).
298
-
299
-
\> Reminder: When using `publishToMavenCentral`, you’ll still need to check and release your deployment manually on the website, as described in the previous section. You may use `publishAndReleaseToMavenCentral` instead for a fully automated release.
305
+
> [!NOTE]
306
+
> Alternatively, you could configure the workflow to [trigger when a tag is pushed](https://stackoverflow.com/a/61892639) to your repository.
307
+
>
308
+
> The script above disables Gradle [configuration cache](https://docs.gradle.org/current/userguide/configuration_cache.html) for the publication task by adding `--no-configuration-cache` to the Gradle command, as the publication plugin does not support it (see this [open issue](https://github.com/gradle/gradle/issues/22779)).
309
+
>
310
+
> Reminder: When using `publishToMavenCentral`, you’ll still need to check and release your deployment manually on the website, as described in the previous section. You may use `publishAndReleaseToMavenCentral` instead for a fully automated release.
300
311
301
312
This action will need your signing details and your Maven Central credentials. These will be configured as GitHub Actions secrets in the next section. The configuration of the workflow above takes these secrets and places them into environment variables, which will make them available to the Gradle build automatically.
302
313
@@ -311,8 +322,6 @@ Click on the `New repository secret` button, and add the following secrets:
311
322
- `SIGNING_PASSWORD`is the passphrase you’ve provided when generating your signing key.
312
323
- `GPG_KEY_CONTENTS`should contain the contents of your GPG private key file, which you’ve created earlier in the [Export your private key](#export-your-private-key) section.
313
324
314
-
#
315
-
316
325

317
326
318
327
Note again that the names used for these secrets must match those used by the workflow that accesses their values.
@@ -341,18 +350,18 @@ The checkboxes below allow you to mark a release as a pre-release (useful for al
341
350
342
351
Click the *Publish release* button to create the new release. This will immediately show up on your GitHub repository’s main page.
343
352
344
-
Click the Actions tab on the top of your GitHub repository. Here you’ll see the new workflow that was triggered by the GitHub release. Click it to see the outputs of the publication task.
353
+
Click the Actions tab on the top of your GitHub repository. Here you’ll see the new workflow was triggered by the GitHub release. Click it to see the outputs of the publication task.
345
354
346
355
After this task completes successfully, navigate to the [Deployments](https://central.sonatype.com/publishing/deployments) dashboard. You should see a new deployment here. This deployment will be in the *pending* and *validating* states for some time while Maven Central performs checks on it.
347
356
348
357
Once your deployment moves to a *validated* state, you should see that it contains all the artifacts you’ve uploaded. If everything looks correct, click the *Publish* button to release these artifacts.
349
358
350
359

351
360
352
-
Note that it will take some time (about 15-30 minutes, usually) after the release for the artifacts to be available publicly on Maven Central.
361
+
Note that it will take some time (about 15–30 minutes, usually) after the release for the artifacts to be available publicly on Maven Central.
353
362
Also note that the library may be available for use before they are indexed on [the Maven Central website](https://central.sonatype.com/).
354
363
355
-
There’s also another task available which both uploads and releases the artifacts automatically once the the deployment is verified, without having to manually release them on the website:
364
+
There’s also another task available which both uploads and releases the artifacts automatically once the deployment is verified, without having to manually release them on the website:
356
365
357
366
```bash
358
367
./gradlew publishAndReleaseToMavenCentral
@@ -369,4 +378,4 @@ There’s also another task available which both uploads and releases the artifa
369
378
370
379
# Other resources
371
380
* [Publishing via the Central Portal](https://central.sonatype.org/publish-ea/publish-ea-guide/)
372
-
* [Gradle Maven Publish Plugin \- Publishing to Maven Central](https://vanniktech.github.io/gradle-maven-publish-plugin/central/)
381
+
* [Gradle Maven Publish Plugin \- Publishing to Maven Central](https://vanniktech.github.io/gradle-maven-publish-plugin/central/)
0 commit comments