You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不依赖SBT,使用Mill构建工具搭建PlayFramework项目?

全程用Mill搭建Play Framework Scala项目(无需SBT)

兄弟,我懂你现在的麻烦——本来想用Mill搭Play Scala项目,结果还得先跑SBT生成结构再删一堆SBT相关的东西,完全不符合Mill的简洁风格对吧?别愁,下面给你一套纯Mill的流程,全程不用碰SBT一步,完全贴合Mill的使用习惯:

1. 创建并进入项目目录

先搞定基础的目录结构,执行以下命令:

mkdir -p /path/to/play-scala-seed-mill && cd /path/to/play-scala-seed-mill

2. 编写Mill核心构建文件 build.sc

这是整个配置的核心,直接创建并写入以下内容,它会帮你整合Play Framework的依赖、编译、运行等逻辑:

import mill._
import mill.scalalib._
import mill.scalalib.publish._
import play.mill._

object playScalaSeed extends PlayModule with ScalaModule with PublishModule {
  // 指定Scala版本,选和Play兼容的版本,比如2.13.12
  override def scalaVersion = "2.13.12"
  // 指定Play Framework版本,比如2.8.20
  override def playVersion = "2.8.20"

  // Play项目的核心依赖
  override def ivyDeps = Agg(
    ivy"com.typesafe.play::play-guice:${playVersion()}",
    ivy"com.typesafe.play::play-logback:${playVersion()}"
  )

  // 测试依赖
  override def testIvyDeps = Agg(
    ivy"com.typesafe.play::play-test:${playVersion()}" % Test
  )

  // 配置Play的资源目录(默认和Play的结构一致)
  override def playConfDir = T.source(millSourcePath / "conf")
  override def playAssetsDir = T.source(millSourcePath / "public")
  override def playViewsDir = T.source(millSourcePath / "app" / "views")

  // 发布相关配置(如果不需要发布可以注释掉)
  override def publishVersion = "0.1.0-SNAPSHOT"
  override def pomSettings = PomSettings(
    description = "Play Scala Seed Project built with Mill",
    organization = "com.example",
    url = "https://example.com",
    licenses = Seq(License.MIT),
    scm = SCM(
      url = "https://github.com/example/play-scala-seed-mill",
      connection = "scm:git:git://github.com/example/play-scala-seed-mill.git"
    ),
    developers = Seq(Developer("yourname", "Your Name", "https://yourwebsite.com"))
  )
}

3. 创建Play项目的基础目录结构

Play有固定的目录规范,我们手动创建这些必要的目录:

mkdir -p app/controllers app/views conf public test/controllers

4. 添加Play的初始配置文件和代码

现在需要补上Play运行必需的配置和基础代码:

4.1 配置文件 conf/application.conf

写入基础的Play配置:

# 生成自己的密钥:可以用`openssl rand -hex 16`生成
play.http.secret.key = "your-generated-secret-key-here"
play.http.router = "routes"
play.server.http.port = 9000

4.2 路由文件 conf/routes

添加初始路由规则:

GET     /       controllers.HomeController.index()

4.3 基础控制器 app/controllers/HomeController.scala

创建一个简单的首页控制器:

package controllers

import play.api.mvc._

class HomeController @javax.inject.Inject()(val controllerComponents: ControllerComponents) extends BaseController {
  def index() = Action { implicit request: MessagesRequest[AnyContent] =>
    Ok(views.html.index())
  }
}

4.4 首页视图 app/views/index.scala.html

创建基础的HTML视图:

@()
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Welcome to Play Scala with Mill!</title>
</head>
<body>
    <h1>Hello from Play Framework, built entirely with Mill!</h1>
</body>
</html>

4.5 测试用例 test/controllers/HomeControllerSpec.scala

添加基础的控制器测试:

package controllers

import org.scalatestplus.play._
import org.scalatestplus.play.guice._
import play.api.test._
import play.api.test.Helpers._

class HomeControllerSpec extends PlaySpec with GuiceOneAppPerTest with Injecting {

  "HomeController" should {

    "render the index page from a new instance of controller" in {
      val controller = new HomeController(stubControllerComponents())
      val home = controller.index().apply(FakeRequest(GET, "/"))

      status(home) mustBe OK
      contentType(home) mustBe Some("text/html")
      contentAsString(home) must include ("Hello from Play Framework")
    }

    "render the index page from the application" in {
      val controller = inject[HomeController]
      val home = controller.index().apply(FakeRequest(GET, "/"))

      status(home) mustBe OK
      contentType(home) mustBe Some("text/html")
      contentAsString(home) must include ("Hello from Play Framework")
    }
  }
}

5. 生成IDEA项目文件

现在可以用Mill直接生成IDEA的项目配置,方便开发:

mill mill.scalalib.GenIdea/idea

6. 运行项目验证

最后一步,启动项目看看效果:

mill run

等项目启动完成后,访问 http://localhost:9000,就能看到我们的首页了!


内容的提问来源于stack exchange,提问作者Shafique Jamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:50:15