Scala environment set up on ec2 , Mac, and for Eclipse

Today I want to take some notes to track my study process:

I prepare to setup Scala environment on my ec2 AMI, and Mac seperately
if the steps are same, I won't present them again.
Now let's do it:

A. Install sbt

1. JDK
in my Mac: java 1.8 --recommended 
in my ec2:



2. install SBT:
curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo
sudo mv bintray-sbt-rpm.repo /etc/yum.repos.d/
sudo yum install sbt







3. make a directory containing the source code
mkdir hello
cd hello
vi hw.scala(paste)
object Hi {
  def main(args: Array[String]) = println("Hi!")
}
or

mkdir hello
$ cd hello$ echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > hw.scala


4. run this Scala code
sbt












sbt 完全按照约定工作。会自动找到以下内容:

  • 项目根目录下的源文件
  • src/main/scala 或 src/main/java 中的源文件
  • src/test/scala 或 src/test/java 中的测试文件
  • src/main/resources 或 src/test/resources 中的数据文件
  • lib 中的 jar 文件
run 








5. Build definition:  build.sbt

 5.1 exit sbt




 5.2
The build definition goes in a file called build.sbt, located in the project’s base directory. You can take a look at the file, but don’t worry if the details of this build file aren’t clear yet. In .sbt build definition you’ll learn more about how to write a build.sbt file.

vi build.sbt
(paste)

lazy val root = (project in file("."))
  .settings(
    name := "hello",
    version := "1.0",
    scalaVersion := "2.12.1"
  )

编写 build.sbt 脚本的东西。

如果你准备将你的项目打包成一个 jar 包,在 build.sbt 中至少要写上 name 和 version

plus: setting the version of sbt 设置 sbt 版本 


可以通过创建 hello/project/build.properties 文件强制指定一个版本的 sbt。

$ mkdir project
$ vi build.properties
e.g. push to use the version 0.13.15:writing this 
sbt.version=0.13.15
sbt 在不同的 release 版本 99% 兼容。但在 project/build.properties 文件中设置 sbt 的版本仍然能避免一些潜在的混淆。for avoiding some latent confusion of version.

B. 一些sbt的结构:


  • base directory:

project hello
build.sbt + hw.scala

  • source code 

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
    java/
       <main Java sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
    java/
       <test Java sources>

  • Build support files
    other sbt files stored in the subdirectory of project:  hello/project/build.properties
project 目录可以包含 .scala 文件,这些文件最后会和 .sbt 文件合并共同构成完整的构建定义。想知道更多请参见 组织构建
  • Build products 
Generated files:  will be written to the target directory by default.
  1. compiled classes
  2. packaged jars
  3. managed files
  4. caches
  5. documentation
  • Configuring version control 
    Your .gitignore (or other  equivalent version control systems) should contain: target/
后面需要跟一个 / (只匹配目录)且前面不能有 / (除了匹配普通的 target/ 还匹配 project/target/ )

  • Build definition 
in build.sbt consists of a set of projects, subprojects, to define subproject and current project directory 
build.sbt 定义了一个 Project,它持有一个名为settings的scala表达式列表。
lazy val root = (project in file("."))
  .settings(
    name := "Hello",
    scalaVersion := "2.12.1"
  )
each subproject is configured k-v pairs
lazy val root = (project in file("."))
  .settings(
    name         := "hello",
    organization := "com.example",
    scalaVersion := "2.12.1",
    version      := "0.1.0-SNAPSHOT"
  )
each entry --setting expression --task expression
每项 Setting 都定义为一个 Scala 表达式。在 settings 中的表达式是相互独立的,且仅仅是表达式,不是完整 Scala 语句。
这些表达式可以用 vallazy valdef 声明。 build.sbt 不允许使用顶层的 object 和 class。它们必须写到 project/ 目录下作为完整的 Scala 源文件。
1. key : 
settingkey[T]一个 key 对应一个只计算一次的 value(这个值在加载project时计算,然后一直保存着)


使用 := 给一个 setting 赋一个值或者给一个 task 赋一种计算。对于 setting,这个值(value)只会在项目加载的时候执行一次。对于 task,这个计算会在 task 每次执行的时候重新计算。

takskey[T]
定义 task ,一个 key 对应一个称之 task 的 value每次都重新计算,可能存在潜在副作用
Tasks 是像 compile 或 package 的操作。它们可能返回 Unit在 Scala 中表示 void),或返回 task 相关的返回值
e.g. package 就是一个类型为 TaskKey[File] 的 task, 它的返回值是其生成的 jar 文件。
“taskiness” (是否每次都重新执行)是 key 的一个属性(property),而不是一个值(value)。


inputkey[T]一个 key 对应一个可以接收命令行参数的 task。
T-expected value type wrong value type will not compile
了解关于任何 key 内容,在 sbt 交互模式的command line输入 inspect <keyname>在顶部会显示 setting 的 value type和 setting 的简介。

2. operator :=

3. body 
organization := {"com.example"}
key              operator    body 

键(Keys)有一个返回 Setting[T] 的 := 方法。可以像使用 Java 的语法一样调用
lazy val root = (project in file("."))  .settings(    name.:=("hello")  )
Scala 允许 name := "hello" 这样调用(在 Scala 中,一个只有单个参数的方法可以使用任何一种语法调用)。


4th key: 

内置built-in Keys :是对象 Keys 的字段。build.sbt 会隐式包含 import sbt.Keys._,所以可以通过 name 取到 sbt.Keys.name

5th key:
user-defined
lazy val hello = taskKey[Unit]("一个 task 示例")
.sbt 文件除了可以包含(settings)外,还可以包含 vals 和 defs。 vals 和 defs 须以空行和(settings)分隔。
lazy vals, vals, defs 

添加依赖库 library Dependencie
两种方式添加第三方依赖。一种是将 jar 文件 放入 lib/(非托管的依赖)中,另一种是在 build.sbt 中添加托管的依赖
val derby = "org.apache.derby" % "derby" % "10.4.1.3"
lazy val commonSettings = Seq(  organization := "com.example",  version := "0.1.0",  scalaVersion := "2.12.1")
lazy val root = (project in file("."))  .settings(    commonSettings,    name := "hello",    libraryDependencies += derby

  )

+= 方法而不是 :=+= 方法是将新值追加该 key 的旧值后而不是替换它,第二个是 % % 方法用来从字符串构造 Ivy 模块 ID 

每一个 key 可以在多个上下文中关联一个值,每个上下文称之为 “scope”


  • 如果在build defenition中有multiple projects,在每个project中同一个 key 可有不同值。
  • 如果根据不同的情形编译它们,key compile 对于 main 源文件和 test 源文件可以有不同的值。
  • Key packageOptions(包含创建 jar 包的一些选项)可以有不同值,在对 class 文件打包时是 packageBin,对源代码打包时是 packageSrc

C. Installing the scala IDE for Eclipsee with the Scala worksheet 


  • creating the eclipse project from sbt

the assignment is a sbt project
ant eclipse user must follow steps here to generate eclipse project files in order to open project withe eclipse otherwise eclipse will not reorganize the project at all

make a new sbt project which will later use from Eclipse









hello-world
├── build.sbt
├── project
│   └── build.properties
└── src
    └── main
        └── scala
            └── Main.scala

4 directories, 3 files



once we have an sbt project we need to generate our eclipse files

  • Now we need to tell sbt that we would like to use the `sbteclipse` plugin. 


~/mymooc-workspace
$ echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")' > hello-world/plugins.sbt
$ sbt
...
>eclipse:
error like
$ sbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Set current project to sbteclipse (in build file:/Users/jacek/sandbox/sbteclipse/)
> eclipse
[error] Not a valid command: eclipse (similar: help, alias)
[error] Not a valid project ID: eclipse (similar: sbteclipse)
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: eclipse (similar: deliver, licenses, clean)
[error] eclipse
[error]        ^





I tried several methods on stack overflow still have no reasonable solution
if you also meet this problem
I suggested you to try
sbt eclipse

since I have addressed this problem by this command line, and succeed to create eclipse project by  sbt.

D. opening the project in Eclipse

  1.  "File" - "Import" from the menu
  2. In the folder "General", select the item "Existing Projects into Workspace" and click "Next >"



Note that sbt can only be started inside a project directory, so first navigate to the project directory that you created in Part 1.





评论

此博客中的热门博文

8 Link Analysis

1 Map reduce problems

NoSql and AWS DynamoDB practices