§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アーキテクチャパターンの各コンポーネントに対応する3つのパッケージがあります。
app/controllers
app/models
app/views
独自のパッケージ(例:app/services
パッケージ)を追加できます。
**注意**: Playでは、
controllers
、models
、views
というパッケージ名は単なる規則であり、必要に応じて変更できます(すべてにcom.yourcompany
というプレフィックスを付けるなど)。
LESSソースやCoffeeScriptソースなどのコンパイル済みアセット用のオプションのディレクトリである`app/assets`もあります。
§public/
ディレクトリ
public
ディレクトリに格納されているリソースは、Webサーバーによって直接提供される静的アセットです。
このディレクトリは、画像、CSSスタイルシート、JavaScriptファイルの3つのサブディレクトリに分割されています。すべてのPlayアプリケーションの整合性を保つために、静的アセットをこのように整理する必要があります。
新しく作成されたアプリケーションでは、
/public
ディレクトリは/assets
URLパスにマッピングされていますが、これを簡単に変更したり、静的アセットに複数のディレクトリを使用したりすることもできます。
§conf/
ディレクトリ
conf
ディレクトリには、アプリケーションの構成ファイルが含まれています。主な構成ファイルは2つあります。
application.conf
は、アプリケーションのメイン構成ファイルです。routes
は、ルーターの定義ファイルです。
アプリケーションに固有の構成オプションを追加する必要がある場合は、application.conf
ファイルにオプションを追加することをお勧めします。
ライブラリに特定の構成ファイルが必要な場合は、conf
ディレクトリの下に配置することをお勧めします。
§lib/
ディレクトリ
lib
ディレクトリはオプションであり、管理対象外のライブラリの依存関係、つまりビルドシステムの外部で手動で管理するすべてのJARファイルが含まれています。JARファイルをここにドロップするだけで、アプリケーションのクラスパスに追加されます。
§build.sbt
ファイル
プロジェクトのメインビルド宣言は、通常、プロジェクトのルートにある`build.sbt`にあります。
§project/
ディレクトリ
project
ディレクトリには、sbtビルド定義が含まれています。
plugins.sbt
は、このプロジェクトで使用されるsbtプラグインを定義します。build.properties
には、アプリのビルドに使用する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
.bsp
.cache
RUNNING_PID
§デフォルトのsbtレイアウト
sbtとMavenで使用されるデフォルトのレイアウトを使用することもできます。このレイアウトを使用するには、レイアウトプラグインを無効にし、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コンソールの使用
このドキュメントに誤りを見つけましたか?このページのソースコードはこちらにあります。ドキュメントガイドラインを読んだ後、プルリクエストを送信してください。質問やアドバイスがありますか?コミュニティフォーラムにアクセスして、コミュニティとの会話を開始してください。