PokeAPI Library 🚀
Tags: programming, tutorial, opensource, kotlin
Published: 2023-10-05 20:10:19

I deploy my first library on Maven Central: https://central.sonatype.com/artifact/fr.tykok/pokeapi. ✨🎉
You can use this library to wrap easily all the data from pokeapi.co API.

Here I'm gonna explain why I create <a href="https://github.com/Tykok/PokeAPI-Kotlin">`PokeAPI-Kotlin`</a>  for the [pokeapi.co](https://pokeapi.co/), and how I deploy it on Maven Central.

![PokeApi Kotlin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xglahgxohsi9vcij2tn5.png)

You can find all about this project on GitHub: [PokeApi-Kotlin](https://github.com/Tykok/PokeAPI-Kotlin).

You are free to contribute to this project 🙌🏽

## What is a library?

Many library are available on [Maven Central](https://central.sonatype.com/artifact/fr.tykok/pokeapi), but what is a library? 🤔
A library is just a piece of **reusable code** created by a developer or a team. A library can contains specific **functions**, **classes** or **modules** that can be used by other developers to simplify their work when creating software.

In short, a library is a set of pre-written codes that developers can use to solve common problems and **accelerate** the development of their software. 
It's like a **toolbox** for developers, where they can find ready-made solutions for their needs.

## Why I create a library 🆕

I create this library to improve my skills and to understand how a library is deployed to Maven Central. My goal was also to create my own tool that I would use later, and that others can use.

![https://i.giphy.com/media/XaAbmtzzz35IgW3Ntn/giphy.webp](https://i.giphy.com/media/XaAbmtzzz35IgW3Ntn/giphy.webp)

<aside>
🧑🏼‍💻 For this project, I used <a href="https://kotlinlang.org/">Kotlin</a> with <a href="https://docs.gradle.org/current/userguide/kotlin_dsl.html">Gradle Kotlin DSL</a>.
</aside>

This desire also came from a desire to create an application on the Play Store.

I wanted to create an Android application, to improve myself but also to understand how to deploy an application on the Play Store.
The aim was also to have one more app to add to my portfolio, and above all to have fun!

Problem: the Kotlin library offered on [pokeapi.co](http://pokeapi.co/) has been out of date for a while (2021) and I've been getting errors when installing dependencies.
I didn't look far to correct this problem, as I saw an opportunity to create my own library that I'd later use for my application project.

That's how I managed to deploy my very first library on Maven Central!

## How I deploy it on Maven Central

To deploy a package to Maven Central, you need to have an account on **Jira**.

<aside>
💬 Go to <a href="https://issues.sonatype.org/secure/Dashboard.jspa">Jira</a> to create your account. 
</aside>

When it’s done, you need to create an **Issue** on Jira with your logged account. When you create an issue, you need to provide many information about your project :

- `Group Id` : For me it was `fr.tykok.pokeapi`
- Project URL: Link it to your GitHub repository
- SCM URL: The URL to your source control system (just add `.git` to your GitHub URL)

<aside>
⚠️ If you use a custom domain for your `groupId` you need to prove the domain it’s your by adding a `DNS TXT record`.
If you want, you can use `io.github` for your domain if you don’t want to pay for domain name.
</aside>

### Configure your Graddle Kotlin DSL

First, you need to define two variable will be used soon at the top of your `build.gradle.kts` file. Here is mine :

```kotlin
description = "PokeApi is a simple library you can use to make request to get data about Pokémon."
group = "fr.tykok"
version = "0.0.3"

val artifact = "pokeapi"
val projectName = "PokeApi"
val sonarSnapshotUri = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
val sonarReleaseUri = "https://s01.oss.sonatype.org/content/repositories/releases/"
```

To define which URL, you need to use, I create a small function in my `build.gradle.kts` file :

```kotlin
fun getUriSonar(): String = if (version.toString().endsWith("SNAPSHOT")) {
    sonarSnapshotUri
} else {
    sonarReleaseUri
}
```

Next, you need to add this `plugins` in your `build.gradle.kts` file : 

```kotlin
plugins {
    `maven-publish`
}
```

And finally, you need to configure the **Maven publication**:

```kotlin
publishing {
    publications {
        create<MavenPublication>("library") {
            from(components["java"])
        }
    repositories {
        maven {
            url = uri(getUriSonar())
            name = projectName
            group = group
            version = version.toString()
            description = description
            credentials {
                username = project.findProperty("ossrh.username") as String?
                    ?: System.getenv("OSSRH_USERNAME")
                password = project.findProperty("ossrh.password") as String?
                    ?: System.getenv("OSSRH_PASSWORD")
            }
        }
    }
}
```

<aside>
📝 If you want, you can check the documentation for <a href="https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:publications">`MavenPublication`</a>.
</aside>

You need to add the `credentials` too in the <a href="http://gradle.properties">`gradle.properties`</a> :

```kotlin
ossrh.username="username"
ossrh.password="password"
```

<aside>
👀 Here, I use `Sytem.getenv()` function to get the credentials from Github Actions too.
</aside>

If you want, you can check the [main documentation](https://central.sonatype.org/publish/publish-guide/) I follow to publish my library.

## What's the next step ?

Next step for me… Create a Pokedex on the Play Store with this library!

![Pokedex](https://i.giphy.com/media/ZZT3Wag0jrTD3dpIfe/giphy.webp)

If you want, you can contribute to this project on [GitHub](https://github.com/Tykok/PokeMobile) !

[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/U7U7PBV82)

## Follow me
Twitter: https://twitter.com/TreportElie1
GitHub: https://github.com/Tykok
Medium: https://medium.com/@tykok