frazman
frazman

Reputation: 33293

using sbt but coding in eclipse

A very naive question. I am using eclipse to write scala code.. Inside my eclipse, in src.. I create a new package..

 helloworld.scala
package bar.foo
object Hi {
      def main(args: Array[String]) = println("Hi!")
 }

and then following is my build.sbt file

name := "Hello World"

version := "1.0"

scalaVersion:= "2.10.4"

sbtVersion := "0.13.1"

libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test"

So, the directory helloworld has two files (helloworld.scala and build.sbt)

Now, sbt run runs just fine..

But now, I switch to eclipse.. and inside src create a package bar.foo and then inside that copied this code..

I create a package bar.foo

and then copy the code helloworld.scala into this new code..

Now when I do sbt update.. It creates a directory

src/bar/foo/helloworld.scala and other standard stuff

On doing sbt run complains..

[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 2 s, completed Apr 9, 2014 2:23:56 PM

Basically, how do i write a hello world example in scala in eclipse.. inside a package..and use sbt to compile and run it. Thanks

Upvotes: 1

Views: 98

Answers (2)

Bauhaus
Bauhaus

Reputation: 519

I think sbt is having trouble finding your file. Try changing the package structure to this instead: src/main/scala/helloworld.scala and try running sbt from within the parent directory of /src

The typical baseline sbt project structure looks something like this:

root/
    build.sbt
    src/
        main/
            scala/
            java/
            resources/
        test/
            scala/
            java/
            resources/
    project/
    target/

sbt will automatically find any source code placed within the directories that follow this convention, however if you want to change the paths to your source code you can of course do so by placing the following line into your build.sbt:

unmanagedSourceDirectories in Compile := List(file("./src/bar/foo/"))

Here are some links to get you started:

Upvotes: 1

AmigoNico
AmigoNico

Reputation: 6862

I think you'll find life easier if you let SBT generate your Eclipse project files for you. You'll need this plugin:

https://github.com/typesafehub/sbteclipse

Let build.sbt be your source of truth and generate whatever else you need from that.

Upvotes: 1

Related Questions