LoginSignup
0
0

More than 5 years have passed since last update.

マルチプロジェクト構成時に、サブプロジェクト内のクラスを実行する

Last updated at Posted at 2019-03-02

サブプロジェクトのクラスを実行できない

以下のようなbuild.sbtを用意する。
httpプロジェクトはakka-httpを含んでおり、当アプリケーションの実行時はakka-httpのWEBサーバーを起動したい。

build.sbt
val baseName = "fp"

lazy val domain = ...

lazy val application = ...

lazy val entity = ...

lazy val http = (project in file("modules/infrastructure/http"))
  .settings(
    ...
    )
  )

lazy val `root` = (project in file("."))
  .settings(
    name := baseName
  )
  .settings(coreSettings)
  .aggregate(
    domain,
    application,
    entity,
    http
  )
mainClass in Compile := Some("com.louvre2489.fp.infrastructure.http.WebServer")

これを普通に実行すると以下のように失敗する。
ルートプロジェクトからサブプロジェクト内のモジュールが見えていないみたい。

 > sbt run                                                                                                                                                                                                                     (git)-[master]
[info] Loading settings for project global-plugins from idea.sbt ...
[info] Loading global plugins from /home/nori/.sbt/1.0/plugins
[info] Loading settings for project fp-build from plugins.sbt ...
[info] Loading project definition from /home/nori/workspace/scala/fp/project
[info] Loading settings for project root from build.sbt ...
[info] Set current project to fp (in build file:/home/nori/workspace/scala/fp/)
[info] Packaging /home/nori/workspace/scala/fp/target/scala-2.12/fp_2.12-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Running com.louvre2489.fp.infrastructure.http.WebServer
[error] (run-main-0) java.lang.ClassNotFoundException: com.louvre2489.fp.infrastructure.http.WebServer
[error] java.lang.ClassNotFoundException: com.louvre2489.fp.infrastructure.http.WebServer
[error]         at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:709)
[error]         at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
[error] Nonzero exit code: 1
[error] (Compile / run) Nonzero exit code: 1
[error] Total time: 1 s, completed 2019/03/02 16:22:59

サブプロジェクトを指定して実行する

「サブプロジェクト軸」を指定することで、任意のサブプロジェクトに対する操作を行うことができる。
sbt Reference Manual

runする際に実行したいプロジェクトのIDを指定して実行することで、サブプロジェクトのプログラムを実行することができる。

 > sbt http/run                                                                                                                                                                                                                (〜省略〜)

Server online at http://0.0.0.0/5000/
Press RETURN to stop...

httpがアプリケーションのエントリーポイントであることを明確にするために、mainClassはサブプロジェクトの宣言内に入れてしまった方が明示的でわかりやすい。

build.sbt
val baseName = "fp"

lazy val domain = ...

lazy val application = ...

lazy val entity = ...

lazy val http = (project in file("modules/infrastructure/http"))
  .settings(
    mainClass in Compile := Some("com.louvre2489.fp.infrastructure.http.WebServer"),
    ...
    )
  )

lazy val `root` = (project in file("."))
  .settings(
    name := baseName
  )
  .settings(coreSettings)
  .aggregate(
    domain,
    application,
    entity,
    http
  )
// mainClass in Compile := Some("com.louvre2489.fp.infrastructure.http.WebServer")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0