In the previous post, we discussed how to set up a maven-based Scala project. Now, in this post, we will learn how we can create an sbt-based Scala project using IntelliJ IDEA IDE. The sbt is an open-source build tool for Scala and Java projects like Maven and Ant. If you need to install IntelliJ IDEA and set it up for Scala development, you can read this post.
These are the steps that need to be followed to create a Scala sbt project using IntelliJ IDEA:
- Download and install IntelliJ IDEA CE – (Covered in this post)
- Install Scala language plugin – (Covered in the previous post)
- Create a new sbt project – (Will be discussed in this post)
- Build and Run the sbt project – (Discussed in this post)
1. Donwload and install IntelliJ IDEA CE
We have already discussed this in detail in the previous post.
2. Install Scala language plugin
We have already discussed this in detail in the previous post.
3. Create a new sbt project
We have discussed points 1 and 2 in the previous post. Here, we will discuss how we can set up an sbt based Scala project. First, we need to open the IntelliJ IDEA application and select Projects in the left pane. Then, click on the “New Project” button located in the right pane.
Next, a new project dialog box appears. We need to choose “Scala” in the left pane and “sbt” in the right pane and then we need to click on the “Next” button.
On the next screen, we need to define the project name, project location, Java SDK, sbt version, scala version, and package prefix (if needed) values. Once, all these values are provided, we can click on the Finish button at the bottom. This screen looks like this.
This will create a new sbt based scala project. The overall structure of the project looks like this.
Now, we can add a scala object/class file to the project and start building it. In order to add an App.scala object, right-click on the scala folder and choose New -> Scala Class.
It will ask for the scala object name and type. Provide a name for the object and select Object as type.
Finally, we need to add some source code to the App.scala file. We can open it by double-clicking on this file in the File Explorer and then we can add the below sample code in it.
package com.sqlrelease.application
object App {
def main(args: Array[String]): Unit = {
println("Hello world!")
}
}
4. Build and Run the sbt project
Now, we can click on the Build menu and choose the Build Project option in order to build this sbt-based scala project.
Similarly, to run the App.scala file, we can click on the Run menu and select the Run App option. This will print the “Hello world!” in the output.
Thanks for the reading. Please share your inputs in the comment section.