LoginSignup
0
0

More than 3 years have passed since last update.

PlayFrameworkでサブモジュールのassetsが見つからない場合

Last updated at Posted at 2019-05-20

TL; DR

PlayFrameworkでアセットパスがわからなくなったら
sbt "show playAllAssets"
が超便利

sizer@sizle:~/hello-play-assets (master *+>)
$ sbt "show playAllAssets"
[info] Loading global plugins from /Users/sizer/.sbt/1.0/plugins
[info] Loading settings for project hello-play-assets-build from plugins.sbt ...
[info] Loading project definition from /Users/sizer/hello-play-assets/project
[info] Loading settings for project root from build.sbt ...
[info] Loading settings for project child from build.sbt ...
[info] Set current project to hello-play-assets (in build file:/Users/sizer/hello-play-assets/)
[info] child / playAllAssets
[info]  List((public/,/Users/sizer/hello-play-assets/child/target/web/public/main))
[info] playAllAssets
[info]  List((public/,/Users/sizer/hello-play-assets/target/web/public/main))
[success] Total time: 0 s, completed 2019/05/20 19:36:09
###
# アセットパスの一覧が見れる。 target/web/public/main に入っているらしい。
###
$ tree target/web/public/main/
target/web/public/main/
├── images
│   └── favicon.png
├── javascripts
│   └── main.js
├── lib
│   └── child
│       ├── images
│       │   ├── favicon.png
│       │   └── image_in_child.jpg
│       ├── javascripts
│       │   └── main.js
│       └── stylesheets
│           └── main.css
└── stylesheets
    └── main.css

8 directories, 7 files
###
# lib/{moduleName}/images で指定すれば取得できるらしい
###

背景

PlayFramework(Java)でwebアプリを開発している。
共通部分など、子モジュールに切り出している。
子モジュール内のアセットを呼び出したい。→見つからない...

├── build.sbt
├── child #=> ここが共通部分
│   ├── build.sbt
│   ├── app
│   ├── conf
│   └── public #=> ここに画像入れる
├── conf
│   ├── application.conf
│   ├── logback.xml
│   └── routes
├── project
│   ├── build.properties
│   ├── plugins.sbt
└── public
    ├── images
    ├── javascripts
    └── stylesheets

`

公式ドキュメントを読む

When you package your application, all assets for the application, including all sub projects, are aggregated into a single jar
How are public assets packaged? - playframework.com

サブプロジェクトも含めてjarファイルにしてくれるのね。ええやん

publicフォルダに入れて、 Assets.at(file: String) とか Assets.versioned(path="/public", file: Asset) でアクセスするのね

→アクセスできずハマる

公式のISSUEを漁る

子モジュールのassets読み込めないよー的なISSUEを見つけた!

Did you add the sbt web plugin or the play plugin (which transitively adds the sbt web plugin) to the subproject? Assets are now no longer considered classpath resources
...
If you add the play plugin, you can put assets in the assets directory or the public directory. If you add the sbt web plugin, then you'll have to add the assets to the default directories for sbt-web, src/main/assets or src/main/public.
https://github.com/playframework/playframework/issues/2944#issuecomment-44243541

子モジュールにsbt-webプラグインかplayプラグイン追加したか?でないとassetsはclasspathに入らないで

(そういう事はドキュメントに書いてくれ...)

If you add the play plugin, you can put assets in the assets directory or the public directory. If you add the sbt web plugin, then you'll have to add the assets to the default directories for sbt-web, src/main/assets or src/main/public.
https://github.com/playframework/playframework/issues/2944#issuecomment-44243541

もしplayプラグインを入れたらassetsディレクトリかpublicディレクトリを読むで。
sbt-webプラグインの場合はsrc/main/assetssrc/main/public な。

(そういう事はドキュメントに...)

Could you run show play-all-assets, and see if the assets are in there? Also useful may be to run show childproject/play-prefix-and-assets, and see if the child project assets are in there.
https://github.com/playframework/playframework/issues/2944#issuecomment-44359790

show play-all-assets を実行したら子プロジェクトのassetsの場所がわかるで

(そういう事はドキュ...)

実行した

ちなみに

- show play-all-assets
+ show playAllAssets

だった

sizer@sizle:~/hello-play-assets (master *+>)
$ sbt "show playAllAssets"
[info] Loading global plugins from /Users/sizer/.sbt/1.0/plugins
[info] Loading settings for project hello-play-assets-build from plugins.sbt ...
[info] Loading project definition from /Users/sizer/hello-play-assets/project
[info] Loading settings for project root from build.sbt ...
[info] Loading settings for project child from build.sbt ...
[info] Set current project to hello-play-assets (in build file:/Users/sizer/hello-play-assets/)
[info] child / playAllAssets
[info]  List((public/,/Users/sizer/hello-play-assets/child/target/web/public/main))
[info] playAllAssets
[info]  List((public/,/Users/sizer/hello-play-assets/target/web/public/main))
[success] Total time: 0 s, completed 2019/05/20 19:36:09
$ tree target/web/public/main/
target/web/public/main/
├── images
│   └── favicon.png
├── javascripts
│   └── main.js
├── lib
│   └── child
│       ├── images
│       │   ├── favicon.png
│       │   └── image_in_child.jpg
│       ├── javascripts
│       │   └── main.js
│       └── stylesheets
│           └── main.css
└── stylesheets
    └── main.css

8 directories, 7 files

Assets.at("lib/child/images/youfile.jpg) って場所に入ってた。

lib/{moduleName} ってなんやねん、そういう事はドキュメントに書いてくれや!

所感

公式ドキュメントの不親切なフレームワークまじ :poop:

追伸

lib/{moduleName} ってprefixヤメたいってISSUE作った。:thumbsup: ください
sbt-web に出すのが適切なのかも知れんけど詳しい人教えてくださいませ

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