Create Simple Project SBT 0.10.X
Asked Answered
P

9

39

(This is a follow up to sbt not creating projects correctly. The question wasn't answered.)


Basically, that question says "I don't know how to create a project under the new sbt. With the old one, I just ran sbt in a new folder and there was a guided wizard that led me through the setup."

The accepted answer does not explain how to create a new project, it just points to the documentation, which also doesn't explicitly say how to create a new project -- only how to write a build.sbt file.

So I tried first writing a build.sbt and then running sbt in the directory with the build.sbt file, but I still don't see a src directory to work with.

Could someone post a simple step-by-step (I'm assuming there are like 3 steps at most) guiding how to create a new project under sbt 0.10.X?

Porpoise answered 12/9, 2011 at 18:49 Comment(1)
I think the latest changes to https://mcmap.net/q/353802/-why-doesn-39-t-sbt-create-project-files/1305344 made it a great source of different approaches to sbt project layout generation (and could make this question obsolete).Advised
P
62

I found the answer I was looking for at this webpage: Scala 2.9.1, sbt 0.10 and ScalaTest step-by-step.

The high-level steps are:

  1. mkdir my_project make a folder for your project
  2. Create a simple my_project/build.sbt file, e.g.:

    name := "A Project"
    
    version := "0.1"
    
    scalaVersion := "2.9.1"
    
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "1.6.1" % "test"
    )
    
  3. Create a file my_project/src/main/scala/HelloWorld.scala, where you create all the directories you need as you go (e.g. create the directory structure src/main/scala/)

    object Main extends App { Console.println("Hello World!") }

  4. Execute your sbt commands: e.g. sbt run

Porpoise answered 12/9, 2011 at 21:0 Comment(1)
For a minimal solution the libraryDependencies can also be omitted. But is is nice to include them in the answer since one always needs some libs.Crossway
T
35

I am surprised that noone gave another solution which is the closest to the old way (as mentioned by @dsg) to create a simple project in sbt: Just run sbt in your project directory, then issue the following commands in the sbt REPL:

> set name := "MyProject"
> set version := "1.0"
> set scalaVersion := "2.9.0"
> session save
> exit

Granted, it is only mildly useful as it will just create the build.sbt file (enough to make it a proper sbt project) with the corresponding properties set, and you might as well create the file by hand (I usually prefer to do so myself). It won't create the src directory either.

Trisomic answered 8/10, 2012 at 12:22 Comment(0)
O
16

Just a few days ago np (new project) plugin to sbt was released. It intended to dealt exactly with that problem:

Initial release. Provides a minimal interface for generating new sbt projects via,... sbt.

Basic use is to install the plugin globally and start up a new project with

$ sbt 
$ np name:my-project org:com.mypackage version:0.1.0-SNAPSHOT  

This will generate a simple build.sbt project for you along with the standard project directory structure for main and test sources.

For more advanced usage, see the project's readme for more info

Overweight answered 12/9, 2011 at 19:0 Comment(0)
D
11

You can use https://github.com/n8han/giter8 to generate project layout using various templates

Decemvir answered 12/9, 2011 at 19:3 Comment(0)
T
4

In newer versions of sbt, you can just install sbteclipse:

// ~/.sbt/plugins/build.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")

then from sbt's console you can run:

eclipse with-source=true

In version 0.10.x I think this post can help you:

http://dcsobral.blogspot.fr/2011/06/very-quick-guide-to-project-creation-on.html

Turpitude answered 12/9, 2012 at 9:21 Comment(0)
H
1

I've been using https://github.com/ymasory/sbt-prototype skeleton. See also my other answer.

It was the first one that just worked and I've been a quite happy with it since then.

Heteroecious answered 14/5, 2012 at 17:14 Comment(0)
G
1

Don't forget the recent sbt 0.13.3 new command:

Example:

First, you need sbt’s launcher version 0.13.13 or above.
Normally the exact version for the sbt launcher does not matter because it will use the version specified by sbt.version in project/build.properties; however for new sbt’s launcher 0.13.13 or above is required as the command functions without a project/build.properties present.

Next, run:

$ sbt new eed3si9n/hello.g8
....
name [hello]:
scala_version [2.11.8]:

Template applied in ./hello

This ran the template eed3si9n/hello.g8 using Giter8, prompted for values for “name” and “scala_version” (which have defaults “hello” and “2.11.8”, which we accepted hitting [Enter]), and created a build under ./hello.

Godinez answered 12/11, 2016 at 9:1 Comment(0)
K
0

Check out the GitHub repo Scala SBT Template. In particular the buildt.sbt file.

Just clone the repo, go to that directory, then call the sbt command.

 $ git clone git://github.com/dph01/scala-sbt-template.git
 $ cd scala-sbt-template
 $ ./sbt

From inside of sbt, you can type run to execute provided code. Have fun!

Kyleekylen answered 27/5, 2014 at 4:15 Comment(0)
A
0

An alternative way to generate the project structure using Intellij:

  • Create the directory for the project and include there a basic sbt file. You just need to specify the project name.

    name := "porjectName"

  • With Intellij import the project. During the process check the options "Use auto-import" and "Create directories for empty content roots automatically"

That will create for you the basic skeleton for the sbt project.

Anion answered 24/11, 2014 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.