Scala Programming: An Introduction

Remember that Scala runs on the JVM, therefore the same code can be executed on almost every platform, assuming that all of the dependencies and SDK/JDK configurations remain compatible.

For a head start on the language’s syntax and features, check out the Scala Cheatsheet featured on the official Scala website.

Managing Packages, Libraries, and DependenciesMany programmers use package managers to easily automate the installation, removal, and configuration of software packages for their programs.

In the case of Scala, various build tools are used according to the requirement of the program, instead of package managers.

We will now look at some of the most used build tools, their functions, and how they affect the building of the program.

sbt — simple build toolsbt is the one of the most well known build tool used for Scala.

It was the first build tool and hence consists of a large set of plugins and integrations.

It is for this reason that it is one of the most reliable build tools used in Scala programming.

Installing sbtsbt can be downloaded and installed from different sites for different OS.

For Windows, MSI can be used for the installation.

For Mac, sbt is available on Homebrew, and can be installed by using the command brew install sbt@1.

For Linux such as RedHat or Debian, packages can be used for the installation of sbt.

Creating a New Projectsbt consists of different templates which help while creating a new project.

For example Giter8 is one such template which can be used to create a new project.

Different templates can be used according to the need of the program like for a simple project, scala-seed template can be used.

This is how a scala seed template looks like for creating a simple project:Defining the BuildA build.

sbt file located at the root of the project is used by sbt to define the build of the program.

More build definitions can be added to the project directory as Scala files.

Using the template we end up with the following code:Running the ProjectTo run a project using sbt build tool we have 2 options.

One option is to run the program using the command line shell directly.

The other option is to use the sbt-shell, which comparatively faster than using the command line.

The sbt shell can be started simply by starting sbt with the command sbt:sbt-test>Dependencies in sbt  Dependencies in sbt should be structured for better building of the program.

The dependencies are added using sbt’s DSL.

The %% notation is used to mark the dependency.

The important thing to make sure is using the correct dependency according to the Scala version.

These dependencies can be added using the library settings with the += operator.

Defining Custom Tasks:sbt allows the users to define their own tasks, resolution and definition layer.

The task key is used to define the task.

This acts as an interface with type, name and description of the task.

For example, in the above code, name is taskKey, type is string, and the description is in the parenthesis.

All that is left is to implement the interface.

Documentation, Community, and Pluginssbt documentation is very refined and complete.

Since sbt has been around for longer than most other Scala-supported build tools, its community is fairly extensive, and has acquired a large number of plugins, most of which are mentioned in the Github organization.

Cbt — Chris’ build toolCbt is an abbreviation of Chris’ Build Tool.

It was created by Christopher Vogt as an additional build tool apart from sbt.

Unlike sbt, Cbt maps the execution of task to JVM invocations and does not add its own layer in between.

Installing CbtFor the installation of cbt, cbt repository is cloned and the directory is added to the path.

Use the command git clone https://github.

com/cvogt/cbt.

git.

After the execution for the first time, it suggests using nailgun for faster execution.

In Homebrew, nailgun can be installed with the command brew install nailgun.

Creating a New ProjectAfter the installing Cbt, we can use it to create a new project.

Defining the Build If there is already a defined build file, then cbt will use it to set its build settings.

We can also let cbt create a new build file.

Running the ProjectIn sbt, we had option of using common shell or the sbt shell.

But in Cbt, there is no individual shell so the tasks has to be executed directly from the common shell.

Dependencies in Cbt sbt DSL is supported in Cbt, which helps in incorporating dependencies from README files using copy and paste.

Apart from that, ScalaDependency and MavenDependency can be used to express the dependencies, though these would have to be nested under a Resolver.

Defining Custom TasksIn Cbt, creating custom tasks is very easy.

One only need to create a new function in the build file and call the task from the Cbt afterwards.

Documentation, Community and PluginsCbt is not as vast as sbt but it does have a few plugins and a small community.

The documentation would be helpful in the start but you would have to learn on your own after some time.

MillMill is rather young build tool as compared to other but contains most of the functionalities of sbt.

It is faster, easier and simple.

In a nutshell better than sbt.

The reason for this is, with sbt one can do a thing in many ways which sometimes results in user doing many unnecessary things to do one thing.

In Mill, you only have one way.

It provides faster initialization and updates the build files automatically, in case anything is changed.

Installing MillFor windows, Mill can be installed by simply downloading the bat file.

For Linux, an AUR package can be downloaded.

For OS X, Mill is available on brew package, and can be installed from brew package with the brew install mill command.

Creating a New ProjectMill currently doesn’t have any method or mechanism to generate a new project.

Defining the BuildThe build file is defined in the root file of the project.

The above code shows how the build definition in Mill looks like.

The build structure of the project looks as follows:The structure of project in Mill is almost like the structure of Maven.

Mill doesn’t pose any restriction on the structure but instead allows the flexibility of the modules.

Its structure is also sbt compatible.

Running the ProjectMill has a module.

task file which dictates a common syntax for the project.

Dependencies in Mill Like sbt, Mill also uses DSL to add dependencies to the project, only instead of using %% for dependencies Mill uses :: notation.

Defining Custom TasksMill handles task definition, ordering and cache by using its own defined task graph abstraction.

When one task is called from another, it creates dependencies.

There are 3 different types of tasks:Targets: It is used to define where the output is generated,compiled or assembled.

Sources: It defines where the code is originated from.

Commands: it defines the things to do.

For example, a task defined as:will produce the following output:Documentation, Community and ModulesMill has a complete documentation at http://www.

lihaoyi.

com/mill/ and a very active community.

Mill uses modules instead of plugins.

Other Build ToolsThere are many build tools other than the ones we covered; too many to be discussed extensively in a single guide.

For example, there is Gradle, which can also be used as a build tool for the scala project using Gradle-Scala plugin.

There is also Fury, a fairly new build tool recently launched, whose documentation and community is only starting to grow.

We also have Maven and Polyglot Maven, which can be used if one wants to use Maven in their project.

At the time of this writing, there are also some build tools currently in early development , such as Ant, Bazel, Pants, and Make.

How to choose a Build Tool?Since each build tool specializes in different programming needs, the decisive factor in choosing the build for you should be based on your needs.

If you’re considering a specific build tool to use, checking the documentation would be a good place to start.

sbt is the most effective and popular build tool used for Scala programming.

For simple projects with straight-forward needs which are well documented, sbt can be used.

Sbt is mostly used by experienced programmers.

For beginners, using sbt for the parts which are not documented will be really difficult, so in these cases it is well suggested to use Mill instead of sbt.

Authors:Alessandro Heres — GithubTristen Sprainis — GithubAyushi Priyadarshi — Github | LinkedIn.. More details

Leave a Reply