diff --git a/README.md b/README.md index 662141b..a36e1cb 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,9 @@ Most of the steps here are still applicable if you’re using a different setup, 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**.** -## Prepare accounts and credentials +### Prepare accounts and credentials -### Register a namespace {#register-a-namespace} +#### Register a namespace {#register-a-namespace} 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`. @@ -59,7 +59,7 @@ To use a domain name that you own as your namespace: 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. 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. -### Generate a Key Pair +#### Generate a Key Pair 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. @@ -86,8 +86,8 @@ Next, you’ll be prompted to set the expiration of the key. If you choose to cr You will be asked for your real name, email, and a comment. You can leave the comment empty. -| `Please select what kind of key you want: (1) RSA and RSA (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) (9) ECC (sign and encrypt) *default* (10) ECC (sign only) (14) Existing key from card Your selection? 9 Please select which elliptic curve you want: (1) Curve 25519 *default* (4) NIST P-384 (6) Brainpool P-256 Your selection? 1 Please specify how long the key should be valid. 0 = key does not expire = key expires in n days w = key expires in n weeks m = key expires in n months y = key expires in n years Key is valid for? (0) 0 Key does not expire at all Is this correct? (y/N) y GnuPG needs to construct a user ID to identify your key.` | -| :---- | +| `Please select what kind of key you want:`
`(1) RSA and RSA`
`(2) DSA and Elgamal`
`(3) DSA (sign only)`
`(4) RSA (sign only)`
`(9) ECC (sign and encrypt) *default*`
`(10) ECC (sign only) `
`(14) Existing key from card`
` Your selection? 9`

`Please select which elliptic curve you want:`
`(1) Curve 25519 *default*`
`(4) NIST P-384`
`(6) Brainpool P-256`
`Your selection? 1 `

`Please specify how long the key should be valid.`
`0 = key does not expire`
` = key expires in n days`
`w = key expires in n weeks`
`m = key expires in n months`
`y = key expires in n years`
`Key is valid for? (0) 0`
`Key does not expire at all`

`Is this correct? (y/N) y `
`GnuPG needs to construct a user ID to identify your key.`| +|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| You will be asked for a passphrase to encrypt the key, which you have to repeat. Keep this passphrase stored securely and privately. You’ll be using it later to access the private key. @@ -98,12 +98,12 @@ Let’s take a look at the key we’ve created with the following command: The output will look something like this: -| `pub ed25519 2024-10-06 [SC] F175482952A225BFC4A07A715EE6B5F76620B385CE uid [ultimate] Your name sub cv25519 2024-10-06 [E]` | +| `pub ed25519 2024-10-06 [SC] `
` F175482952A225BFC4A07A715EE6B5F76620B385CE`
`uid [ultimate] Your name `
`sub cv25519 2024-10-06 [E]` | | :---- | You’ll need to use the long alphanumerical identifier of your key displayed here in the following steps. -### Upload the public key +#### Upload the public key You need to [upload the public key to a keyserver](https://central.sonatype.org/publish/requirements/gpg/#distributing-your-public-key) for it to be accepted by Maven Central. There are multiple available keyservers, we’ll use `keyserver.ubuntu.com` as a default choice. @@ -112,7 +112,7 @@ Run the following command to upload your public key using `gpg`, **substituting | `gpg --keyserver keyserver.ubuntu.com --send-keys F175482952A225BFC4A07A715EE6B5F76620B385CE` | | :---- | -### Export your private key {#export-your-private-key} +#### Export your private key {#export-your-private-key} 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. @@ -123,43 +123,43 @@ This will create a `key.gpg` file which contains your private key. Remember not If you check the contents of the file, you should see contents similar to this: -| `-----BEGIN PGP PRIVATE KEY BLOCK----- lQdGBGby2X4BEACvFj7cxScsaBpjty60ehgB6xRmt8ayt+zmgB8p+z8njF7m2XiN bpD/h7ZI7FC0Db2uCU4CYdZoQVl0MNNC1Yr56Pa68qucadJhY0sFNiB63KrBUoiO ... SQ== =Qh2r -----END PGP PRIVATE KEY BLOCK-----` | +| `-----BEGIN PGP PRIVATE KEY BLOCK-----`
`lQdGBGby2X4BEACvFj7cxScsaBpjty60ehgB6xRmt8ayt+zmgB8p+z8njF7m2XiN`
`bpD/h7ZI7FC0Db2uCU4CYdZoQVl0MNNC1Yr56Pa68qucadJhY0sFNiB63KrBUoiO `
`... SQ== =Qh2r`
`-----END PGP PRIVATE KEY BLOCK-----` | | :---- | -### Generate the user token {#generate-the-user-token} +#### Generate the user token {#generate-the-user-token} 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*. The output will look like the example below, containing a username and a password. Store this information securely, as it can’t be viewed again on the Central Portal. If you lose these credentials, you’ll need to generate new ones later. -| ` ${server} l3nfaPmz gh9jT9XfnGtUngWTZwTu/8241keYdmQpipqLPRKeDLTh ` | -| :---- | +| ``
` ${server}`
` l3nfaPmz`
` `
`
` | +|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -## Configure the project +### Configure the project -### Prepare your library project +#### Prepare your library project If you started developing your library from a template project, this is a good time to change any default names in the project to match your own library’s name. This includes the name of your library module, and the name of the root project in your top-level `build.gradle.kts` file. 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. -| `android { namespace = "io.github.kotlinhandson.fibonacci" }` | -| :---- | +| `android {`
` namespace = "io.github.kotlinhandson.fibonacci"`
`}` | +|:-----------------------------------------------------------------------------| -## Set up the publishing plugin +#### Set up the publishing plugin This guide uses [vanniktech/gradle-maven-publish-plugin](https://github.com/vanniktech/gradle-maven-publish-plugin) to help with publications to Maven Central. You can read more about the advantages of the plugin [here](https://vanniktech.github.io/gradle-maven-publish-plugin/#advantages-over-maven-publish). See the [plugin’s documentation](https://vanniktech.github.io/gradle-maven-publish-plugin/central/) to learn more about its usage and available configuration options. To add the plugin to your project, add the following line in the plugins block, in your library module’s `build.gradle.kts` file: -| `plugins { id("com.vanniktech.maven.publish") version "0.29.0" }` | +| `plugins {`
` id("com.vanniktech.maven.publish") version "0.29.0" `
`}` | | :---- | *Note: for the latest available version of the plugin, check its [releases page](https://github.com/vanniktech/gradle-maven-publish-plugin/releases).* In the same file, add the following configuration. Customize all these values appropriately for your library. -| `mavenPublishing { publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL) signAllPublications() coordinates("io.github.kotlin-hands-on", "fibonacci", "1.0.3") pom { name.set("Fibonacci library") description.set("A mathematics calculation library.") inceptionYear.set("2024") url.set("https://github.com/kotlin-hands-on/fibonacci/") licenses { license { name.set("The Apache License, Version 2.0") url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") distribution.set("http://www.apache.org/licenses/LICENSE-2.0.txt") } } developers { developer { id.set("kotlin-hands-on") name.set("Kotlin Developer Advocate") url.set("https://github.com/kotlin-hands-on/") } } scm { url.set("https://github.com/kotlin-hands-on/fibonacci/") connection.set("scm:git:git://github.com/kotlin-hands-on/fibonacci.git") developerConnection.set("scm:git:ssh://git@github.com/kotlin-hands-on/fibonacci.git") } } }` | +| `mavenPublishing {`
` publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)`
` signAllPublications()`
` coordinates("io.github.kotlin-hands-on", "fibonacci", "1.0.3")`
` pom {`
` name.set("Fibonacci library")`
` description.set("A mathematics calculation library.")`
` inceptionYear.set("2024")`
` url.set("https://github.com/kotlin-hands-on/fibonacci/")`
` licenses {`
` license {`
` name.set("The Apache License, Version 2.0")`
` url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")`
` distribution.set("http://www.apache.org/licenses/LICENSE-2.0.txt")`
` }`
` }`
` developers {`
` developer {`
` id.set("kotlin-hands-on")`
` name.set("Kotlin Developer Advocate")`
` url.set("https://github.com/kotlin-hands-on/")`
` }`
` }`
` scm {`
` url.set("https://github.com/kotlin-hands-on/fibonacci/")`
` connection.set("scm:git:git://github.com/kotlin-hands-on/fibonacci.git")`
` developerConnection.set("scm:git:ssh://git@github.com/kotlin-hands-on/fibonacci.git")`
` }`
` }`
` }` | | :---- | Note that it’s also possible to use Gradle properties instead. @@ -171,15 +171,15 @@ Some of the most important, required settings here are: * The [developer information](https://central.sonatype.org/publish/requirements/#developer-information) which lists the authors of the library. * [SCM (Source Code Management) information](https://central.sonatype.org/publish/requirements/#scm-information), which specifies where the sources of your library are available. -# Publish to Maven Central from Continuous Integration +### Publish to Maven Central from Continuous Integration -## Add a GitHub Actions workflow to your project +#### Add a GitHub Actions workflow to your project You can set up continuous integration which builds and publishes your library for you. We’ll use [GitHub Actions](https://docs.github.com/en/actions) as an example. To get started, add the following workflow to your repository, in the `.github/workflows/publish.yml` file. -| `name: Publish on: release: types: [released, prereleased] jobs: publish: name: Release build and publish runs-on: macOS-latest steps: - name: Check out code uses: actions/checkout@v4 - name: Set up JDK 21 uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: 21 - name: Publish to MavenCentral run: ./gradlew publishToMavenCentral --no-configuration-cache # Note: Disable Configuration Cache because of https://github.com/gradle/gradle/issues/22779 env: ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }} ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }} ORG_GRADLE_PROJECT_signingInMemoryKeyId: ${{ secrets.SIGNING_KEY_ID }} ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }} ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_KEY_CONTENTS }}` | +| `name: Publish`
` on:`
` release:`
` types: [released, prereleased]`
` jobs:`
` publish:`
` name: Release build and publish`
` runs-on: macOS-latest`
` steps:`
` - name: Check out code`
` uses:`
` actions/checkout@v4`
` - name: Set up JDK 21`
` uses:`
` actions/setup-java@v4`
` with:`
` distribution: 'zulu'`
` java-version: 21`
` - name: Publish to MavenCentral`
` run: ./gradlew publishToMavenCentral --no-configuration-cache`
` # Note: Disable Configuration Cache because of https://github.com/gradle/gradle/issues/22779`
` env:`
` ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_CENTRAL_USERNAME }}`
` ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}`
` ORG_GRADLE_PROJECT_signingInMemoryKeyId: ${{ secrets.SIGNING_KEY_ID }}`
` ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_PASSWORD }}`
` ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.GPG_KEY_CONTENTS }}` | | :---- | 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. @@ -192,7 +192,7 @@ After committing and pushing this change, this workflow will run automatically w 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. -## Add secrets to GitHub +### Add secrets to GitHub To use the keys and credentials required for publication in your GitHub Action workflow while keeping them private, you need to place those values into secrets. From your GitHub repository, go to `Settings` \> `(Security) Secrets and variables > Actions`. @@ -203,24 +203,34 @@ Click on the `New repository secret` button, and add the following secrets: - `SIGNING_PASSWORD` is the passphrase you’ve provided when generating your signing key. - `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. -![][image1] +# + +![](/images/image5.png) + Note again that the names used for these secrets must match those used by the workflow that accesses their values. -### Create a release on GitHub +#### Create a release on GitHub With the workflow and secrets set up, you’re now ready to [create a release](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository#creating-a-release) that will trigger the publication of your library. Go to your GitHub repository’s main page, and click on Releases in the menu in the right sidebar. -![][image2] + +![](/images/image3.png) + Click *Draft a new release*. -![][image3] + +![](/images/image7.png) + Each release creates a new tag. Set the name for the tag to be created, and set a name for the release (these may be identical). Note that setting a version here does not change the version of your coordinates configured in your `build.gradle.kts` file, so you should update that version before creating a new release. -![][image4] + +![](/images/image4.png) Double-check the branch you want to target with the release (especially if you want to release from a branch that’s different from your default), and add appropriate release notes for your new version. The checkboxes below allow you to mark a release as a pre-release (useful for alpha, beta, or RC versions of a library), or to set the release as the latest available one: -![][image5] + +![](/images/image6.png) + Click the *Publish release* button to create the new release. This will immediately show up on your GitHub repository’s main page. 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. @@ -229,7 +239,7 @@ After this task completes successfully, navigate to the [Deployments](https://ce 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. -![][image6] +![](/images/image2.png) 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. Also note that the library may be available for use before the website is updated. @@ -238,25 +248,8 @@ There’s also another task available which both uploads and releases the artifa | `./gradlew publishAndReleaseToMavenCentral` | | :---- | -Et voilà, you have successfully published your library to Maven Central. +**Et voilà, you have successfully published your library to Maven Central.** # Other resources - * [Publishing via the Central Portal](https://central.sonatype.org/publish-ea/publish-ea-guide/) -* [Gradle Maven Publish Plugin \- Publishing to Maven Central](https://vanniktech.github.io/gradle-maven-publish-plugin/central/) - -# - -[image1]: - -[image2]: - -[image3]: - -[image4]: - -[image5]: - -[image6]: - -[image7]: +* [Gradle Maven Publish Plugin \- Publishing to Maven Central](https://vanniktech.github.io/gradle-maven-publish-plugin/central/) \ No newline at end of file diff --git a/images/image1.png b/images/image1.png new file mode 100644 index 0000000..cf4efb0 Binary files /dev/null and b/images/image1.png differ diff --git a/images/image2.png b/images/image2.png new file mode 100644 index 0000000..088ac0a Binary files /dev/null and b/images/image2.png differ diff --git a/images/image3.png b/images/image3.png new file mode 100644 index 0000000..64a9175 Binary files /dev/null and b/images/image3.png differ diff --git a/images/image4.png b/images/image4.png new file mode 100644 index 0000000..737d45e Binary files /dev/null and b/images/image4.png differ diff --git a/images/image5.png b/images/image5.png new file mode 100644 index 0000000..0118ec8 Binary files /dev/null and b/images/image5.png differ diff --git a/images/image6.png b/images/image6.png new file mode 100644 index 0000000..8397607 Binary files /dev/null and b/images/image6.png differ diff --git a/images/image7.png b/images/image7.png new file mode 100644 index 0000000..1687c97 Binary files /dev/null and b/images/image7.png differ