To define a project, I do
project in file(".")
What is the function of file("."), as opposed to settings baseDirectory?
For example, what is the difference between
lazy val myProject = project in file("foo")
and
lazy val myProject = (project in file(".")).settings(
baseDirectory := file("foo"))
For projects, there is no difference between using project in file(...) and setting baseDirectory yourself. use show baseDirectory to convince yourself :)
However, since baseDirectory is a setting, it can be used with any scope, e.g. you can set the base directory for a specific configuration or task.
Related
I'd like to create an SBT project with inheritance and shared dependencies.
With Maven's POM files, there is the idea of Project Inheritance where you can set a parent project. I'd like to do the same thing with SBT.
The xchange-stream library uses Maven's Project Inheritance to resolve subproject dependencies when compiled from the parent project.
Here is my idea of what the file structure would look like:
sbt-project/
project/
dependencies.scala # Contains dependencies common to all projects
build.sbt # Contains definition of parent project with references
# to subprojects
subproject1/
build.sbt # Contains `subproject3` as a dependency
subproject2/
build.sbt # Contains `subproject3` as a dependency
subproject3/
build.sbt # Is a dependency for `subproject1` and `subproject2`
Where project1 and project2 can include project3 in their dependencies lists like this:
libraryDependencies ++= "tld.organization" % "project3" % "1.0.0"
Such that when subproject1 or subproject2 are compiled by invoking sbt compile from within their subdirectories, or when the parent: sbt-project is compiled from the main sbt-project directory, then subproject3 will be compiled and published locally with SBT, or otherwise be made available to the projects that need it.
Also, how would shared dependencies be specified in sbt-project/build.sbt or anywhere in the sbt-project/project directory, such that they are useable within subproject1 and subproject2, when invoking sbt compile within those subdirectories?
The following examples don't help answer either of the above points:
jbruggem/sbt-multiproject-example:
Uses recursive build.sbt files, but doesn't share dependencies among child projects.
Defining Multi-project Builds with sbt: pbassiner/sbt-multi-project-example:
Uses a single build.sbt file for the projects in their subdirectories.
sachabarber/SBT_MultiProject_Demo:
Uses a single build.sbt file.
Such that when subproject1 or subproject2 are compiled by invoking sbt compile from within their subdirectories...
Maybe Maven is meant to be used together with the shell environment and cd command, but that's not how sbt works at least as of sbt 1.x in 2019.
The sbt way is to use sbt as an interactive shell, and start it at the top level. You can then either invoke compilation as subproject1/compile, or switch into it using project subproject1, and call compile in there.
house plugin
A feature similar to parent POM would be achieved by creating a custom plugin.
package com.example
import sbt._
import Keys._
object FooProjectPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
val commonsIo = "commons-io" % "commons-io" % "2.6"
override def buildSettings: Seq[Def.Setting[_]] = Seq(
organization := "com.example"
)
override def projectSettings: Seq[Def.Setting[_]] = Seq(
libraryDependencies += commonsIo
)
}
sbt-sriracha
It's not exactly what you are asking for, but I have an experimental plugin that allows you to switch between source dependency and binary dependency. See hot source dependencies using sbt-sriracha.
Using that you could create three individual sbt builds for project1, project2, and project3, all located inside $HOME/workspace directory.
ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.1-SNAPSHOT"
lazy val project3Ref = ProjectRef(workspaceDirectory / "project3", "project3")
lazy val project3Lib = "tld.organization" %% "project3" % "0.1.0"
lazy val project1 = (project in file("."))
.enablePlugins(FooProjectPlugin)
.sourceDependency(project3Ref, project3Lib)
.settings(
name := "project1"
)
With this setup, you can launch sbt -Dsbt.sourcemode=true and it will pick up project3 as a subproject.
You can use Mecha super-repo concept. Take a look on the setup and docs here: https://github.com/storm-enroute/mecha
The basic idea is that you can combine dependent sbt projects (with their own build.sbt) under single root super-repo sbt project:
/root
/project/plugins.sbt
repos.conf
/project1
/src/..
/project/plugins.sbt
build.sbt
/project2
/src/..
/project/plugins.sbt
build.sbt
Please, note that there is no build.sbt in the root folder!
Instead there is repos.conf file. It contains definition of the sub-repos and looks like the folowing:
root {
dir = "."
origin = ""
mirrors = []
}
project1 {
dir = "project1"
origin = "git#github.com:some_user/project1.git"
mirrors = []
}
project2 {
dir = "project2"
origin = "git#github.com:some_user/project2.git"
mirrors = []
}
Then you can specify the Inter-Project, source-level Dependencies within individual projects.
There are two approaches:
dependencies.conf file
or in the build source code
For more details, please, see the docs
My build consists of a root project, sub projects, and other meta projects. The sub projects produce a JAR while the meta projects are for other things like packaging etc.
See Cross platform build with SBT for the reason why I am doing this. For now assume this the right thing to do, but if not then feel free to comment on that question.
What I want to be able to do is to aggregate all my sub projects like so:
lazy val root = (project in file(".")).
aggregate(subProjects: _*)
val subProjects = Seq(projectA, projectB)
lazy val projectA = project
lazy val projectB = project
For the packaging I have something like:
lazy val packaging = (project in file("packaging").
enablePlugins(JavaServerAppPackaging). // Using sbt-native-packager
settings(
libraryDependencies ++= Seq(projectA.projectID, projectB.projectID)
)
Now before packaging I have to publish the sub projects:
sbt> publishLocal
And then to package from those published artifacts I do:
sbt> packaging/universal:packageBin
This all works great but now for the real question, how can I aggregate specific tasks? For example I want to be able to clean all projects including the packaging one.
I know that it is possible to do the opposite, see Disable aggregate for sbt custom task.
I've had a bit of a look at sbt.Aggregation but I can't make head or tail of it.
My ideal solution would look something like:
lazy val root = (project in file(".")).
aggregate(subProjects: _*).
settings(aggregatedTasks(metaTasks, metaProjects ): _*)
...
val metaProjects = Seq(packaging, someOtherProject)
val metaTasks = Seq(clean, someOtherTask)
def aggregatedTasks(tasks: Seq[TaskKey[_]], projects: Seq[Project]): Seq[Setting[_]] = {
??? // Is there a way to do this?
}
I am totally new to SBT. Suppose I have three Scala projects: project_a, project_b, project_c. How should I go about building all three projects into one jar file? Suppose I use project_a as the root project. The directory structure is like
--project_a
--build.sbt
--project_b
--project_c
Following the instructions on sbt webiste, I created a build.sbt file, which looks something like
lazy val root = (project.in(file("."))).aggregate(project_b, project_c)
lazy val project_b = project
lazy val project_c = project
I put the build.sbt under the project_a. When I run sbt clean compile under project_a, a new (kinda of empty) project_b and project_c folders are created under the folder project_a. However, in the build.sbt file, I meant project_b and project_c to refer to the original folders I already created which contains the source and test code, and which are outside project_a.
Can someone let me know what I did wrong?
Thanks
First, your multi-project setup is not right.
Getting Started guide says:
Aggregation means that running a task on the aggregate project will also run it on the aggregated projects.
If you have project_a that uses project_b and project_c, then you need root in addition to project_a, project_b, and project_c.
Root can aggregate all three (a, b, and c), but it only aggregates commands given to sbt shell, for instance for compiling all three at the same time.
project_a should be set up to depend on project_b and project_c.
Here's an example:
lazy val commonSettings = Seq(
scalaVersion := "2.11.4",
organization := "com.example"
)
lazy val root = (project in file(".")).
aggregate(project_a, project_b, project_c).
settings(commonSettings: _*)
lazy val project_a = project.
dependsOn(project_b, project_c).
settings(commonSettings: _*).
settings(
// your settings here
)
lazy val project_b = project.
settings(commonSettings: _*)
lazy val project_c = project.
settings(commonSettings: _*)
How should I go about building all three projects into one jar file?
If you just want *.class files from your own projects, you can see an example on Macro Projects.
If you want *.class files and library dependencies, you need sbt-assembly.
Why does sbt not able to find a command in a multiproject build?
My plugin resembles
object MyPlugin extends Plugin {
lazy val plug = Seq( commands ++= Seq(versionWriteSnapshotRelease) )
def versionWriteSnapshotRelease = Command.command(
"versionWriteSnapshotRelease",
"Writes the release format of the snapshot version. This is used to preserve the actual snapshot version in a release commit.",
""
) { state => .... }
}
I have my project/Build.scala file as follows:
lazy val app = Project(id = "app-subproject", base = file("app"))
.settings(MyPlug.plug :_*)
.settings(...)
lazy val common = Project(id = "library-subproject", base = file("common"))
.settings(MyPlug.plug :_*)
.settings(...)
With files laid out like
root
|_ common
|_ src
|_ app
|_ src
This configurations fails with the error:
[error] Not a valid command: versionWriteSnapshotRelease
[error] Not a valid project ID: versionWriteSnapshotRelease
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: versionWriteSnapshotRelease (similar: version, ...)
[error] versionWriteSnapshotRelease
However if I restructure to something like
lazy val app = Project(id = "app-subproject", base = file("."))
.settings(MyPlug.plug :_*)
.settings(...)
lazy val common = Project(id = "library-subproject", base = file("common"))
.settings(MyPlug.plug :_*)
.settings(...)
With files laid out like
root
|_ common
|_ src
|_ src
Then it works. Note that my change is to put the app project /src in the basedir and set the app project to have base ".".
This plugin is used across multiple projects and has no issue when the file layout is in the second form. So I know it isn't an issue with the plugin per-se.
The reason for the error is that you define two submodules common and app with the third default root project without the command set.
You should add yet another definition of a project for the default root project so the command is also defined for it as follows:
lazy val root = Project(id = "root", base = file("."))
.settings(MyPlug.plug :_*)
.settings(...)
You've confirmed that thinking with your test when you turned app into the default root project.
As an additional check before you add the third module definition, do projects and see what projects are defined. For every project in the list, execute [projectName]/versionWriteSnapshotRelease. I'm sure it's going to be fine for app and common, but not for the default root module.
So I'm using the packageArchetype.java_server and setup my mappings so the files from "src/main/resources" go into my "/etc/" folder in the debian package. I'm using "sbt debian:package-bin" to create the package
The trouble is when I use "sbt run" it picks up the src/main/resources from the classpath. What's the right way to get the sbt-native-packager to give /etc/ as a resource classpath for my configuration and logging files?
plugins.sbt:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.7.0-M2")
build.sbt
...
packageArchetype.java_server
packageDescription := "Some Description"
packageSummary := "My App Daemon"
maintainer := "Me<me#example.org>"
mappings in Universal ++= Seq(
file("src/main/resources/application.conf") -> "conf/application.conf",
file("src/main/resources/logback.xml") -> "conf/logback.xml"
)
....
I took a slightly different approach. Since sbt-native-packager keeps those two files (application.conf and logback.xml) in my package distribution jar file, I really just wanted a way to overwrite (or merge) these files from /etc. I kept the two mappings above and just added the following:
src/main/templates/etc-default:
-Dmyapplication.config=/etc/${{app_name}}/application.conf
-Dlogback.configurationFile=/etc/${{app_name}}/logback.xml
Then within my code (using Typesafe Config Libraries):
lazy val baseConfig = ConfigFactory.load //defaults from src/resources
//For use in Debain packaging script. (see etc-default)
val systemConfig = Option(System.getProperty("myapplication.config")) match {
case Some(cfile) => ConfigFactory.parseFile(new File(cfile)).withFallback(baseConfig)
case None => baseConfig
}
And of course -Dlogback.configuration is a system propety used by Logback.