Scala Foundation Course - Scala IDE


Welcome back. We used sbt and Scala REPL in our prior videos. That's enough for experiment driven development. However, when your project size is growing, and you need some productivity features like code completion, semantic highlight, and visual debugging capabilities, you may want to use an IDE. Eclipse is the recommended IDE for your Scala projects. In this lesson, I will download, Install and configure Eclipse IDE for Scala. I will also cover the steps to integrate sbt with your Scala IDE. With the demonstration shown in this tutorial, you should be able to use sbt as well as Eclipse IDE simultaneously on the same project. So, let’s start.
I am using a Linux VM on my personal laptop as well as on my office desktop. Everything that I do is on a Linux VM. This approach gives me the flexibility to create separate machines for different projects and tech stacks. It is like having tens of computers at my disposal and start the one which you need for the current tech stack. But in fact, I just have a desktop or a laptop with 16 GB RAM and a quad-core processor. I recommend this approach to my team and my viewers. That's why I ignore Eclipse and SBT setup on a windows machine. In this lesson, we will install Scala IDE on a Linux machine.
The fastest method to get Scala IDE is to download and install integrated IDE from Scala IDE website. You can download the file and then un-compress it using below command.

                                
    tar -zxvf scala-SDK-4.5.0-vfinal-2.11-linux.gtk.x86_64.tar.gz -C /usr/local/                                          
                            

I am extracting it in /usr/local directory. That’s it. Installation is complete. Simple isn't it. You may want to create a shortcut for Eclipse. If you are using CentOS, follow these steps.

  1. Right click on your desktop panel.
  2. Select the add to panel menu item.
  3. Double click the custom application launcher.
  4. Give it a name.
  5. Browse the executable for Eclipse. We uncompressed it in /usr/local directory. So, select the binary file from that location.
  6. You may want to change the icon as well. Go to the same directory and pick the image file.

You can launch the Eclipse. It starts with default Scala perspective. You can create Scala project, class, object and packages. If you are familiar with Eclipse, and you know Scala, you can start using it.


Integrating SBT with Eclipse

The next thing is to integrate sbt with your IDE. Most of the time, you will start your project using sbt and experiment on Scala REPL. When you feel the need for an IDE, you can import your sbt project in Eclipse. It's a three-step process.

  1. Configure the SBT Eclipse plugin.
  2. Execute SBT Eclipse task.
  3. Import the project in Eclipse.

Let me show you all of these.
The SBT Eclipse plugin is available on GitHub . I will set up this SBT plugin for all my projects.

  1. Create a global plugin directory in your home directory.
                                
    mkdir -p ~/.sbt/0.13/plugins/                                        
                         
  1. Create a plugin configuration file.
                                
    vi ~/.sbt/0.13/plugins/plugins.sbt                                       
                         
  1. Copy the configuration and paste it into your file.
                                
    addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")                                       
                         
  1. Execute the SBT eclipse task on your project.
    Let’s create a simple project to test the plugin and Eclipse import. It says Hello World from SBT.

                                
    mkdir -p TestEclipse/src/main/scala
    cd TestEclipse
    vi src/main/scala/TestEclipse.scala
    //Copy and Paste the below code in your TestEclipse.scala file
    object TestEclipse{
        def main(args: Array[String]): Unit = {
            println("Hello, World! from SBT")
        }
    }                                           
                         
  1. Create a Scala build file
                                
    vi build.sbt
    //Copy and paste below content into your build.sbt file
    name:= "TestEclipse"
    version := "1.0"
    scalaVersion := "2.11.8"
    publishMavenStyle := false
    libraryDependencies += "org.apache.spark" % "spark-core_2.11" % "2.0.2" notTransitive()
    EclipseKeys.withJavadoc := false
    EclipseKeys.withSource := false                                         
                         

My build file looks a little complex. Right? But it's not. Let me explain.
You already know the name and the version. The next line sets Scala version. Scala 2.11.8 is the latest version of Scala at the time of recording this video. The default Scala version for my SBT is 2.10.6. I want to use Scala 2.11.x, so I added this setting into my build file.
You already know library dependency. I added this to show you that how simple it is to get the dependent jars. I am using notTransitive() option to make sure that sbt doesn't download transitive dependencies. The Spark Core has several other dependencies, and then they have further dependencies and so on. This transitive dependency goes on to several levels. In a typical case, you need all of those. But in my case, I know, It's a simple Hello World project. It doesn't even need Spark Core. I just added the dependency for the demonstration. So, I switch off the transitive dependency, and sbt will download just one jar.
Switching off the transitive dependency will show a warning message that you can't publish your project in a maven repository. So, the next setting is to tell sbt that we don't want to publish it in a maven repository. It's just to stop the warning message.
Finally, last two settings belong to the plugin. These two are again to prevent transitive jar files. The plugin tries to download the source and documentation jars for the dependencies. We don't want that, so we set them to false.
There are many other settings for the plugin, if you are interested, you can check the documentation for the plugin.
Good. You can execute this program using sbt run.
Now, I want to bring this project into Eclipse.The next step is to execute SBT Eclipse task. Execute below command.

                                
    sbt eclipse                                          
                         

If you check your project directory, you should find .classpath and .project files. You will also see a .settings directory. They define an Eclipse project. The plugin generates these files. Now, you can start Eclipse and import the project into Eclipse. That’s it.The Scala project along with source files, Scala version, dependent jars, everything loads in Eclipse. Save the project and execute it.
You can go back to sbt, change the code. Run it using sbt. Switch back to Eclipse. Refresh and execute or debug it there. If you are changing you build definition file, you have to re-execute sbt eclipse task and refresh your Eclipse project.
Great, in this session, we learned following things.


  1. How to setup Eclipse Scala IDE.
  2. How to configure SBT Plugin for Eclipse.
  3. How to generate Eclipse project from an SBT project.
  4. How to import SBT project into Eclipse.

After completing all this, you can use sbt and Eclipse both on the same Scala project. The choice is yours. You can use sbt and REPL when you just want to explore or experiment something. You can use Eclipse when you want to write some code in your source file. The only thing that you need to remember is re-execute the sbt eclipse command after modifying your build file and refresh your Eclipse project.
In some cases, you may face a problem with Eclipse Import. It may not appear always, but it shows up many times. Let me cover that as well.
If you see this red colour error sign on your Eclipse project after importing it from sbt. You won’t be able to execute the project and get this error message that can't find main class. That happens because Eclipse couldn't compile the project successfully. If you click on the problems tab to realize the reason for the failure. It says, incompatible version of Scala. In my example, Spark core is for Scala 2.11, but my project includes Scala 2.12. That's a conflict. I made sure that this issue doesn't show up by specifying Scala version in my sbt build definition file, but it looks like Eclipse failed to import that setting. You can fix this problem by changing the Scala compiler in your Eclipse project properties and that should solve this problem.
Thanks for watching learning journal. Keep learning and Keep Growing.


You will also like:


Kafka Core Concepts

Learn Apache Kafka core concepts and build a solid foundation on Apache Kafka.

Learning Journal

Hadoop Security

Hadoop security implementation using Kerberos.

Learning Journal

Free virtual machines

Get upto six free VMs in Google Cloud and learn Bigdata.

Learning Journal

Lazy Evaluations

Evaluate the expression now vs evaluate it for the first use. Strict vs Lazy?

Learning Journal

Scala named arguments

Learn about named arguments and default values in Scala functions with examples.

Learning Journal