Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

The future of AI-driven development. Join the discussion around insights on low code's and AI's roles in building mission-critical apps.

Has GenAI transformed how you work? Or has the hype cycle played out? Tell us your thoughts on its impact inn your organization.

DevEx Roundtable: Join the discussion about the evolving needs of the developer from streamlined workflows to infrastructures investments.

Scala in a Java Maven Project

Learn how you can, with the help of a single plugin, use Scala in a Java project.

By  · Tutorial
Comment (9)
Save
53.9K Views

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:

maven-java-project-structure

as you can see, the project structure is pretty simple. it has a standard layout and only three java classes. let’s consider them:


then, the so-called data store:


and finally runner:

don’t forget that we have the pom.xml file. it might contain some dependencies, plugins ,and build goals. this is actually not very important.

mixing scala into a java maven project

in order to make scala available inside of a java maven project, we need to use a maven plugin . yes, you understood correctly! there is only one required step. so let’s customize our pom.xml file:


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.

let’s add some scala code in the project:


after this i, want to create an alternative entry point to the program:

here is how the project looks after adding scala files:

java-maven-scala-project

try to run this code from the ide. it works as expected.

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.

scala-maven-project-run-from-command-line

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.

Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Comments