Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the sub project path in sbt multi project build

Tags:

scala

sbt

I am trying to get the location of sub project in multi-project build in sbt. But I am able to get only the root project directory.

lazy val copyToResources = taskKey[Unit]("copies the assembly jar.")
private val rootLocation: File = file(".").getAbsoluteFile
private val subProjectLocation: File =  file("sub_project").getAbsoluteFile.getParentFile
lazy val settings = Seq(copyToResources := {
val absPath = subProjectLocation.getAbsolutePath
println(s"rootLocation:$subProjectLocation $absPath, sub-proj-location: ${rootLocation.getAbsolutePath}")
 })

Output:

 rootLocation:/home/user/projects/workarea/repo /home/vdinakaran/projects/workarea/repo, sub-proj-location: /home/vdinakaran/projects/workarea/repo
 rootLocation:/home/user/projects/workarea/repo /home/vdinakaran/projects/workarea/repo, sub-proj-location: /home/vdinakaran/projects/workarea/repo

directory structure:

repo
   |-- sub_project

As a work around , I have added the sub_project folder using the rootLocation. But why the file("sub_project") is not returning the path ?

like image 220
Knight71 Avatar asked Nov 23 '16 14:11

Knight71


People also ask

What does %% mean in sbt?

This is part of SBT which play uses as a build tool. Specifically this is an import statement. The percent symbol % is a actually a method used to build dependencies. The double percent sign %% injects the current Scala version - this allows you to get the correct library for the version of scala you are running.

Where are dependencies stored in sbt?

If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.

What does the src folder contain in an sbt project?

The project folder contains sbt configurations and settings, while src contains all your source code. sbt creates the target folder after you compile your code; it includes the JVM bytecode of your application.

Which folder contains generated classes from sbt?

In sbt's terminology, the “base directory” is the directory containing the project. So if you created a project hello containing /tmp/foo-build/build. sbt as in the sbt by example, /tmp/foo-build is your base directory.


1 Answers

If you define your subproject like this

lazy val subProject = project in file("sub_project") // ...

then you can get its path using the scoped baseDirectory setting:

baseDirectory.in(subProject).value.getAbsolutePath

Or in the sbt console:

> show subProject/baseDirectory

About the problem with your code (beside that you mixed up root and sub-project in the output) is the usage of relative paths. Sbt documentation on Paths explicitly says

Relative files should only be used when defining the base directory of a Project, where they will be resolved properly.

Elsewhere, files should be absolute or be built up from an absolute base File. The baseDirectory setting defines the base directory of the build or project depending on the scope.

like image 144
laughedelic Avatar answered Sep 20 '22 23:09

laughedelic