alvin's blog

How to create an SBT project with subprojects

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.6, “How to create an SBT project with subprojects.”

Problem

You want to configure SBT to work with a main project that depends on other subprojects you’re developing.

Solution

Create your subproject as a regular SBT project, but without a project subdirectory. Then, in your main project, define a project/Build.scala file that defines the dependencies between the main project and subprojects.

SBT: How to control which version of a managed dependency is used

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 18.5, “SBT: How to control which version of a managed dependency is used.”

Problem

You want to make sure you always have the desired version of a managed dependency, including the latest integration release, milestone release, or other versions.

How to manage project dependencies with SBT (Simple Build Tool)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.4, “How to manage dependencies with SBT (Simple Build Tool).”

Problem

You want to use one or more external libraries in your Scala/SBT projects.

Solution

You can use both managed and unmanaged dependencies in your SBT projects.

How to run Scala project tests with SBT and ScalaTest

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.3, “How to run tests with SBT and ScalaTest.”

Problem

You want to set up an SBT project with ScalaTest, and run the tests with SBT.

Solution

Create a new SBT project directory structure as shown in Recipe 18.1, and then add the ScalaTest library dependency to your build.sbt file, as shown here:

How to compile, run, and package a Scala project with SBT

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.2, “How to compile, run, and package a Scala project with SBT.”

Problem

You want to use SBT to compile and run a Scala project, and package the project as a JAR file.

Solution

Create a directory layout to match what SBT expects, then run sbt compile to compile your project, sbt run to run your project, and sbt package to package your project as a JAR file.

How to create an SBT project directory structure

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.1, “How to create an SBT project directory structure.”

Problem

SBT doesn’t include a command to create a new project, and you’d like to quickly and easily create the directory structure for a new project.

Solution

Use either a shell script or a tool like Giter8 to create your project’s directory structure. Both approaches are shown here.

How to wrap Scala traits so they can be used from Java code

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.7, “How to write Scala traits so they can be used from Java code.”

Problem

You’ve written a Scala trait with implemented methods and need to be able to use those methods from a Java application.

Solution

You can’t use the implemented methods of a Scala trait from Java, so wrap the trait in a class.

How to create JavaBeans in Scala (to interact with Java libraries)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.6, “How to create JavaBeans in Scala (to interact with Java libraries).”

Problem

You need to interact with a Java class or library that accepts only classes that conform to the JavaBean specification.

Solution

Use the @BeanProperty annotation on your fields, also making sure you declare each field as a var.

The @BeanProperty annotation can be used on fields in a Scala class constructor:

How to annotate varargs methods in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 17.5, “How to annotate varargs methods in Scala.”

Problem

You’ve created a Scala method with a varargs field, and would like to be able to call that method from Java code.

Solution

When a Scala method has a field that takes a variable number of arguments, mark it with the @varargs annotation.

How to use the Spring Framework in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.4, “How to use the Spring Framework in Scala.”

Problem

You want to use the Java Spring Framework library in your Scala application.

Solution

In my experience, the only real changes in using the Spring Framework in Scala applications involve how you cast the objects you instantiate from your Spring application context file, and that’s only because the casting process is different between Scala and Java.

Syndicate content