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

时间:2014-09-24 13:26:19

标签: 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)

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

我非常感谢知道如何运行此Scala项目的一些帮助。

2 个答案:

答案 0 :(得分: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.

默认情况下,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
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

这确实意味着该项目没有主要课程,但还有其他方法来证明" 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

因此,要了解该项目,您应该查看测试的来源(在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

你也可以使用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>

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

答案 1 :(得分:2)

TRank是一个SBT项目。您需要从此站点http://www.scala-sbt.org/

安装SBT

然后你可以像这样构建它:

sbt clean compile package

这将在target /文件夹中为您创建一个jar文件。获得jar文件后,就像使用Java Jar文件一样使用它(除了必须使用scala自动包含scala库jar):

scala -cp /path/to/trank_2.10-1.0.jar com.example.SomeMainClass