使用SBT创建新的Scala项目?

20

我可以这样创建一个sbt项目:

$ mkdir project1
$ cd project1
$ sbt
Loading /usr/share/sbt/bin/sbt-launch-lib.bash

> set name := "project1"
[info] Defining *:name
...

> set scalaVersion :="2.10.2"
[info] Defining *:scalaVersion
...

> set version :="1.0"
[info] Defining *:version
...

> session save
[info] Reapplying settings...

> exit

这将为项目创建build.sbt文件。

$ cat build.sbt 
name := "project1"

scalaVersion :="2.10.2"

version :="1.0"

现在是否有类似的命令行可以执行相同的操作?例如:

sbt new_project "name" version scala_version

编辑1

我找到了另一种方法,即创建项目文件夹:

$ mkdir project1
$ cd project1/

更新项目详细信息:

$ sbt 'set name := "project1"' 'set scalaVersion :="2.10.2"' 'set version :="1.0"' 'session save'
Loading /usr/share/sbt/bin/sbt-launch-lib.bash
[info] Set current project to project1 (in build file:/tmp/scala/project1/)
[info] Defining *:name
[info] The new value will be used by *:description, *:normalizedName and 6 others.
[info]  Run `last` for details.
[info] Reapplying settings...
[info] Set current project to project1 (in build file:/tmp/scala/project1/)
[info] Defining *:scalaVersion
[info] The new value will be used by *:allDependencies, *:ivyScala and 10 others.
[info]  Run `last` for details.
[info] Reapplying settings...
[info] Set current project to project1 (in build file:/tmp/scala/project1/)
[info] Defining *:version
[info] The new value will be used by *:isSnapshot, *:projectId and 3 others.
[info]  Run `last` for details.
[info] Reapplying settings...
[info] Set current project to project1 (in build file:/tmp/scala/project1/)
[info] Reapplying settings...
[info] Set current project to project1 (in build file:/tmp/scala/project1/)

我们已经生成了项目并保存了设置:

$ ls
build.sbt  project
$ cat build.sbt 
name := "project1"

scalaVersion :="2.10.2"

version :="1.0"

我希望SBT能够像Maven一样原生地提供功能:mvn archetype:generate


1
你可能想阅读https://dev59.com/eWw15IYBdhLWcg3wy-zO#20362511。 - Jacek Laskowski
我也很好奇为什么没有人提到http://typesafe.com/activator。 - Jacek Laskowski
@JacekLaskowski 你提到的那个问题真的很有趣,解释得非常好 :-) - tuxdna
@tuxdna,你有找到一些方法来为sbt创建maven类型的原型吗? - lazywiz
@lazywiz 不太确定你的意思。你是想使用sbt命令行创建mvn原型项目吗? - tuxdna
3个回答

15

从sbt 0.13.13开始,内置了一个new命令:

sbt new scala/scala-seed.g8

8

有一个SBT的插件叫做np,可以完成你需要的操作。 使用方法:

$ sbt 
$ np name:my-new-project org:org.myproject version:0.1.0-SNAPSHOT  

np插件是一件好东西。但它不允许我创建自己的原型。我想要像项目模板一样的东西(带有10-15%的可用代码,需要扩展),而不是空项目。这可能吗? - pkozlov

3

你可能想要查看我的项目skeleton

它可以简化你快速启动时的第一步。


3
每次我需要创建一个项目时,我都必须使用“骨架”遵循相同的步骤。我想要一个简单的命令行工具,或许你可以添加一个shell脚本来使它更加容易。 - tuxdna

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接