文件

§Play 應用程式的結構

§Play 應用程式配置

Play 應用程式的配置標準化,以盡可能保持簡單。在首次編譯成功後,專案結構如下所示

app                      → Application sources
 └ assets                → Compiled asset sources
    └ stylesheets        → Typically LESS CSS sources
    └ javascripts        → Typically CoffeeScript sources
 └ controllers           → Application controllers
 └ models                → Application business layer
 └ views                 → Templates
build.sbt                → Application build script
conf                     → Configurations files and other non-compiled resources (on classpath)
 └ application.conf      → Main configuration file
 └ routes                → Routes definition
dist                     → Arbitrary files to be included in your projects distribution
public                   → Public assets
 └ stylesheets           → CSS files
 └ javascripts           → Javascript files
 └ images                → Image files
project                  → sbt configuration files
 └ build.properties      → Marker for sbt project
 └ plugins.sbt           → sbt plugins including the declaration for Play itself
lib                      → Unmanaged libraries dependencies
logs                     → Logs folder
 └ application.log       → Default log file
target                   → Generated stuff
 └ resolution-cache      → Info about dependencies
 └ scala-2.13
    └ api                → Generated API docs
    └ classes            → Compiled class files
    └ routes             → Sources generated from routes
    └ twirl              → Sources generated from templates
 └ universal             → Application packaging
 └ web                   → Compiled web assets
test                     → source folder for unit or functional tests

§app/ 目錄

app 目錄包含所有可執行人工製品:Java 和 Scala 原始碼、範本和編譯後的資產來源。

app 目錄中有三個套件,每個套件對應 MVC 架構模式的一個組成部分

您可以新增自己的套件,例如 app/services 套件。

注意:在 Play 中,controllersmodelsviews 套件名稱只是約定俗成,必要時可以變更(例如,使用 com.yourcompany 作為所有內容的前綴)。

還有一個名為 app/assets 的選用目錄,用於編譯後的資產,例如 LESS 來源CoffeeScript 來源

§public/ 目錄

儲存在 public 目錄中的資源是靜態資產,由 Web 伺服器直接提供服務。

此目錄分成三個子目錄,分別為圖片、CSS 樣式表和 JavaScript 檔案。您應該這樣整理您的靜態資源,以保持所有 Play 應用程式的一致性。

在一個新建立的應用程式中,/public 目錄會對應到 /assets URL 路徑,但您可以輕鬆地變更它,甚至為您的靜態資源使用多個目錄。

§conf/ 目錄

conf 目錄包含應用程式的設定檔。有兩個主要的設定檔

如果您需要加入特定於您應用程式的設定選項,建議您在 application.conf 檔中加入更多選項。

如果一個函式庫需要一個特定的設定檔,建議您在 conf 目錄下提供它。

§lib/ 目錄

lib 目錄是可選的,它包含未管理的函式庫依賴項,亦即您想要在建置系統外手動管理的所有 JAR 檔。只要將任何 JAR 檔放到這裡,它們就會加入到您的應用程式類別路徑中。

§build.sbt

您的專案主要建置宣告通常會在專案根目錄的 build.sbt 中找到。

§project/ 目錄

project 目錄包含 sbt 建置定義

§target/ 目錄

target 目錄包含建置系統所產生的一切。知道這裡產生了什麼可能會很有用

§典型的 .gitignore 檔案

產生的資料夾應由版本控制系統忽略。以下是 Play 應用程式的典型 .gitignore 檔案

logs
project/project
project/target
target
tmp
dist
.bsp
.cache
RUNNING_PID

§預設 sbt 配置

您也可以選擇使用 sbtMaven 使用的預設配置。若要使用此配置,您必須停用配置外掛程式並為 twirl 範本設定明確監控

// Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>

lazy val root: Project = (project in file("."))
  .enablePlugins(PlayScala)
  // Use sbt default layout
  .disablePlugins(PlayLayoutPlugin)

這將停止 Play 覆寫預設 sbt 配置,其外觀如下

build.sbt                  → Application build script
src                        → Application sources
 └ main                    → Compiled asset sources
    └ java                 → Java sources
       └ controllers       → Java controllers
       └ models            → Java business layer
    └ scala                → Scala sources
       └ controllers       → Scala controllers
       └ models            → Scala business layer
    └ resources            → Configurations files and other non-compiled resources (on classpath)
       └ application.conf  → Main configuration file
       └ routes            → Routes definition
    └ twirl
       └ views             → Templates
    └ assets               → Compiled asset sources
       └ css               → Typically LESS CSS sources
       └ js                → Typically CoffeeScript sources
    └ public               → Public assets
       └ css               → CSS files
       └ js                → Javascript files
       └ images            → Image files
 └ test                    → Unit or functional tests
    └ java                 → Java source folder for unit or functional tests
    └ scala                → Scala source folder for unit or functional tests
    └ resources            → Resource folder for unit or functional tests
 └ universal               → Arbitrary files to be included in your projects distribution
project                    → sbt configuration files
 └ build.properties        → Marker for sbt project
 └ plugins.sbt             → sbt plugins including the declaration for Play itself
lib                        → Unmanaged libraries dependencies
logs                       → Logs folder
 └ application.log         → Default log file
target                     → Generated stuff
 └ scala-2.13
    └ cache
    └ classes              → Compiled class files
    └ classes_managed      → Managed class files (templates, ...)
    └ resource_managed     → Managed resources (less, ...)
    └ src_managed          → Generated sources (templates, ...)

接下來:使用 Play 主控台


在這個文件發現錯誤?此頁面的原始碼可以在 這裡 找到。在閱讀 文件指南 後,請隨時提交拉取請求。有問題或建議要分享?前往 我們的社群論壇 與社群展開對話。