SBT構建開發環境與代理和倉庫配置

五柳-先生發表於2016-01-26
SBT構建開發環境
1)安裝
安裝大體可參考http://www.scala-sbt.org/release/tutorial/zh-cn/Combined+Pages.html 中所示,我的過程如下:
windows環境,下載sbt-0.13.9.zip,解壓後到D:/sbt/(不要有空格的路徑),然後修改D:/sbt/conf目錄下的sbtconfig.txt檔案,在最後加入如下幾行配置:
-Dsbt.ivy.home=D:/sbt/.ivy2
-Dsbt.global.base=D:/sbt/.sbt
-Dsbt.repository.config=D:/sbt/conf/repo.properties
-Dsbt.log.format=true

##如下為代理的配置包括http與https.
-Dhttp.proxyHost=10.18.11.11
-Dhttp.proxyPort=8080
-Dhttp.proxyUser=xx
-Dhttp.proxyPassword=xx

-Dhttps.proxyHost=10.18.1111
-Dhttps.proxyPort=8080
-Dhttps.proxyUser=xx
-Dhttps.proxyPassword=xx

第一個是設定本地自定義repository路徑(如果不設定就是預設的使用者目錄)。
然後在D:/sbt/conf/目錄下新建repo.properties檔案,內容為:
[repositories]
  local
  comp-maven:http://mvnrepository.com/artifact/
  store_cn:http://maven.oschina.net/content/groups/public/
  store_mir:http://mirrors.ibiblio.org/maven2/
  store_0:http://maven.net.cn/content/groups/public/
  store_1:http://repo.typesafe.com/typesafe/ivy-releases/
  store_2:http://repo2.maven.org/maven2/
  sbt-releases-repo: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
  sbt-plugins-repo: http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
  maven-central: http://repo1.maven.org/maven2/

請確保一定要增加高亮部分,否則會出現如下錯誤, 最後把路徑D:\sbt\bin 新增到Path中。

D:\work\scala\sbt\bin>sbt.bat
Getting org.scala-sbt sbt 0.13.9 ...

:: problems summary ::
:::: WARNINGS
                module not found: org.scala-sbt#sbt;0.13.9
        ==== local: tried
          D:\work\scala\sbt\.ivy2\local\org.scala-sbt\sbt\0.13.9\ivys\ivy.xml
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          D:\work\scala\sbt\.ivy2\local\org.scala-sbt\sbt\0.13.9\jars\sbt.jar
        ==== comp-maven: tried
          http://mvnrepository.com/artifact/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.pom
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          http://mvnrepository.com/artifact/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.jar
        ==== store_cn: tried
          http://maven.oschina.net/content/groups/public/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.pom
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          http://maven.oschina.net/content/groups/public/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.jar
        ==== store_mir: tried
          http://mirrors.ibiblio.org/maven2/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.pom
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          http://mirrors.ibiblio.org/maven2/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.jar
        ==== store_0: tried
          http://maven.net.cn/content/groups/public/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.pom
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          http://maven.net.cn/content/groups/public/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.jar
        ==== store_1: tried
          http://repo.typesafe.com/typesafe/ivy-releases/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.pom
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          http://repo.typesafe.com/typesafe/ivy-releases/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.jar
        ==== store_2: tried
          http://repo2.maven.org/maven2/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.pom
          -- artifact org.scala-sbt#sbt;0.13.9!sbt.jar:
          http://repo2.maven.org/maven2/org/scala-sbt/sbt/0.13.9/sbt-0.13.9.jar
                ::::::::::::::::::::::::::::::::::::::::::::::
                ::          UNRESOLVED DEPENDENCIES         ::
                ::::::::::::::::::::::::::::::::::::::::::::::
                :: org.scala-sbt#sbt;0.13.9: not found
                ::::::::::::::::::::::::::::::::::::::::::::::

:::: ERRORS
        SERVER ERROR: Proxy Timeout ( The connection timed out. For more information about this event, see ISA Server Help.  ) url=http://repo2.maven.org/maven2/org/scala-sbt/sbt/0
.13.9/sbt-0.13.9.pom

:: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS
unresolved dependency: org.scala-sbt#sbt;0.13.9: not found
Error during sbt execution: Error retrieving required libraries
  (see D:\work\scala\sbt\.sbt\boot\update.log for complete log)
Error: Could not retrieve sbt 0.13.9

如果使用Idea來開發有關scala sbt專案,那需要在idea裡安裝scala外掛,如下圖:

然後在settings裡更改兩個地方:

第一個是sbt_launch.jar的本地路徑。
第二個是VM parameters的設定,加入最後兩行就可使用本地ivy庫。
這樣就完成了sbt與idea scala外掛的安裝,以後用idea建sbt工程都會用那個本地庫。

3)在cmd中,進入專案路徑中後,鍵入sbt,然後就可使用命令(compile,package)就可以將依賴包加到庫中,編譯,測試,打包等。

4)有關libraryDependencies,格式為:groupID % artifactID % revision,或者groupID %% artifactID % revision,使用後者的話,artifactID將會加上當前scala版本對應的jar的version號,我認為這種方式很容易造成錯誤。

If you use groupID %% artifactID % revision rather than groupID % artifactID % revision (the difference is the double %% after the groupID), sbt will add your project’s Scala version to the artifact name. This is just a shortcut. You could write this without the %%:

libraryDependencies += "org.scala-tools" % "scala-stm_2.11.1" % "0.3"

Assuming the scalaVersion for your build is 2.11.1, the following is identical (note the double %% after "org.scala-tools"):

libraryDependencies += "org.scala-tools" %% "scala-stm" % "0.3"

相關參考:
http://www.scala-sbt.org/release/tutorial/zh-cn/Combined+Pages.html 
http://my.oschina.net/u/580483/blog/110499 修改sbt預設快取路徑
http://www.cnblogs.com/vincent-hv/p/3298416.html 本地編寫並執行scala程式
http://www.scala-sbt.org/0.13/tutorial/Library-Dependencies.html sbt官方 http://stackoverflow.com/questions/23845357/changing-ivy-cache-location-for-sbt-projects-in-intellij-idea 解決本地庫問題

相關文章