sbt入门指南翻译(1)--渣翻译有错误望指教

Getting Started with sbt

Sbt入门

sbt uses a small number of concepts to support flexible and powerful build
definitions. There are not that many concepts, but sbt is not exactly like other
build systems and there are details you will stumble on if you haven’t read the
documentation.

Sbt使用了少数的术语来支持灵活强大的构造定义。虽然没有很多术语,但是sbt并不完全像其他构造系统,并且有些细节你会无意中发现即使你还没有阅读文档

The Getting Started Guide covers the concepts you need to know to create and
maintain an sbt build definition.

入门指南覆盖了你需要知道创建和维护sbt 构造定义的术语

It is highly recommended to read the Getting Started Guide!
强烈推荐阅读入门指南

If you are in a huge hurry, the most important conceptual background can be
found in .sbt build definition, scopes, and more kinds of setting. But we don’t
promise that it’s a good idea to skip the other pages in the guide.

如果你是大忙人,最重要概念背景可以在sbt构造定义,范围,和其他类型设置找到。但是我们不承诺在这个入门指南中跳过其他页是一个好的主意

It’s best to read in order, as later pages in the Getting Started Guide build on
concepts introduced earlier.

最好是按顺序阅读,入门指南的后页都是基于前面介绍的术语

Thanks for trying out sbt and have fun!
感谢尝试sbt,玩的开心


Installing sbt

安装sbt

To create an sbt project, you’ll need to take these steps:
为了创建sbt项目,你需要执行以下步骤
• Install sbt and create a script to launch it.
安装sbt并且创建一个脚本启动它
• Setup a simple hello world project
设置一个简单的hello world 项目
– Create a project directory with source files in it.
    创建项目路径并且放入源文件
– Create your build definition.
    创建你的构造定义
• Move on to running to learn how to run sbt.
    继续到执行学习怎样执行sbt
• Then move on to .sbt build definition to learn more about build definitions.
    继续sbt构造定义去学习更多关于构造定义

Ultimately, the installation of sbt boils down to a launcher JAR and a shell
script, but depending on your platform, we provide several ways to make the
process less tedious. Head over to the installation steps for Mac, Windows,
Linux, or manual installation.

最后,安装sbt归结到一个启动jar 和一个shell 脚本,但是取决于你的系统,我们提供了少数方法使得这个处理乏味。转向安装步骤对于mac,windows,linux 获取手工安装

Tips and Notes
提示和说明
If you have any trouble running sbt, see Setup Notes on terminal encodings,
HTTP proxies, and JVM options.

如果你在执行sbt遇到任何麻烦,可以看设置说明在终端编码在http 代理和jvm配置

Installing sbt on Windows
安装sbt到windows

Windows installer
Download msi installer and install it.
直接安装

Installing Lightbend Activator (including sbt)

安装lightbendActivator


Lightbend Activator is a custom version of sbt which adds two extra commands,
activator ui and activator new. The activator command is a superset of sbt, in short.

Lightbend Activator是sbt定制版本添加了两个额外的命令,activator ui and activator new.,简单来说activator的命令是sbt的超集


其他版本安装请参考官网文档哈



Hello, World

This page assumes you’ve installed sbt.

这页假设你已经安装好sbt

Create a project directory with source code
A valid sbt project can be a directory containing a single source file. Try creating
a directory hello with a file hw.scala, containing the following:
object Hi {
def main(args: Array[String]) = println("Hi!")
}


创建一个项目目录和源代码。一个有效的sbt项目可以是一个目录只包含一个源文件。尝试创建一个hello文件并包含一个 hw.scala文件。文件内容如下。

注释:这是一段scala代码,定义了一个object单例,然后main方法打印hi


Now from inside the hello directory, start sbt and type run at the sbt interactive

console. On Linux or OS X the commands might look like this:
$ mkdir hello
$ cd hello
$ echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > hw.scala
$ sbt
...
> run
...
Hi!

上面一段是在linux和os x系统上直接创建一个hello文件夹和hw.scala文件的命令,然后启动sbt

-》sbt

->run 执行hw.scala的main方法

In this case, sbt works purely by convention. sbt will find the following automatically:

这这里例子中,sbt纯粹有约定来工作,sbt 会自动找到下面信息

• Sources in the base directory

在基本目录下的源文件

• Sources in src/main/scala or src/main/java

在src/main/scala or src/main/java源文件

• Tests in src/test/scala or src/test/java

可以在src/test/scala or src/test/java 下进行测试(java代码,scala是scaa路径)

• Data files in src/main/resources or src/test/resources

资源文件在src/main/resources 或者 src/test/resources

• jars in lib

需要依赖的jars在lib目录

By default, sbt will build projects with the same version of Scala used to run
sbt itself.

默认下,sbt会创建项目使用执行sbt 自身scala版本


You can run the project with sbt run or enter the Scala REPL with sbt console. Invoking sbt console sets up your project’s classpath so you can try
out live Scala examples based on your project’s code.

你可以通过sbt执行项目,或者在scala 的repl里面启动sbt。调用sbt 控制台设置你的项目类路径,这样你可以离开本scala基于你项目代码


Build definition

构造定义

 

Most projects will need some manual setup.Basic build settings go in a file

called build.sbt, located in the project’s base directory.

 

大多数项目需要一些手动设置。基本构造设置都放在一个文件夹叫build.sbt.处于项目的基本目录下

For example, if your project is in thedirectory hello, in hello/build.sbt you

might write:

例如如果你的项目在一个hello项目,在hello/build.sbt有可能写下面配置

lazy val root = (projectin file(".")). 设置root文件目录

settings(

name := "hello",  ---项目名称

version := "1.0"----项目版本

scalaVersion := "2.11.8"  -----当前scala版本

)

In .sbt build definition you’ll learn more about how to write a build.sbt file.

If you plan to package your project in a jar,you will want to set at least the

name and version in a build.sbt.

在sbt的构造定义,你可以学到更多关于怎样写一个build.sbt文件。如果你计划打包你的项目到一个jar。你可以需要设置至少name和version在build.sbt

 

Setting the sbt version

You can force a particular version of sbt bycreating a file hello/project/build.properties.

In this file, write:

你可以设定一个以前版本的sbt通过创建一个文件。Hello/project/build.properties里面写一下设置

sbt.version=0.13.13  --sbt版本0.13.13

to force the use of sbt 0.13.13. sbt is 99%source compatible from release to

release. Still, setting the sbt version in project/build.properties avoids any

potential confusion.

设置使用sbt0.13.13可以覆盖99%源文件兼容以前的版本。设置sbt版本在project/build.properties可以避免任何的潜在混乱

 

Directory structure

目录结构

This page assumes you’ve installed sbt and seen the Hello, World example.

这页假设你已经安装sbt并且看到hello world例子

Base directory

基本目录

In sbt’s terminology, the “base directory” is the directory containingthe

project. So if you created a project hello containing hello/build.sbt and

hello/hw.scala as in the Hello,World example, hello is your basedirectory.

在sbt术语中,基本目录是一个目录包含项目。所以如果你创建一个hello 项目包含了 hello/build.sbt 和 hello/hw.scala 在hello world 例子中,这个hello就是你的基本目录

 

Source code

Source code can be placed in the project’s base directory as with

hello/hw.scala. However, mostpeople don’t do this for real projects;

too much clutter.

源文件就是在项目的基本目录下的地方例如hello/hw.scala.尽管这样,大多数人不会在真实的项目中这样做,因为这样太杂乱了

 

sbt uses the same directory structure asMaven for source files by default (all

paths are relative to the base directory):

sbt使用和maven相同的目录结构,通过默认的路径相对于基本路径存放源文件

src/

main/

resources/

<files to include in main jar here>----资源文件

scala/

<main Scala sources>  ---主要scala代码

java/

<main Java sources>  ---主要java代码

test/

resources

<files to include in test jar here>  --测试资源文件

scala/

<test Scala sources>  --测试scala代码

java/

<test Java sources>   --测试java代码

Other directories in src/ will be ignored. Additionally, all hidden directories

will be ignored.

其他目录在src都会被忽略。此外,所有隐藏目录都会被忽略

 

sbt build definition files

sbt 构造定义文件

 

You’ve already seen build.sbt in the project’s base directory. Other sbtfiles

appear in a project subdirectory.

你可能已经看到build.sbt在项目的基本路径中。其他的sbt文件应用在项目的子路径下

project can contain .scala files, which are combined with .sbt files to form

the complete build definition. See organizingthe build for more.

项目可以包含.scala文件,这些文件可以结合.sbt文件形成一个完整的构造定义。看更多的组织build

 

build.sbt

project/

Build.scala

You may see .sbt files inside project/ but they are notequivalent to .sbt files

in the project’s base directory.Explaining this will come later, since you’ll need

some background information first.

你可以看到.sbt文件在项目/,但是他们并不是等价于.sbt文件在项目的基本目录下。后面会解释。因为你首先需要一些背景信息

 

Build products

构造生成

Generated files (compiled classes, packagedjars, managed files, caches, and

documentation) will be written to the target directory by default.

 

生的文件(编译classes,打包jars,管理文件,缓存和文档)默认会被写在一个目标路径

 

Configuring version control

设置版本控制

Your .gitignore (or equivalent for other version control systems) should contain:

你的gitgnore(或者其他等级的版本控制系统)应该包含

target/

Note that this deliberately has a trailing / (to match only directories) and it

deliberately has no leading / (to match project/target/ in addition toplain

target/).

注意这个是故意有一个/(只匹配目录)并且故意没有开头/(只匹配 project/target 除了普通的 target/)

Running

执行

This page describes how to use sbt once youhave set up your project. It assumes

you’ve installed sbt andcreated a Hello, World or other project.

这页描述一旦你已经设置好你的项目怎样使用sbt。假设你已经安装sbt 和创建 ahello world 或者其他的项目

 

Interactive mode

交互模式

Run sbt in your project directory with noarguments:

直接在你的项目上不需要参数运行sbt

$ sbt

Running sbt with no command line argumentsstarts it in interactive mode.

Interactive mode has a command prompt (withtab completion and history!).

运行sbt没有命令参数进入交互模式,在交互模式中有一个命令提示符

For example, you could type compile at the sbt prompt:

譬如你可以打入 compile  sbt 提示

> compile

To compile again, press uparrow and then enter.

To run your program, type run.

To leave interactive mode, type exit or use Ctrl+D (Unix) or Ctrl+Z (Windows).

Batch mode

批处理模式

You can also run sbt in batch mode,specifying a space-separated list of sbt

commands as arguments. For sbt commands thattake arguments, pass the

command and arguments as one argument to sbtby enclosing them in quotes.

你也可以执行sbt在批处理模式。指定一个空格分离列表作为sbt 命令参数。为了使sbt命令获取参数。传入命令和 参数组通过用引号包含作为一个参数传入给sbt

For example,

$ sbt clean compile "testOnly TestATestB" 

In this example, testOnly has arguments, TestA and TestB. The commands

will be run in sequence (clean, compile, then testOnly).

这个例子中 testOnly 是有参数的,TestA和TestB。这个命令会被逐次执行

 

Continuous build and test

持续构建和测试

To speed up your edit-compile-test cycle, youcan ask sbt to automatically recompile

or run tests whenever you save a source file.

为了加快你编辑-编译-测试流程速度。你可以叫sbt自动重新编译或者运行测试在你需要保存一个源文件的时候

Make a command run when one or more sourcefiles change by prefixing the

command with ~. For example, in interactive mode try:

当一个或者多个源文件改变的时候命令执行,通过在命令加一个前缀~。例如在交互模式下尝试

> ~ compile

Press enter to stop watching for changes.

You can use the ~ prefix with either interactive mode or batch mode.

See Triggered Execution for more details.

 

Common commands

通常的命令

Here are some of the most common sbtcommands. For a more complete list,

这里有一些最通常使用的sbt命令,更多的完整命令请查看Command Line Reference

see Command Line Reference.

<td><tt>clean</tt></td>

<td>Deletes all generated files (in the<tt>target</tt> directory).</td>

<td><tt>compile</tt></td>

<td>Compiles the main sources (in<tt>src/main/scala</tt> and

src/main/java directories).

<td><tt>test</tt></td>

<td>Compiles and runs alltests.</td>

<td><tt>console</tt></td>

<td>Starts the Scala interpreter with aclasspath including the

compiled sources and all dependencies. Toreturn to sbt, type :quit, Ctrl+D

(Unix), or Ctrl+Z (Windows).

<td><nobr>

<tt>run &lt;argument&gt;*</tt></nobr></td>

<td>Runs the main class for the projectin the same

virtual machine as sbt.

<td><tt>package</tt></td>

<td>Creates a jar file containing thefiles in

<tt>src/main/resources</tt> andthe classes compiled from <tt>src/main/scala</tt> and

<tt>src/main/java</tt>.</td>

<td><tt>help &lt;command&gt;</tt></td>

<td>Displays detailed help for thespecified command.

If no command is provided, displays briefdescriptions of all

commands.</td>

<td><tt>reload</tt></td>

<td>Reloads the build definition(<tt>build.sbt</tt>, <tt>project/*.scala</tt>,

<tt>project/*.sbt</tt> files).Needed if you change the build definition.</td>

 

Tab completion

Tab完成

Interactive mode has tab completion,including at an empty prompt. A special

sbt convention is that pressing tab once mayshow only a subset of most likely

completions, while pressing it more timesshows more verbose choices.

在交互模式下有tab完成选项。包括在一个空的提示符。一个特殊sbt约定按tab一次

可以展示只有一个最可能完成的子集。当传入多次展示跟多的选项

---展示所有命令

 

History Commands

历史命令

Interactive mode remembers history, even ifyou exit sbt and restart it. The

simplest way to access history is with the uparrow key. The following commands

are also supported:

在交互模式下记录历史。即使你退出了sbt并且重启它。最简单的方式访问历史就是使用上箭头按键。

<td><tt>!</tt></td>

<td>Show history commandhelp.</td>

<td><tt>!!</tt></td>

<td>Execute the previous commandagain.</td>

<td><tt>!:</tt></td>

<td>Show all previouscommands.</td>

<td><tt>!:n</tt></td>

<td>Show the last<tt>n</tt> commands.</td>

<td><tt>!n</tt></td>

<td>Execute the command with index<tt>n</tt>, as shown by the <tt>!:</tt>command.</td>

<td><tt>!-n</tt></td>

<td>Execute the nth command before thisone.</td>

<td><tt>!string</tt></td>

<td>Execute the most recent commandstarting with 'string.'</td>

<td><tt>!?string</tt></td>

<td>Execute the most recent commandcontaining 'string.'</td>

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值