Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

Probably the most painful thing for software developers is to be restricted in their choice of technology that they want to use in a development. You pay for a conference ticket, listen about new cool frameworks, development approaches, or tools… Then you return to your office with huge enthusiasm and to try something on a real project. Unfortunately, such initiative very frequently meets resistance from different sides: team members don’t want to learn something new, a manager thinks it is risky, a product owner hurries with a new release, etc.

Well, to be honest, I described this situation too dramatically. Of course, nobody will punish you for proposing to integrate something new in a project. Another question is how to do it right.

Today I want to discuss an interesting topic — how to use Scala in a Java project? This case will be interesting for those developers who want to try Scala in real scenarios. Doesn’t matter what type of app you have: Spring or Spark or something else. I’m going to demonstrate how to integrate Scala to a Maven project.

Let’s start!

Abstract Java Maven Project

Let’s assume you are working on some Java project. Also, Maven is used as project management tool. Such project always looks like this:

As you can see, the project structure is pretty simple. It has a standard layout and only three Java classes. Let’s consider them:

After this update, you need to wait while Maven downloads all this stuff and validates it. This is possible if you set auto-update for Maven, but otherwise, you have to force it manually.

Now we can use Scala in the project. For this purpose, you need to create two new folders — ‘src/main/scala‘ and ‘src/test/scala‘. The Scala Maven plugin looks at these directories and compiles Scala files within them.

Package Scala-Maven Projects in a JAR

What if you want to package this project in a JAR file and then run it somewhere? For this, you need run the mvn package command or use its analogy in the IDE. This action produces two JARs in the 'target' folder. You need to work with the one that has ‘*-jar-with-dependencies.jar‘ in its name.

In the screenshot above, I highlighted two options to run the JAR. In the first onem you may specify which class you want to run. In the second one, we run the JAR and it runs the default class specified in the pom.xml ScalaRunner .

By the way, you can generate executable JARs by using the maven-assembly-plugin and its 'single' goal.

Summary

Integrating Scala in a Java project is pretty straightforward. As you see, it can be done in 10 minutes. Of course, it makes sense only if you already know at least the Scala basics and know that with some help, you can develop a project more efficiently or rewrite existing functionality. Everything depends on you.

MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.