DEV Community

Cover image for PokeAPI Library πŸš€
Tykok
Tykok

Posted on

PokeAPI Library πŸš€

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 PokeAPI-Kotlin for the pokeapi.co, and how I deploy it on Maven Central.

PokeApi Kotlin

You can find all about this project on GitHub: PokeApi-Kotlin.

You are free to contribute to this project πŸ™ŒπŸ½

What is a library?

Many library are available on Maven Central, 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

πŸ§‘πŸΌβ€πŸ’» For this project, I used Kotlin with Gradle Kotlin DSL.

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 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.

πŸ’¬ Go to Jira to create your account.

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)
⚠️ 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.

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 :

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/"
Enter fullscreen mode Exit fullscreen mode

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

fun getUriSonar(): String = if (version.toString().endsWith("SNAPSHOT")) {
    sonarSnapshotUri
} else {
    sonarReleaseUri
}
Enter fullscreen mode Exit fullscreen mode

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

plugins {
    `maven-publish`
}
Enter fullscreen mode Exit fullscreen mode

And finally, you need to configure the Maven publication:

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")
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“ If you want, you can check the documentation for MavenPublication.

You need to add the credentials too in the gradle.properties :

ossrh.username="username"
ossrh.password="password"
Enter fullscreen mode Exit fullscreen mode

πŸ‘€ Here, I use Sytem.getenv() function to get the credentials from Github Actions too.

If you want, you can check the main documentation 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

If you want, you can contribute to this project on GitHub !

ko-fi

Follow me

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

Top comments (0)