如何设置sbt / scala / play多模块项目,该项目可与Intellij scala插件配合使用

时间:2019-06-01 15:02:24

标签: scala playframework sbt

我正在建立一个新的多模块项目(sbt / scala / play / IntejjiJ),我想做两件事:

  1. 具有一个具有这种布局的build.sbt文件的多模块项目
project
 |_Dependencies.scala
 |_plugin.sbt
modules
 |_adapters
   |_api (Play REST API)
     |_app
     |_conf
   |_infrastructure (bare bone scala)
     |-src/main/scala
 |_application (bare bone scala)
   |_src/main/scala
 |_domain (bare bone scala)
   |_src/main/scala
 |_query (bare bone scala)
   |_src/main/scala 
build.sbt
  1. 我希望能够使用IntelliJ Play2插件(运行/调试配置)

到目前为止,使用Play2的“运行/调试配置”设置运行应用程序时出现以下错误:

[error] java.lang.RuntimeException: No main class detected.
[error]     at scala.sys.package$.error(package.scala:26)
[error] (Compile / bgRun) No main class detected.
[error] Total time: 2 s, completed Jun 1, 2019 11:21:31 PM

这是我到目前为止所拥有的:

build.sbt

import Dependencies._

lazy val commonSettings = Seq(
  organization := "com.borkke.rally",
  version := "0.1.0-SNAPSHOT",
  scalaVersion := "2.12.8",
  scalacOptions := Seq(
    "-deprecation",
    "-feature"
  ),
  libraryDependencies ++= CommonDependencies
)


//PROJECTS
lazy val rally = project
  .in(file("."))
  .aggregate(domain,application,query,api,infrastructure)
  .settings(
    name := "rally",
    commonSettings,
    publishArtifact := false
  )

lazy val api = project
  .in(file("modules/adapter/api"))
  .enablePlugins(PlayScala)
  .dependsOn(domain,application,query,infrastructure)
  .settings(
    name := "api",
    commonSettings,
    libraryDependencies ++= ApiDependencies
  )

lazy val domain = project
  .in(file("modules/domain"))
  .settings(
    name := "domain",
    commonSettings
  )

lazy val application = project
  .in(file("modules/application"))
  .dependsOn(domain)
  .settings(
    name := "application",
    commonSettings
  )

lazy val query = project
  .in(file("modules/query"))
  .settings(
    name := "query",
    commonSettings
  )

lazy val infrastructure = project
  .in(file("modules/adapter/infrastructure"))
  .dependsOn(domain)
  .settings(
    name := "infrastructure",
    commonSettings,
    libraryDependencies ++= InfrastructureDependencies
  )

Dependencies.scala

import sbt._
import play.sbt.PlayImport._

object Dependencies {
  private val scalatest_version = "3.0.5"
  private val v2Db_version = "1.4.198"
  private val logback_version = "5.3"
  private val play_version = "2.7.2"
  private val cassandra_driver_version = "3.7.1"
  private val postgresql_driver_version = "42.2.5"
  private val kafka_client_version = "2.2.0"

  private val scalatest = "org.scalatest" %% "scalatest" % scalatest_version
  private val scalatic = "org.scalactic" %% "scalactic" % scalatest_version
  private val h2Db = "com.h2database" %% "h2" % v2Db_version
  private val logback = "net.logstash.logback" % "logstash-logback-encoder" % logback_version
  private val play = "com.typesafe.play" %% "play" % play_version
  private val cassandra_driver = "com.datastax.cassandra" % "cassandra-driver-extras" % cassandra_driver_version
  private val postgresql_driver = "org.postgresql" % "postgresql" % postgresql_driver_version
  private val kafka_client = "org.apache.kafka" %% "kafka" % kafka_client_version

  lazy val CommonDependencies = Seq(scalatic, scalatest % "test", logback, guice)

  lazy val InfrastructureDependencies = Seq(cassandra_driver, postgresql_driver, kafka_client)

  lazy val ApiDependencies = Seq(play)
}

plugin.sbt

logLevel := Level.Warn

//wrapper around play console.
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.2")

//dependency resolver. parallel downloads
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.1.0-M11")

//shows available updates. dependencyUpdates || dependencyUpdatesReport
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.4.0")

//create one jar for application.
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")

//linter
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.2.1")

2 个答案:

答案 0 :(得分:6)

考虑更改构建结构,以使api成为根项目,如下所示:

lazy val api = project
  .in(file("."))
  .enablePlugins(PlayScala)
  .aggregate(domain,application,query,infrastructure)
  .dependsOn(domain,application,query,infrastructure)
  .settings(
    name := "api",
    commonSettings,
    publishArtifact := false,
    libraryDependencies ++= ApiDependencies
  )

这意味着modules/adapters/api已移至项目根目录,并且lazy val root = ...已从build.sbt中删除,因此目录结构变为

.
├── app
│   ├── controllers
│   ├── filters
│   ├── services
│   └── views
├── build.sbt
├── conf
│   ├── application.conf
│   ├── logback.xml
│   └── routes
├── modules
│   ├── adapters
│   │   └── infrastructure
│   ├── application
│   │   ├── src
│   ├── domain
│   │   ├── src
│   └── query
│       ├── src
├── project
│   ├── Dependencies.scala
│   ├── build.properties
│   ├── plugins.sbt
│   ├── project
│   └── target

这可以使Play 2 App运行配置再次正常运行,尽管通过以下操作确保启用了Play编译器:

Preferences | Languages & Frameworks | Play2 | Compiler | Use Play 2 compiler for this project

如果希望保留原始结构,则作为解决方法,尝试定义sbt Task运行配置而不是Play 2 App,如官方docs中所述:

  1. Run | Edit Configurations
  2. 点击+添加新配置
  3. 选择sbt Task
  4. tasks输入框中,放入api/run
  5. 应用更改并选择确定。

答案 1 :(得分:1)

背景上下文,请随意跳过:

在使用 Play2 文档中定义的 sbt task 运行配置时,最新版本的 Intellij 会遇到路由编译问题。所以我想使用 Play 2 App 运行配置作为替代。

虽然马里奥的解决方案有效,但对于那些无法移动基本目录的人来说,这不是一个选择。尝试使用 Play 2 App 运行配置会导致 No main class detected 类似于 Aleksandar 在开头问题的评论中所经历的。

解决方案

要使用 Play 2 App 运行配置,您必须:

  1. 像往常一样在 build.sbt 中指定项目。

  2. 使用您的 build.sbt 为项目指定 Play2 模块和项目 uri。除非在步骤 1 中更改,否则这将是 Play2 默认值:分别是项目名称和项目根。

    此处找到有问题的设置:

    File | Settings | Languages & Frameworks | Play2

    如下面的屏幕截图所示,我还检查了 Use Play 2 compiler for this project

    Project uri defined in intellij settings

  3. 创建一个 Play 2 App 运行配置并使用您的主类方法将 Play 2 module 指定为模块。

    调试端口应该是您的断点侦听器设置中指定的端口。默认无需更改。

    Run config definition

  4. 运行您新创建的运行配置。日志输出应该类似于通过 bash 运行 sbt 的输出。示例如下。

    Server starting from play 2 app

如果它无法启动,请确保您没有在 Scala 和 JVM 的默认设置中重复声明 JVM 参数。