LoginSignup
0
0

sbtでマルチプロジェクトな構成を作る

Posted at

やること

sbt のマルチプロジェクトに対応したgiter8テンプレートを作成する

環境

OS: Windows 11
Java : Open JDK 17.0.7
sbt : 1.9.0
scala : 3.3.0

参考

https://qiita.com/kijuky/items/64ea7e1c95ede6799a24
この記事で挙げられていた
https://github.com/kijuky/springboot-scala.g8/tree/scala3

ここ。scala3ブランチを使う。

構成はほぼ同じで、
serverだけちょっと違う。

変更点

build.sbt
val scala3Version = "3.3.0"
val AkkaVersion = "2.7.0"
val AkkaHttpVersion = "10.5.2"

ThisBuild / organization := "com.isageek.hakushimeita"
ThisBuild / scalaVersion := scala3Version
ThisBuild / version := "0.1.0-SNAPSHOT"

lazy val root = (project in file(".") withId "XNote")
  .aggregate(server, client, shared.jvm, shared.js)
  .settings(Compile / bgRun := (server / Compile / bgRun).evaluated)


lazy val server = project
  .dependsOn(shared.jvm)
  .enablePlugins(JavaAppPackaging, SbtWeb)
  .settings(
    Compile / mainClass := Some("com.isageek.hakushimeita.xnote.server.HttpServerRoutingMinimal"),
    libraryDependencies ++= Seq(
      "org.scalameta" %% "munit" % "0.7.29" % Test,
      "com.typesafe.akka" %% "akka-actor-typed" % AkkaVersion,
      "com.typesafe.akka" %% "akka-stream" % AkkaVersion,
      "com.typesafe.akka" %% "akka-http" % AkkaHttpVersion,
    )
  )

lazy val client = project
  .dependsOn(shared.js)
  .enablePlugins(ScalaJSPlugin, ScalaJSWeb)
  .settings(
    scalaJSUseMainModuleInitializer := true,
    libraryDependencies  += "org.scala-js" % "scalajs-dom_sjs1_2.13" % "2.4.0"
  )


lazy val shared = crossProject(JSPlatform, JVMPlatform)
  .crossType(CrossType.Pure)
  .in(file("shared"))
  .jsConfigure(_.enablePlugins(ScalaJSWeb))

serverのプロジェクトのごちゃごちゃした設定を除いて
mainクラスをHttpServerRoutingMinimalにしている。
それからlibraryDependenciesはAkka-HTTP関連のものになっている。

HttpServerRoutingMinimalオブジェクトはこちら。

/server/src/main/scala/$package$/server/Main.scala
/*
@main def hello: Unit =
  println("Hello world!")
  println(msg)

def msg = "I was compiled by Scala 3. :)"
*/

/*
 * Copyright (C) 2020-2023 Lightbend Inc. <https://www.lightbend.com>
 */

//package docs.http.scaladsl
package com.isageek.hakushimeita.xnote.server

import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import scala.io.StdIn

object HttpServerRoutingMinimal {

  def main(args: Array[String]): Unit = {

    implicit val system = ActorSystem(Behaviors.empty, "my-system")
    // needed for the future flatMap/onComplete in the end
    implicit val executionContext = system.executionContext

    val route =
      path("hello") {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
        }
      }

    val bindingFuture = Http().newServerAt("localhost", 8080).bind(route)

    println(s"Server now online. Please navigate to http://localhost:8080/hello\nPress RETURN to stop...")
    StdIn.readLine() // let it run until user presses return
    bindingFuture
      .flatMap(_.unbind()) // trigger unbinding from the port
      .onComplete(_ => system.terminate()) // and shutdown when done
  }
}

sbt run してやれば、サーバが起動する。

予定

clientを動かす方法はまだよくわかっていない。
scala.jsからjavascriptを吐き出すようにさせたい。

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