I am currently a
lamer in sbt technology. Sbt stands for Simple build tool. This is a
build system that is highly developed, modern and is the de facto
standard build tool in the Scala world. Currently I'm using sbt for
several job tasks and last week I came across a rather unpleasant
problem that probably is very easy to solve for experienced Scala and
sbt developer. I am writing this article to help people with a
similar problem in the future. Sbt has several global settings. One
of this global settings is
baseDirectory.
Depending on the scope, this is the base directory for the build, the
project, the configuration, or the task. You can easy get value of
project base directory from sbt console. Just type
baseDirectory and
you will get the value of this setting.
You can also use inspect baseDirectory and you will get additional information.
In sbt is a good practice to isolate long tasks into singleton object and keep build.sbt much shorter. This is very cool but in order to have some global settings into this singleton object there are several tricky things that you must to do. The same thing is true if you want to have streams available to the singleton object. My job task involved to process a certain way all the files that meet certain criteria. For this purpose, I had to have baseDirectory for project. When I tried to pass baseDirectory as a method argument in this way:
And this:
Every time I got error like this:
I tried several things. For example to pass baseDirectory.value in this way:
But I got errors that .value cannot be used in this way.
In the end, I managed to solve the problem with using ".." into the singleton object, that means "the parent directory". But this solution is not good at all.
What will happen if the location of .scala file is changed? I love stackoverflow. Sometimes you can ask and get valuable help and explanations. Also you can answer questions
and thereby helping others. I asked the question and received a helpful guidance. So If you want to have baseDirectory or similar value into singleton object,
you must define the object like this.
And into build.sbt this:
Or this:
I like second solution because looks much clearer.