Getting project's root directory

2,007 views
Skip to first unread message

Malte Schwerhoff

unread,
May 29, 2013, 3:47:29 PM5/29/13
to simple-build-tool
Dear list,

I am slightly embarrassed that I have to ask this, but I just couldn't
figure out how to do the following:

How do I get the root directory of a project from inside a Build.scala?

Basically, I want to do something like

val rootDirectory: String =
... // somehow get project root from Sbt settings

I suspect that 'baseDirectory' has what I'm looking for, but

val rootDirectory: String = baseDirectory

obviously doesn't work. In order to at least see if the value of
'baseDirectory' is actually what I am looking for I also tried

baseDirectory map {bd => println(bd)}

That, however, doesn't seem to have any effect at all. At least, the
output doesn't show up anywhere.

Thanks,
Malte

Robin Green

unread,
May 30, 2013, 4:47:16 AM5/30/13
to simple-b...@googlegroups.com, mun123...@googlemail.com


On Wednesday, 29 May 2013 20:47:29 UTC+1, Malte Schwerhoff wrote:
How do I get the root directory of a project from inside a Build.scala?

Basically, I want to do something like

  val rootDirectory: String =
    ... // somehow get project root from Sbt settings

You're not supposed to do it like that. You're supposed to build up other settings/tasks based on the setting containing the root directory. sbt's dependency engine will then provide all and only those settings which you've required, when required.

Although, there is another way. What is it exactly that you want to do with the root directory once you've got it?

Malte Schwerhoff

unread,
May 30, 2013, 8:03:58 AM5/30/13
to simple-build-tool, Robin Green
On 5/30/2013 10:47, Robin Green wrote:
> On Wednesday, 29 May 2013 20:47:29 UTC+1, Malte Schwerhoff wrote:
>>
>> How do I get the root directory of a project from inside a Build.scala?
>>
>> Basically, I want to do something like
>>
>> val rootDirectory: String =
>> ... // somehow get project root from Sbt settings
>>
>
> You're not supposed to do it like that. You're supposed to build up other
> settings/tasks based on the setting containing the root directory. sbt's
> dependency engine will then provide all and only those settings which
> you've required, when required.
Yes, I know that I can do that. It seems like overkill, though, to be
forced to declare a new setting/task just for the sake of being able to
access the setting's value.

> Although, there is another way. What is it exactly that you want to do with
> the root directory once you've got it?
I want to pass the path (as a String or a java.io.File) to a 3rd-party
library method.

Best regards,
Malte

Malte Schwerhoff

unread,
Jun 4, 2013, 6:00:48 AM6/4/13
to simple-build-tool
Please excuse me for warming up this thread, but I cannot believe that
it is so difficult to achieve such a small thing.

Is there really no way to do this? Anybody?

Best regards,
Malte

Heiko Seeberger

unread,
Jun 4, 2013, 5:52:18 PM6/4/13
to simple-b...@googlegroups.com
baseDirectoy is a setting. You can inspect it or simply show it's value it from an sbt session:

demo-equality$ sbt
[info] Loading global plugins from /Users/heiko/.sbt-0.12/plugins
[info] Loading project definition from /Users/heiko/projects/demo-equality/project
[info] Set current project to demo-equality (in build file:/Users/heiko/projects/demo-equality/)
> inspect base-directory
[info] Setting: java.io.File = /Users/heiko/projects/demo-equality
[info] Description:
[info] The base directory.  Depending on the scope, this is the base directory for the build, project, configuration, or task.
[info] Provided by:
[info] {file:/Users/heiko/projects/demo-equality/}default-179d1d/*:base-directory
[info] Dependencies:
[info] *:this-project
[info] Reverse dependencies:
[info] *:target
[info] *:runner
[info] *:ivy-paths
[info] *:unmanaged-base
[info] *:source-directory
[info] Delegates:
[info] *:base-directory
[info] {.}/*:base-directory
[info] */*:base-directory
[info] Related:
[info] {.}/*:base-directory
> show base-directory
[info] /Users/heiko/projects/demo-equality

I think that Robin wanted to point out that you seem to ask the wrong question: As the whole sbt API is built around settings and tasks, all you can do with a setting is use it to define your own setting or task or redefine an existing one.

In order to help you, please describe your use case.

Heiko

--

Heiko Seeberger
Twitter: @hseeberger

--
You received this message because you are subscribed to the Google Groups "simple-build-tool" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-build-t...@googlegroups.com.
To post to this group, send email to simple-b...@googlegroups.com.
Visit this group at http://groups.google.com/group/simple-build-tool?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Robin Green

unread,
Jun 5, 2013, 4:09:21 AM6/5/13
to simple-b...@googlegroups.com, mun123...@googlemail.com
Well... if you have a multi-project build with

 lazy val main: sbt.Project = ...

you can access the root directory as main.base

I didn't want to post this because I think it's a bit of a hack.

Malte Schwerhoff

unread,
Jun 5, 2013, 6:12:31 AM6/5/13
to simple-b...@googlegroups.com, Heiko Seeberger, Robin Green
Dear Heiko and Robin,

I was about to reply to Heiko by describing my scenario in greater
detail, but while doing so I noticed that I was mistaken and that it
actually is an option for me to use the usual "someSetting <<=
otherSetting {v => ... }" transformation in order to achieve what I
want. I initially thought that I don't have a left-hand side
"someSetting" in order to trigger the transformation, but that was wrong.

In any case, thanks a lot for trying to help!

Best regards,
Malte
Reply all
Reply to author
Forward
0 new messages