gpt4 book ai didi

scala - 如何设置 sbt/scala/play 多模块项目,它可以与 Intellij scala 插件一起正常工作

转载 作者:行者123 更新时间:2023-12-04 14:37:49 26 4
gpt4 key购买 nike

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

  • 一个多模块项目,其中包含一个带有此布局的 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
  • 我希望能够使用 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

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

    构建.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
    )


    依赖项.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)
    }

    插件.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")

    运行配置
    enter image description here

    最佳答案

    考虑更改构建结构,以便 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 中所述:
  • Run | Edit Configurations
  • 点击+添加新配置
  • 选择sbt Task
  • tasks输入框放api/run
  • 应用更改并选择确定。
  • 关于scala - 如何设置 sbt/scala/play 多模块项目,它可以与 Intellij scala 插件一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56407815/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com