开发者

Create Simple Project SBT 0.10.X

(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-开发者_JAVA百科by-step (I'm assuming there are like 3 steps at most) guiding how to create a new project under sbt 0.10.X?


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


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.


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


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


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


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.


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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜