Every Scala developer out there knows
SBT
is the default build tool for Scala but there may be some situations limiting
you to stick with the famous build tool for Java:
Apache Maven. Haven't come across a reason to use Maven for Scala projects yet? Keep
using SBT. However, if your team decides to use Apache Maven because it works
well with their CI server or because they have a well-established development
environment with Apache Maven, you have no choice and this article is here to
help you.
Creating a Scala project using Apache Maven in the command-line is easy. Just
run the following command, answer the questions in the interactive shell and
you are good to go. Install Apache Maven on Linux to run mvn from the terminal.
mvn archetype:generate -DarchetypeGroupId=net.alchim31.maven -DarchetypeArtifactId=scala-archetype-simple
However, why should we switch to the terminal just to create a project and
then switch back to the IntelliJ Idea and import the project to continue the
development? In this article, you will see how to prepare your IntelliJ Idea
to create Scala projects with Apache Maven.
First comes the core requirements: Of course, you need IntelliJ Idea installed
on your system. If you are using a Linux machine or VM for development, follow
the "Install IntelliJ IDEA on Linux" article to install the latest IntelliJ Idea. Scala depends on JDK 8. Even
if you have the latest JDK installed, make sure that you have JDK 8 installed
on your system. Again, the Linux users can follow the "Install Oracle JDK 8 on Linux" article to install Oracle JDK 8. Mac users can follow the "Install the Latest Oracle JDK on Mac" article with Oracle JDK 8.
Step 01:
Step 01:
Open your IntelliJ Idea and click on the New Project button. You can
also select File → New → Project... if you already have IntelliJ Idea
opened with a project.
Step 02: (One-time setup)
In the opened dialog, select Maven from the left side list, make sure that you
have selected the JDK 8 as the Project SDK, tick the "Create from archetype"
IntelliJ does not come with the archetype to create simple Scala applications.
Therefore you need to add the archetype to the IntelliJ Idea. Once it is
added, you can use it forever to create Scala projects with Maven.
In the opened dialog, enter the following values and click OK:
GroupId: net.alchim31.maven
ArtifactId: scala-archetype-simple
Version: 1.7
Step 03:
GroupId: net.alchim31.maven
ArtifactId: scala-archetype-simple
Version: 1.7
Step 03:
Now you will see the added archetype in the list of archetypes. Select it and
click Next. Once again, make sure that you have the JDK 1.8 as the Project
SDK.
Step 04:
Step 04:
Fill in the Name, GroupId, ArtifactId, and
Version of your project. This article creates a demo project named
"scala-maven-demo" with the
artifact id "com.javahelps". Click
Next once you have filled in the project details.
Step 05:
Step 05:
In this step, IntelliJ will ask you to confirm the details provided in the
previous step. Just click Finish.
Step 06:
Step 06:
After a few seconds, you will see your project created as shown below in the
screenshot. The time to create the project may be longer if this is the first
time you are using Maven to create a Scala application.
Step 07: (One-time setup)
Step 07: (One-time setup)
If you open the App.scala file, you may notice plain text view instead
of the usual keywords highlighted source code view. This happens if you do not
have the Scala plugin installed. In such a scenario, IntelliJ will ask you to
install the plugin in the top right corner. Click on it. You can also install
the Scala plugin as explained in the "Setup Scala on IntelliJ Idea" article.
In the appeared dialog, just click OK to install the Scala plugin.
In the appeared dialog, just click OK to install the Scala plugin.
After installing the plugin, IntelliJ will ask you to restart the IDE to apply
the plugin. Go ahead and restart the IDE.
Step 08:
Step 08:
Now you may see a different notification to set up the Scala SDK. The Scala
plugin installed in the previous step tells the IDE how to handle a Scala
project. However, the IntelliJ Idea needs the Scala SDK to run the project
from the IDE. Sometimes, IntelliJ Idea acts smart and automatically detects
the Scala SDK from the Maven library. Sometimes it doesn't. So whenever it
asks you to set up the Scala SDK, do not ignore it. Click on the "Setup Scala
SDK" link.
Step 09:
Step 09:
Check the pom.xml file for the Scala version used in the project and select it
in the appeared dialog. For example, the demo project is using
Scala 2.12.6.
If you do not have the SDK already added to IntelliJ Idea, click on the Create
button. If you see the exact version in the appeared dialog, select it and
click OK.
Otherwise, you can click on the Download button and download the required
Scala version, and then set it as the Scala SDK for your project.
Finally select the Scala SDK in the drop-down menu of the original dialog and click OK.
Finally select the Scala SDK in the drop-down menu of the original dialog and click OK.
Step 10:
Click on the play button next to the object name or the main method and select
"Run App".
IntelliJ Idea will compile your project and run it as shown below.
IntelliJ Idea will compile your project and run it as shown below.
Congratulations on your first Scala with Maven project in IntelliJ Idea and
all the best for your future endeavors with Scala and Maven. If you face any
problems, feel free to comment below. I will try my best to answer them. Not
only the problems, but you can also comment if this article helps you to set
up a Scala project with Maven in IntelliJ Idea.
EmoticonEmoticon