Why is sbt current project name "default" in 0.10? - sbt

I'm using sbt 0.10 to build a Scala project using just a build.sbt file instead of a full configuration.
Every time I start sbt it gives me the messages as follows:
[info] Set current project to default-ee699e (in build file:/Users/.../project/plugins/)
[info] Set current project to default-8febe7 (in build file:/Users/.../)
I did set the name and mainClass settings in the build.sbt file, so I don't know what I need to set to get the project names default-XXXX go away.
EDIT: the answer given below is correct in that this is cosmetic. If you switch to a full configuration of sbt, then it uses that project's name as opposed to default-XXXX however.

The message can be a bit misleading, it's not saying that you must "set the curent project", it's telling you what it's doing.
It sets the current project to the plugins folder, does it's stuff (compile, etc.), then sets the current project to your actual build folder and does it's thing once again.
You don't need to set anything else.

Related

what is the usage of sbt-git plugin

My sbt version is 1.1.5
I read readme in git,and add addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0") in plugins.sbt .
When I use enablePlugins(GitVersioning), it's red, "Cannot resolve symbol GitVersioning"
New user to sbt, cannot figure out where did I missed.
Reload project and error will dissapear.
Every change to sbt build definition (e.g. build.sbt, plugins.sbt) requires sbt project rebuild.
If working directly from sbt, type reload or from console sbt reload
If working from IntelliJ, then right click on sbt project and choose Refresh sbt project (rebuild once) or Auto-import (rebuild on every change).
You have to add addSbtPlugin line to project/plugins.sbt file (create it if doesn't exist). Then add enablePlugins(GitVersioning) to your build.sbt (in the root directory of your project).

Set sbt options in build.sbt

I'm working on an SBT project that has to be built with the options like:
-Xmx2G -Xss256M -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled
This means that every new developer has to read the readme and assign the options to SBT_OPTS in bash profile or put them in the sbtopts file. Similarly, this has to be configured on Jenkins and this applies to all the projects (so if someone wants to use -XX:+UseG1GC with other projects it becomes an issue). Is it possible to specify the required options in the build file itself? This seems logical to me, as the options are project-specific and without them, you cannot build the project.
Create a .sbtopts file at the root of the build with contents:
-J-Xmx2G
-J-Xss256M
-J-XX:+UseConcMarkSweepGC
-J-XX:+CMSClassUnloadingEnabled

Reuse test resources (logback-test.xml) from dependent project with SBT

I've got 3 project defined in my Build.scala file:
common
services.dependsOn(common)
web.dependsOn(common)
Project Common contains /test/resources with logback-test.xml configuration which I would like to use for services and web tests.
This setup works fine in intelliJ with sbt-idea but when I try to run 'sbt test' from command line the logback-test.xml isn't copied over to /services/target/testClasses or /web/target/testClasses which means that the tests will use default slf4j configuration with useless tons of DEBUG info.
What should I do to force sbt to copy test resources from dependent project to others.
Thanks in advance
Dependencies don't include test configuration by default. You can change this like so:
common
services.dependsOn(common % "compile->compile;test->test")
web.dependsOn(common % "compile->compile;test->test")
More info here

Create standalone jar using SBT

I was a heavy Maven user and now I'm gradually using SBT for some of my projects.
I'd like to know how could I use SBT to create a standalone Java project? This project should be packaged as a JAR file and this JAR file would be used as a dependency in another SBT project.
In Maven, I could tell in my pom.xml what type of artifact it should produce when I build it. Is there something similar that I can do in SBT?
There is a difference between standalone and making a project useable as a dependency or another project. In the first case, you would use a plugin such as sbt-assembly. What it will do is create one jar file containing the project class files along with all of its dependencies. If you write an application, what you get is a double-clickable jar that you can execute from anywhere.
If you want to use your project A as a dependency for another project B, you have different options. You could just package the class files of A, using sbt package (answer of #Channing Walton). Then you could drop the resulting .jar file in the lib directory of project B. However, if A also requires libraries, you must make sure that they also end up in project B's libraries.
A better approach is to publish your project. You can do that purely on your local machine, using sbt publish-local. That will store the jar as produced by package in a special local directory which can be accessed from sbt in another project, along with a POM file that contains the dependencies of A. It will use a group-ID (organization) and artifact-ID (name) and a version of your project A. For example, in build.sbt:
name := "projecta"
version := "0.1.0-SNAPSHOT"
organization := "com.github.myname"
scalaVersion := "2.10.3"
publishMavenStyle := true
After publishing with sbt publish-local, you can add the following dependency to your project B:
libraryDependencies += "com.github.myname" %% "projecta" % "0.1.0-SNAPSHOT"
If you have a pure Java project, you can omit the Scala version suffix, i.e. in Project A:
crossPaths := false
autoScalaLibrary := false
And then in Project B:
libraryDependencies += "com.github.myname" % "projecta" % "0.1.0-SNAPSHOT"
(using only one % character between group and artifact ID).
More on publishing in the sbt documentation.
'sbt package' will produce a jar file.
If you want it to be executable you need to add the following to your .sbt config:
mainClass in Compile := Some("your.main.Class")
Sure, you can use 'sbt package' command, it creates a jar file but this jar will be without any dependencies. To run it necessary to specify 'classpath' arg to the libraries.
In your case you wish a standalone runnable file. And you need to add the dependencies.
To do this you can use 'assembly' plugin for SBT, see https://github.com/sbt/sbt-assembly/
Afterward you can just run 'sbt assembly' command, it provides a fat jar file with all dependencies that you can deploy and run anywhere and at any time.
For details see this article
publishLocal
builds the artifact and publish in the local Ivy repository making it available for your local project dependencies.
publishM2
same as above, but the artifact is published in local Maven repo instead of Ivy repo.
I think the easiest way to produce a stand-alone jar with your project in it,
is sadly not lying inside sbt.
I personally use my IDE: Intellij to make the jar (through the 'build artifact' feature).
Thanks to Intellij I can easily choose which library I want to include in the jar or not, (for instance the scala stl).
IMHO, this is by far the simplest method to get an executable jar for your project.
If you put the scala stl you can run your jar with the "java -jar" command, if you don't you have to run it somewhere with the correct version of scala installed with "scala".

How to change destination jar directory

How to change destination directory of jars getting downloaded by sbt in libraryDependencies.
I want to change it to some thing like $HOME/custom_jars rather than its default one ($HOME/.ivy2/....)
Thanks,
Abhinandan
Setting retrieveManaged to true causes sbt to create a lib_managed directory in your project directory and to put your dependency jars there.
The directory name is used is controlled by managedDirectory and defaults to baseDirectory.value / "lib_managed", but you can set it to something else if you like.

Resources