compile

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.”

Back to top

Problem

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

Back to top

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.

Table of Contents

  1. Problem
  2. Solution
Back to top

How to make your Scala shell scripts run faster by pre-compiling them

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.13, “How to make your Scala shell scripts run faster by pre-compiling them.”

Problem

You love using Scala as a scripting language, but you’d like to eliminate the lag time in starting up a script.

Solution

Use the -savecompiled argument of the Scala interpreter to save a compiled version of your script.

A basic Scala script like this:

How to compile Scala code faster with the 'fsc' command-line compiler

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 14.9, “How to compile your Scala code faster with the 'fsc' command-line compiler.”

Problem

You’re making changes to a project and recompiling it with scalac, and you’d like to reduce the compile time.

Solution

Use the fsc command instead of scalac to compile your code:

How to compile Scala code with ‘scalac’ and run it with ‘scala’

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.5, “How to compile Scala code with 'scalac' and run it with 'scala'.”

Problem

Though you normally use the Simple Build Tool (SBT) to build Scala applications, you may want to use more basic tools to compile and run small test programs, in the same way you might use javac and java with small Java applications.

Common SBT commands (Scala SBT)

I had the common Scala SBT commands in the notes for my project named SARAH, and thought I'd list them here to make them easier to find:

A Java regular expression example (featuring Pattern and Matcher classes)

Summary: Java regular expressions in Java 1.4, featuring regex expressions and pattern matching, using the new Java Pattern and Matcher classes.

The following Java example offers an introduction to regular expressions in Java 1.4. In this code we're creating a regular expression that can search for a date. Specifically, that date must be in a format of two digits, followed by a hyphen, followed by two digits, followed by a hyphen, followed by four digits.

This date pattern is created in this line of code:

How to compile a Java program with Ant

While I'm digging around through Ant build scripts today, here's a sample Ant target I created that demonstrates how to compile a Java program, while also showing a few other things, including how to exclude your JUnit tests during the compile process.