Today I’ll explain all the required steps and wiring to create a Scala library and publish it using SBT in this new cool feature of GitHub called GitHub Packages. The process is conceptually simple but requires several steps, configuration files and the like, so it’s easy to lose a lot of time until everything is working as expected. To help in this process (and have a simple guide ready for my future-self), I created this step-by-step tutorial. The resulting code is available here: https://github.com/supermanue/example-library
This said, let’s go!
- Create the repo. This is done in GitHub -> new Repository. This one will be “example-library”. I created it as public, but it can be private too.
- As you want to automatically upload the package from your laptop to GitHub, you have to identify yourself. This is done by creating a Token in GitHub and placing it in the appropriate folder of your local machine.
- - Create token: anywhere in GitHub click on your user -> Settings -> Developer Settings -> Personal access tokens -> Senerate new token. It needs the following permissions: repo.*, write:packages
- - Store this token in $HOME/.sbt/1.0/github.sbt, so SBT can read it automatically. The full content of the file should be:
- fork the project, download it with your favorite IDE, and start coding. I have created only a file, stored under /src/main/scala/org/supermanue/exampleLibrary/ExampleClass.scala
- Currently, GitHub offers support to Maven but not to SBT. To do so we have to use a third party plugin. It is configured by creating this file in /project/plugins.sbt
- Create build.sbt file and place it on /. Note the githubOwner and githubRepository parameters. Also, if you are using IntelliJ and everything is red in this file, restart the application so it detects that the project is using sbt
- commit and push everything to GitHub. This is not mandatory, but as we are publishing a package it makes sense that the code is available too
- in the SBT shell, or in the SBT menu of IntelliJ, press/write publish
and that’s it! Your brand new package should be available for everybody to donwload an use. The one created in this tutorial is freely available here: https://github.com/supermanue/example-library/packages/141288
If you want to use the package, the process is really easy. Remember to document these steps in your README, so the users know what to do.
- you’ll need to add an external resolver. This means that in the build.sbt of your project using this library, you should have something like:
- and after that, use it in your project with
Voila! We have successfully created a library, uploaded it to GitHub and used importing it into another project, all without leaving our favorite IDE.