Creating and Deploying Local Scala SBT Packages
In the last few months we have discussed various features of scala. Some features required using other scala packages. In this post, let’s discuss how we can create our own artifacts. We can also discuss how we can use these packages in other scala projects locally.
Let’s create a sample Scala SBT project AdderLib. It is using Scala version 2.11.7 and SBT version 0.13.8. Just select auto-import to make sure that the packages are updated as soon as we modify the sbt file.
Let’s add an Adder type to the project. It just has one method which takes two operands of Double type as input. It returns the sum of the operands as Double.
In order to generate an artifact, we need to provide some details. IntelliJ Idea supports the configuration. Here we are creating JAR package with the default settings.
As we add the artifact details, the Output tab for the project updates as follows:
Now we can easily generate the artifact. Just building the project wouldn’t generate an artifact. Just use Build Artifact from IntelliJ Idea menus. The artifact is generated in the directory we specified while configuring the artifact the details. By default, it is generated as follows:
We can use sbt to publish an artifact locally. This would publish the artifact to the local ivy repository .
The published artifact can be found on users .ivy\local folder.
We can publish to local maven repository as well. SBT provides publish-m2 command to publish to maven repository.
We can use this published artifact in another scala project on the same machine. Let’s create a new project AdderUser. Here we are using ivy repository. We update the build.sbt file for the project as follows:
Now we can use all the types exported by the package. Here we are using Adder type exported by the artifact. It adds two numbers using Adder.add and prints the result.