« SBT run: choose automatically the App to launch 30 Dec 2012
I like Christmas because, beside resting from work and having fun with family and friends, usually there is time to learn something new. During this Xmas I’ve been playing with Scala: First, trying to finish the Coursera Functional Programming Principles course. Later, working a bit in a personal project. Better late than never :smile
As for my personal project, it provides more than one executable entry point:
- web application exposing a REST API using Scalatra.
- offline script to retrieve data from a third party periodically. This could be easily done with Rake in the Ruby world, and as far as I know there’s not a task management tool “de facto standard” in python.
Working with Scala and SBT, the command sbt run looks like the natural alternative. It seeks for every Scala Object in the project that could be used as the assembly entry point:
- an object that defines a main method
- an object that inherits from App
If your application has more than one object fitting the previous requirement, the command sbt run will ask for your help to finish the execution.
Let’s consider the following snippet of code, having two objects that define a main method:
When you execute the sbt run command, the following text shows up:
It requires human action (in the previous example, fill in the number 2), as the run command does not receive any parameter to automate the process.
Fortunately, there’s an easy solution using the SBT plugin sbt-start-script . You just need to follow these three steps:
- Create (or update) the file project/plugins.sbt, including:
- Create (or update) the file build.sbt, adding:
- Execute:
As result, a new file target/start is created. A file that requires the main class name to be executed as the first argument:
Two last tips:
-
In case your program just has a single main class, the script does not require any argument.
-
Remember to add the automatically generated file target/start to your CVS
« Home