IntelliJ Import Dependencies - Getting Started With build.sbt

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:28 pm

Overview

In this tutorial, we will show how to use SBT to import external libraries so that we can access and use them in our Scala applications.

 

As an example, we will import the Scala Logging library to allow us to log statements from our Scala application.

 

Steps

1. Quick introduction to SBT

  • What is SBT?
    • Stands for Simple Build Tool.
    • Also commonly referred to as Scala Build Tool.
    • If you've used Java in the past, think of it as a build tool similar to Ant or Ivy.
    • If you've used .NET in the past, think of it as a build tool similar to Nuget.
  • What can SBT do?
    • It comes with tasks for compiling, running, testing, packing … etc your Scala application.
  • More info
    • You can refer to the online documentation for SBT and as a matter of fact you would most certainly want to bookmark it. The documentation has useful code snippets to guide you with using various SBT tasks.

2. Open IntelliJ and our allaboutscala project
If you do not have IntelliJ installed or the allaboutscala project, please follow the previous tutorials.



intellij open scala project

 

3. Create a new package and Scala object
Let us name our package com.allaboutscala.chapter.one.tutorial_10 and also create a new Scala object named HelloWorldWithScalaLogging as follows:

 

intellij import dependencies

 

NOTE:

4. Open build.sbt file
If you recall from the Tutorial 8, the build.sbt file is located at the root of our project. Double click on the build.sbt file as shown below.

 

intellij import dependencies build sbt

 

NOTE:

  • Our build.sbt file contain a few properties including the name of our application, some default version number and the Scala version which our application is currently targeting.

5. Add Scala Logging dependency
To add the Scala Logging library, you need to add the following library dependencies in build.sbt:

"ch.qos.logback" % "logback-classic" % "1.1.7"

"com.typesafe.scala-logging" %% "scala-logging" % "3.4.0"

intellij import dependencies logging

 

NOTE:

  • Scala logging also requires a logger such as logback

Afterwards save the build.sbt file and you should notice that IntelliJ will start downloading the dependencies. There will be a progress bar in the bottom panel:

 

intellij import dependencies logging download

 

If for some reason IntelliJ does not pull down the dependencies after you've saved the build.sbt file, you can expand the SBT tab in the right panel and click on the refresh button.

 

intellij import dependencies refresh

 

6. Verify Scala Logging is in your classpath
To verify that the Scala Logging dependencies are now in our classpath you can open the module settings as described in Tutorial 9. However, let's take another shortcut and expand the External Libraries folder on the left panel:

 

intellij import dependencies external tab

 

NOTE:

  • We now have the relevant dependencies as shown above.

7. Use Scala Logging
If you've followed the previous steps, you should now have access to Scala Logging from your application. So let's use it by adding the LazyLogging trait to our HelloWorldWithScalaLogging application as follows:

 

intellij import dependencies using logging

 

NOTE:

  • We have used the with keyword to add the LazyLogging trait. In upcoming tutorials, I will cover the trait and with keyword in more details, but for now think of it as adding extra functionality to our HelloWorldWithScalaLogging application.
  • We now also have a logger variable which has an info function that takes a String which in our case is simply "Hello World from Scala Logging".




8. Run HelloWorldWithScalaLogging
To run our application, it’s the usual right click anywhere in our HelloWorldWithScalaLogging file and select Run as follows:

 

intellij import dependencies run scala application

 

In IntelliJ's console window, we will see the output of our application as follows:

 

intellij import_dependencies running application with logging

 

NOTE:

  • Unlike when we used println() from the previous tutorials,  using logger.info() from Scala Logging provides additional prepended data points such as time followed by the function and class where the logger.info() was called.
  • Let us not focus on the actual logging configurations, but instead make sure that you've now seen how to add and use external libraries.

This concludes our tutorial on IntelliJ Import Dependencies - Getting Started With build.sbt and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials.

 

Don't forget to like and share this page :)

Summary

In this article, we went over the following:

  • How to add library dependency in build.sbt file.
  • How to check that the dependencies were downloaded and added to our classpath.
  • How to import and use Scala Logging.

Tip

  • You can find additional information on the Scala Logging utility which we used in this tutorial from the GitHub page.
  • You can review our explanation of GitHub from the What is Scala programming language tutorial.
  • Another popular build tool which is also used for managing the build life cycle of Scala applications is Gradle.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will provide a bit more detail on where IntelliJ downloaded our Scala Logging artifact from, along with a brief overview of open source projects.

 

Stay tuned!

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on

  • Syed Saad

    Hello ,
    https://uploads.disquscdn.com/images/ef8dc9636b69ddc80f02c8f26382f6b0630ecf7213b3e7c7096bc63c01886624.png
    I am getting this error while trying to build it. Can you tell whats the problem with SBT?

    • allaboutscala

      Hi Syed,
      In the above screenshot, you are missing a comma after the version number for logback-classic. The dependencies should be as follows:

      libraryDependencies ++= Seq (
      "ch.qos.logback" % "logback-classic" % "1.1.7",
      "com.typesafe.scala-logging" %% "scala-logging" % "3.4.0"
      )

      -Cheers, Nadim

      • Syed Saad

        Thanks a lot!!! It saved me !!

        • Nadeem Akhtar

          Hi Nadim,

          Getting below errors while importing the dependencies. Could you please let me know what might be the problem

          libraryDependencies ++= Seq (
          “ch.qos.logback” % “logback-classic” % “1.1.7”,
          “com.typesafe.scala-logging” %% “scala-logging” % “3.4.0”
          )

          Errors :

          SBT ‘AllaboutScala’ project refresh failed
          Error:Error while importing SBT project:…[info] Resolving com.thoughtworks.paranamer#paranamer;2.6 …
          [info] Resolving org.scala-sbt#completion;0.13.15 …
          [info] Resolving org.scala-sbt#control;0.13.15 …
          [info] Resolving org.scala-sbt#sbt;0.13.15 …
          [info] Resolving org.scala-sbt#run;0.13.15 …
          [info] Resolving org.scala-sbt.ivy#ivy;2.3.0-sbt-48dd0744422128446aee9ac31aa356ee203cc9f4 …
          [info] Resolving org.scala-sbt#test-interface;1.0 …
          [info] Resolving com.jcraft#jsch;0.1.50 …
          [info] Resolving org.scala-lang#scala-compiler;2.10.6 …
          [info] Resolving jline#jline;2.14.3 …
          [info] Resolving org.scala-sbt#compiler-ivy-integration;0.13.15 …
          [info] Resolving org.scala-sbt#incremental-compiler;0.13.15 …
          [info] Resolving org.scala-sbt#logic;0.13.15 …
          [info] Resolving org.scala-sbt#main-settings;0.13.15 …
          [trace] Stack trace suppressed: run ‘last *:ssExtractDependencies’ for the full output.
          [trace] Stack trace suppressed: run ‘last *:update’ for the full output.
          [error] (*:ssExtractDependencies) sbt.ResolveException: unresolved dependency: com.typesafe.scala-logging#scala-logging_2.12;3.4.0: not found
          [error] (*:update) sbt.ResolveException: unresolved dependency: com.typesafe.scala-logging#scala-logging_2.12;3.4.0: not found
          [error] Total time: 5 s, completed Jul 4, 2017 4:29:45 PM
          Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=384M; support was removed in 8.0See complete log in file:/C:/Users/G707051/.IdeaIC2017.1/system/log/sbt.last.log

          • allaboutscala

            Hi Nadeem,

            The scala version in my example is 2.11.8. But your stack trace suggests that you are using scala version 2.12. You will need to search in the maven repository to find the scala-logging 2.12 version. https://search.maven.org/ and in the search box just look for com.typesafe.scala-logging which will list matching scala versions.

            Cheers,
            Nadim

          • Richish Jain

            Problem is 3.4.0 is not available for 2.12 in maven repo. Use 3.7.0 (or any version later that 3.5.0) instead.
            Refer- https://mvnrepository.com/artifact/com.typesafe.scala-logging/scala-logging_2.12