Packaging the application
So far so good, but what if we want to send this application to uncle Bob so that he can plan for his retirement too? It would not be very convenient to ask him to download IntelliJ or SBT. We are going to package our application in a .jar file so that we can run it with a single command.
SBT provides a package task that can create a .jar file, but this file will not contain the dependencies. In order to package our own classes as well as the classes coming from the dependent libraries, we are going to use the sbt-assembly plugin. Create a new file called project/assembly.sbt containing the following:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
Then, edit build.sbt to define the name of our main class:
name := "retirement_calculator"
version := "0.1"
scalaVersion := "2.12.4"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.4" % "test"
mainClass in Compile := Some("retcalc.SimulatePlanApp")
Click on the SBT tab on the top-right and then on the Refresh button. Then, expand the project and double-click on the assembly task:
The assembly task will compile all the classes, run all the tests, and if they all pass, package a fat JAR. You should see an output similar to this at the bottom of the SBT console:
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Strategy 'discard' was applied to a file
[info] SHA-1: 7b7710bf370159c549a11754bf66302a76c209b2
[info] Packaging /home/mikael/projects/scala_fundamentals/retirement_calculator/target/scala-2.12/retirement_calculator-assembly-0.1.jar ...
[info] Done packaging.
[success] Total time: 11 s, completed 14-Jan-2018 12:23:39
Copy the location of the .jar file in your clipboard. Now, you can use a Unix terminal or a Windows command prompt and run the application as follows:
$ java -jar <path to your .jar file> 1997.09,2017.09 25 40 3000 2000 10000
Capital after 25 years of savings: 499923
Capital after 40 years in retirement: 586435
It is now easier to try different parameters. It is interesting to see that some periods are much more lucrative than others:
$ java -jar <path to your .jar file> 2000.01,2010.01 25 40 3000 2000 10000
Capital after 25 years of savings: 225209
Capital after 40 years in retirement: -510074
$ java -jar <path to your .jar file> 1950.01,1960.01 25 40 3000 2000 10000
Capital after 25 years of savings: 4505196
Capital after 40 years in retirement: 2077953853