IntelliJ Project Structure - Getting Started with Scala Project
Overview
In this tutorial, we will go over the main components that make up a Scala project.
Steps
1. Open allaboutscala project
Open IntelliJ IDE and on the welcome screen you should see the allaboutscala project which we created in Scala Hello World in IntelliJ tutorial.
NOTE:
- If you do not have IntelliJ installed, you can follow the instructions from Install IntelliJ tutorial.
2. src/main/scala
In the Project panel of IntelliJ, you will see the src/main/scala folder. This is the location where you will create your Scala files such as classes, traits, objects and package objects. For now, you should see the HelloWorld object which we created in Scala Hello World in IntelliJ tutorial.
NOTE:
- What are classes, traits, objects and package objects?
I will go over these terms in more details in upcoming tutorials, but let's have a quick explanation for each one:
A class is conceptually similar to any other classes if you have done Java or .NET in that it is a placeholder for your methods or functions.
A trait is most certainly going to be a new term if you have never done any Scala. You can think of it as having similar behaviour to an interface in Java and or .NET in that it is a contract for your method or function signatures. However, a trait provides higher semantics than an interface which we will discuss later.
An object in Scala is a singleton! This means there will be only one instance of this object within your application/JVM. The singleton pattern is such a commonly used pattern that Scala has built-in support for it.
A package object will also be a new term if you have never done any Scala. It is a placeholder where you can store and expose globally scoped objects and functions such as implicit conversion.
3. src/main/java
Scala has built-in support to interoperate with Java classes and as such you should place your Java classes under the src/main/java folder.
4. src/main/resources
Under this folder, you can add any configuration files which would be visible in your Scala application's classpath. As an example, if you were writing an Akka application, you would add your application.conf file under src/main/resources.
5. src/test
The src/test folder has a similar folder hierarchy as src/main namely:
- src/test/java
- src/test/resources
- src/test/scala
6. src/target
The src/target folder is where IntelliJ will keep the compiled versions of your Scala application files. As such, the files and folders within the target directory is automatically managed by IntelliJ and you should not place any of your application files here.
7. build.sbt
The build.sbt file is a very important file as it is used by SBT (Scala Build Tool) to essentially package your application. If you are not familiar with SBT, no need to worry, I will go over it later.
8. project folder
IntelliJ also creates the project folder which has two important files namely:
- build.properties
- plugins.sbt
NOTE:
- The build.properties file currently has only the SBT version which you are using.
- The plugins.sbt file is pretty much empty at the moment, and I will show how it can be used in later tutorials.
9. .idea
IntelliJ also creates a .idea folder which it uses to store your general information about your workspace. You should not place any of your application files in this folder as it is managed by IntelliJ.
This concludes our tutorial on IntelliJ Project Structure - Getting Started with Scala Project 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:
- The main folders created by IntelliJ for your Scala application.
Tip
- The main folders which you will be using are:
src/main/java
src/main/resources
src/main/scala
src/test/java
src/test/resources
src/test/scala
The main configuration files which you will be using are:
build.sbt
plugins.sbt
build.properties
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, we will discuss in more detail the application classpath for your Scala project.
Share this article on