1. 程式人生 > >Play框架2.5.6教程——Play應用的剖析

Play框架2.5.6教程——Play應用的剖析

Play應用的佈局

Play應用的佈局是以儘可能簡單為標準的。第一次成功編譯之後,Play應用如下所示:

app                      Application sources
  assets                Compiled asset sources
     stylesheets        Typically LESS CSS sources
     javascripts        TypicallyCoffeeScript 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 publicPublic assets stylesheets CSS files javascripts Javascript files images Image files project sbt configuration files build
.properties Markerfor sbt project plugins.sbt sbt plugins including the declaration forPlay itself lib Unmanaged libraries dependencies logs Logs folder application.log Default log file target Generated stuff resolution-cache Info about dependencies scala-2.11 api Generated API docs classes Compiledclass 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/controllers

app/models

app/views

當然你可以增加你自己的包,比如增加一個app/utils包。

注意在Play中,controllers,models和views包的名字只是目前約定的,如果需要的話可以更改,(比如在每一個之前加上字首com.yourcompany)。

對於編譯後的資原始檔,比如LESS資源和CoffeeScript資源也有可選擇的目錄叫app/assets。

Public目錄

儲存在public目錄中的資源是直接為伺服器服務的靜態資源。

這個目錄分成三個子目錄,分別存放圖片,CSS樣式表和JavaScript檔案,你應該按照這樣的目錄來組織你的靜態資源,從而保持所有Play應用的一致性。

在一個新建立的目錄中,Public目錄對映到/assertURl的目錄中,但是你可以很簡單地改變這些目錄,甚至可以為你的資源設定幾個不同的目錄。

conf目錄

conf包含應用的配置檔案。其中有兩個主要的配置檔案:

application.conf:應用的主要配置檔案,包含了配置的引數。

routes:註釋檔案的路徑。

如果你需要為你的應用新增特殊的配置選項,最好將更多的選項加到application.conf中。

如果一個類庫需要特殊的配置檔案,儘量在conf目錄下建立。

lib目錄

lib目錄是可選擇的並且包含非管理的庫依賴,你想要在編譯系統外手動管理的所有jar包,只需將jar包存到該目錄,它們將會增加到你的應用路徑中。

build.sbt檔案

你的專案主要構建宣告通常在專案根檔案中的build.sbt建立。在project目錄中的.scala檔案也可以用來宣告你專案的建立。

project目錄

project目錄包含sbt構建定義:

plugins.sbt定義專案要使用的sbt外掛。

build.properties包含用來建立你app的sbt版本。

target目錄

target目錄包含編譯系統生成的任意檔案,如果你想知道在這裡生成了什麼,這個目錄很有用。

classes包含了所有編譯好的類(從Java跟Scala資原始檔編譯成的)。

classes_managed只包含這個框架管理的類(比如路由器或模板系統生成的類)。它可以增加這個類資料夾作為你的IDE專案外部的類資料夾。

resource_managed包含生成好的資源,比如LESS CSS和CoffeeScript編譯結果的典型編譯後的資原始檔。

src_managed包含生成的資源,比如模板系統生成的Scala資源。

web包含被sbt-web處理後的資源,比如來自app/assets和public資料夾的資源。

典型的.gitignore檔案

生成的資料夾應該被你的版本控制系統忽視。這是一個Play應用典型的·.gitignore檔案:

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

預設的SBT佈局

你也可以選擇使用SBT和Maven預設的佈局。請注意這些佈局是試驗性的,可能會存在問題。為了使用這個佈局,你不能使用佈局的相關外掛並給可用的模板建立明確的檢查:

disablePlugins(PlayLayoutPlugin)PlayKeys.playMonitoredFiles ++=(sourceDirectories in(Compile,TwirlKeys.compileTemplates)).value
下面的程式碼會阻止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                TypicallyCoffeeScript sources
    publicPublic assets
        css                CSS files
        js                Javascript files
        images            Image files
  test                    Unitor 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        Markerfor sbt project
  plugins.sbt              sbt plugins including the declaration forPlay itself
lib                        Unmanaged libraries dependencies
logs                       Logs folder
  application.log         Default log file
target                     Generated stuff