如何构建,编译和运行Scala项目?

Ahm*_*saf 6 scala sbt

我想评估一下我在Github上发现的Scala项目,即TRank.

我找到了构建文件build.sbt.我设法通过自制软件安装Scala和sbt,然后sbt run在项目根文件夹上运行命令.这样做最终导致错误:

java.lang.RuntimeException: No main class detected.
    at scala.sys.package$.error(package.scala:27)
Run Code Online (Sandbox Code Playgroud)

现在项目文件在src/main/scala/io/mem0r1es/trank,当我尝试编译scalac或运行sbt run那里时,我得到一堆关于对象不是基础打包成员的错误,即object ranking is not a member of package io.mem0r1es.trank

我非常感谢知道如何运行这个Scala项目的一些帮助.

Jac*_*ski 6

正如您已经注意到该项目由sbt管理.

为了打包项目,即创建一个包含应该分发的所有项目工件的jar,执行package.

> package
[info] Updating {file:/Users/jacek/sandbox/TRank/}trank...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 13 Scala sources to /Users/jacek/sandbox/TRank/target/scala-2.10/classes...
[warn] there were 1 deprecation warning(s); re-run with -deprecation for details
[warn] one warning found
[info] Packaging /Users/jacek/sandbox/TRank/target/scala-2.10/trank_2.10-1.0.jar ...
[info] Done packaging.
Run Code Online (Sandbox Code Playgroud)

默认情况下,sbt管理src/main/scala目录下的源.在那里你可以找到一个App要运行的对象.

在sbt中,run搜索src/main/scala应用程序下的所有源.

> help run
Runs a main class, passing along arguments provided on the command line.
Run Code Online (Sandbox Code Playgroud)

如果项目没有主类,则打印出错误:

> run
java.lang.RuntimeException: No main class detected.
    at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed Sep 24, 2014 9:40:52 PM
Run Code Online (Sandbox Code Playgroud)

它确实意味着该项目没有主类,但还有其他方法可以"证明"API正常工作 - 使用测试.

执行test以触​​发测试:

> test
[info] Compiling 5 Scala sources to /Users/jacek/sandbox/TRank/target/scala-2.10/test-classes...
[warn] there were 2 feature warning(s); re-run with -feature for details
[warn] one warning found
[info] ANC_DEPTHSpec:
[info] An ANC_DEPTH ranker
[info] - should rank types properly
[info] - should not fail when no types are provided
[info] ANCESTORSSpec:
[info] An ANCESTORS ranker
[info] - should rank types properly
[info] - should not fail when no types are provided
[info] DEPTHSpec:
[info] A DEPTH ranker
[info] - should rank types by maximum depth
[info] - should not fail when no types are provided
Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... [info] PreProcessorSpec:
[info] A PreProcessor
[info] - should remove boilerplate from HTML content
[info] - should leave intact textual content
[info] - should not fail with empty content
done [2.8 sec].
[info] NERSpec:
[info] A NER
[info] - should extract entity labels
[info] - should not fail with content without Named Entities
[info] - should not fail with empty content
[info] Passed: Total 12, Failed 0, Errors 0, Passed 12
[success] Total time: 4 s, completed Sep 24, 2014 9:42:09 PM
Run Code Online (Sandbox Code Playgroud)

因此,要了解项目,您应该查看测试的来源(下src/test/scala)和可以使用doc任务生成的scaladoc :

> doc
[info] Main Scala API documentation to /Users/jacek/sandbox/TRank/target/scala-2.10/api...
model contains 20 documentable templates
[info] Main Scala API documentation successful.
[success] Total time: 1 s, completed Sep 24, 2014 9:43:22 PM
Run Code Online (Sandbox Code Playgroud)

您还可以使用console任务输入Scala REPL并自行播放类型.

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala>
Run Code Online (Sandbox Code Playgroud)

通过测试,scaladoc和Scala REPL,您应该已经准备好学习项目的API.