[没打算用idea,就总结下eclipse中如何构建和开发scala项目]
1.基本概念
scala 一种语法。(类似java) http://www.scala-lang.org/
sbt 一个构建工具。(类似maven,gradle,ant等) http://www.scala-sbt.org/
eclipse 。。这家伙只有scala开发环境的插件,可以构建scala project,但是没有sbt 插件。。就像没有maven插件的eclipse,只能构建和编辑java project,但是整不了maven project.
2.步骤。
@构建Scala语言环境
和java一样,分两步,下载软件,和配置HOME和Path.
@安装SBT 工具
下载、配置全局变量(在windows环境中添加sbt.bat脚本,可以直接运行)
@使用SBT生成一个类似maven 包结构的scala project
3.实际操作
@新建一个目录叫 test
@在test目录中新建文件build.sbt
@在test目录新建project目录,进入project目录,并新建plugins.sbt,在其中添加
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")
@在build.sbt中配置工程的name,scala编译环境,依赖等等。如:
import sbt._
import Process._
import Keys._
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Resource
lazy val commonSettings = Seq(
name := "test",
organization := "com.marsyoung",
version := "0.0.1-SNAPSHOT",
scalaVersion := "2.11.7"
)
lazy val root = (project in file(".")).
settings(commonSettings: _*).
settings(
libraryDependencies ++= Seq(
"junit" % "junit" % "4.4",
"javax.ws.rs" % "jsr311-api" % "1.1.1"
)
)
@在cmd中进入对应的project目录,即test目录。运行sbt。
@执行eclipse命令,将对应的项目转化成可以引入eclipse并且目录结构类似maven的项目。
@打开已经安装了scala ide的eclipse,导入对应的project,会自动的编译成scala peoject.
4.Tips:
SBT配置使其支持本地maven和私服,如下:
在用户根目录下的.sbt文件夹内,在windows下就是C->用户->用户名->.sbt目录下新建repositories文件并插入内容:
[repositories]
local
activator-launcher-local: file:${activator.local.repository-${activator.home-${user.home}/.activator}/repository}, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
activator-local: file:${activator.local.repository-D:/maven/repo3.3.1}
sohu-public: http://xxx.com/nexus/content/groups/public
typesafe-releases: http://repo.typesafe.com/typesafe/releases
typesafe-ivy-releasez: http://repo.typesafe.com/typesafe/ivy-releases, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
sonatype-oss-releases
sonatype-oss-snapshots
maven-central
本地maven地址为:
D:/maven/repo3.3.1
nexus私服地址为:
http://xxx.com/nexus/content/groups/public
有点不爽的是,如果有新的依赖或者包需要引入,在eclipse中不会自动编译,还得手动的去sbt控制台去compile和eclipse。
希望过段时间会有sbt的插件吧。(2015-12-24 )