how to set main class in SBT 0.13 project

后端 未结 9 702
旧时难觅i
旧时难觅i 2020-12-23 18:58

Could you guys please explain to me how to set main class in SBT project ? I\'m trying to use version 0.13.

My directory structure is very simple (unlike SBT\'s docu

相关标签:
9条回答
  • 2020-12-23 19:05

    If the Main class is in a different project then by setting the below command in build.sbt would work:

    addCommandAlias("run", "; project_folder/run")
    
    0 讨论(0)
  • 2020-12-23 19:13

    You need to put your application's source in src/main/scala/, project/ is for build definition code.

    0 讨论(0)
  • 2020-12-23 19:13

    There are 4 options

    1. you have 1 main class

      • sbt run and sbt will find main for you
    2. you have 2 or more main classes

      • sbt run and sbt will propose to select which one you want to run.

    Multiple main classes detected, select one to run:
    [1] a.b.DummyMain1
    [2] a.b.DummyMain2
    Enter number:
    

    1. You want to set main class manually.

      mainClass in run := Some("a.b.DummyMain1")
      
    2. You could run with main class as parameter

      sbt runMain a.b.DummyMain1
      
    0 讨论(0)
  • 2020-12-23 19:15

    For custom modules in SBT (0.13), just enter on the SBT console:

     project moduleX
     [info] Set current project to moduleX (in build file:/path/to/Projects/)
     >   run
     [info] Running main 
    

    to switch scope to moduleX, as define in Built.scala. All main classes within that scope will be detected automatically. Otherwise you get the same error of no main class detected. For God's sake, SBT does not tell you that the default scope is not set. It has nothing to do with default versus non default source folders but only with SBT not telling anything that it doesn't know which module to use by default.

    Big Hint to typesafe: PLEASE add a default output like:

    [info] Project module is not set. Please use ''project moduleX''  set scope 
    or set in Built file (LinkToDocu)  
    

    at the end of SBT start to lower the level of frustration while using SBT on multi module projects.....

    0 讨论(0)
  • 2020-12-23 19:16

    I had the same issue. Resolved it after adding PlayMinimalJava plugin in build.sbt.

    Not sure how it got fixed, if someone can highlight how PlayMinimalJava solves it, would be great.

    enablePlugins(PlayMinimalJava)
    

    My build.sbt looks like this

    organization := "org"
    version := "1.0-SNAPSHOT"
    scalaVersion := "2.13.1"
    libraryDependencies += guice
    enablePlugins(PlayMinimalJava)
    

    Log

    C:\Users\KulwantSingh\repository\pdfdemo>sbt run
    Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
    [info] Loading settings for project pdfdemo-build from plugins.sbt ...
    [info] Loading project definition from C:\Users\KulwantSingh\repository\pdfdemo\project
    [info] Loading settings for project pdfdemo from build.sbt ...
    [info] Set current project to pdfdemo (in build file:/C:/Users/KulwantSingh/repository/pdfdemo/)
    [warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
    
    --- (Running the application, auto-reloading is enabled) ---
    
    [info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
    
    (Server started, use Enter to stop and go back to the console...)
    
    [info] Compiling 6 Scala sources and 2 Java sources to C:\Users\KulwantSingh\repository\pdfdemo\target\scala-2.13\classes ...
    [info] p.a.h.EnabledFilters - Enabled Filters (see <https://www.playframework.com/documentation/latest/Filters>):
    
        play.filters.csrf.CSRFFilter
        play.filters.headers.SecurityHeadersFilter
        play.filters.hosts.AllowedHostsFilter
    
    [info] play.api.Play - Application started (Dev) (no global state)
    
    0 讨论(0)
  • 2020-12-23 19:23

    I had the same issue: was mode following the tutorial at http://www.scala-sbt.org/0.13/docs/Hello.html, and in my opinion, as a build tool sbt's interaction and error messages can be quite misleading to a newcomer.

    It turned out, hours of head scratching later, that I missed the critical cd hello line in the example each time. :-(

    0 讨论(0)
提交回复
热议问题