如何构建、编译和运行 Scala 项目? [英] How to build, compile and run a Scala project?

查看:87
本文介绍了如何构建、编译和运行 Scala 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想评估我在 Github 上找到的一个 Scala 项目,即 TRank.>

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

java.lang.RuntimeException:未检测到主类.在 scala.sys.package$.error(package.scala:27​​)

现在项目文件在 src/main/scala/io/mem0r1es/trank 中,当我尝试通过 scalac 编译或运行 sbt run 在那里我收到一堆关于对象不是基础包成员的错误,即 对象排名不是包 io.mem0r1es.trank 的成员

如果能帮助我了解如何运行这个 Scala 项目,我将不胜感激.

解决方案

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

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

<代码>>包裹[信息] 正在更新 {file:/Users/jacek/sandbox/TRank/}trank...[信息] 解析 org.fusesource.jansi#jansi;1.4 ...[信息] 更新完毕.[信息] 将 13 个 Scala 源编译到/Users/jacek/sandbox/TRank/target/scala-2.10/classes...[警告] 有 1 个弃用警告;使用 -deprecation 重新运行以获取详细信息[警告] 发现一个警告[信息] 打包/Users/jacek/sandbox/TRank/target/scala-2.10/trank_2.10-1.0.jar ...[信息] 完成包装.

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

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

<代码>>帮跑运行一个主类,传递命令行上提供的参数.

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

<代码>>跑步java.lang.RuntimeException:未检测到主类.在 scala.sys.package$.error(package.scala:27​​)[trace] 堆栈跟踪被抑制:运行 last compile:run 以获得完整输出.[错误] (compile:run) 未检测到主类.[错误] 总时间:0 秒,2014 年 9 月 24 日晚上 9:40:52 完成

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

执行 test 来触发测试:

<代码>>测试[信息] 将 5 个 Scala 源编译到/Users/jacek/sandbox/TRank/target/scala-2.10/test-classes...[警告] 有 2 个功能警告;使用 -feature 重新运行以获取详细信息[警告] 发现一个警告[信息] ANC_DEPTHSpec:[信息] ANC_DEPTH 排名器[信息] - 应该正确排列类型[信息] - 不提供类型时不应失败[信息] 祖先规格:[信息] 一个祖先排名者[信息] - 应该正确排列类型[信息] - 不提供类型时不应失败[信息] DEPTHSpec:[信息] 深度排名者[信息] - 应按最大深度对类型进行排名[信息] - 不提供类型时不应失败从 edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz 加载分类器 ... [info] PreProcessorSpec:[信息] 预处理器[信息] - 应该从 HTML 内容中删除样板[信息] - 应保留完整的文本内容[信息] - 不应因空内容而失败完成 [2.8 秒].[信息] NERSpec:[信息] NER[信息] - 应该提取实体标签[信息] - 不应该因没有命名实体的内容而失败[信息] - 不应因空内容而失败[信息] 通过:总计 12,失败 0,错误 0,通过 12[成功] 总时间:4 s,完成时间:2014-09-24 9:42:09

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

<代码>>文档[信息] 主要 Scala API 文档到/Users/jacek/sandbox/TRank/target/scala-2.10/api...模型包含 20 个可记录的模板[信息] 主要 Scala API 文档成功.[成功] 总时间:1 s,完成时间:2014-09-24 9:43:22 PM

您也可以使用 console 任务进入 Scala REPL 并自己玩转类型.

<代码>>安慰[信息] 启动 Scala 解释器...[信息]欢迎使用 Scala 版本 2.10.2(Java HotSpot(TM) 64 位服务器 VM,Java 1.8.0_20).输入表达式以对它们进行评估.输入 :help 以获取更多信息.标度>

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

I would like to evaluate a Scala project that I have found on Github, namely TRank.

I found the build file build.sbt. I managed to install Scala and sbt via homebrew and then run the command sbt run on the project root folder. Doing so ended up with an error:

java.lang.RuntimeException: No main class detected.
    at scala.sys.package$.error(package.scala:27)

Now the project files are in the src/main/scala/io/mem0r1es/trank and when I try to compile via scalac or run sbt run there I get a bunch of errors about objects not being members of the base packaged i.e. object ranking is not a member of package io.mem0r1es.trank

I would greatly appreciate some help knowing how to run this Scala project.

解决方案

As you've already noticed the project is managed by sbt.

In order to package a project, i.e. creating a jar with all the project artifacts that are supposed to be distributed, you execute 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.

By default sbt manages sources under src/main/scala directory. That's where you could find an App object to run.

In sbt, run searches all the sources under src/main/scala for applications.

> help run
Runs a main class, passing along arguments provided on the command line.

If a project has no main classes, the error is printed out:

> 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

It does mean that the project has no main classes, but there are other ways to "prove" that an API works properly - using tests.

Execute test to fire the tests:

> 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

So, to learn the project you should review the sources of the tests (under src/test/scala) and a scaladoc that you can generate using doc task:

> 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

You could also use console task to enter Scala REPL and play with the types yourself.

> 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>

With the tests, the scaladoc and the Scala REPL, you should be all set to learn the API of the project.

这篇关于如何构建、编译和运行 Scala 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆