How to build, compile and run a Scala project?

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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.

回答1:

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.



回答2:

TRank is a SBT project. You need to install SBT from this site http://www.scala-sbt.org/

Then you can build it like so:

sbt clean compile package 

This will create a jar file for you in target/ folder. Once you get the jar file, you can use it just like you use Java Jar files ( except that you must use scala to include scala library jars automatically ):

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!