2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gradleでsrc/main/javaにresourceファイルを置きたい

Last updated at Posted at 2019-07-30

背景

gradleでは

  • javaファイルは src/java
  • それ以外のファイルは src/resources

配置する構成が用意されていますが、java ファイルと関連が強いファイルを resources 側に置くと関連がわかりづらくなるという課題があります。
そういった、javaファイルのすぐ隣に関連するリソースファイルを配置したいというニーズにお答えします。
(例えば、java と fxml を隣に並べたいとか)

解法

build.gradle への記載はこれだけです。

build.gradle
// java-libraryなどのpluginが適用されているとして

sourceSets {
    main {
        resources {
            srcDir "src/main/java"
        }
    }
}

補足

  • ぱっと見では src/main/resources を上書きしてしまっているように思えますが、 srcDir追記なので、デフォルトの src/main/resources は残ります。
    • この記述では、main resourcesに src/main/resourcessrc/main/java の2つを指定していることになります。
  • *.javaファイルがresources扱いされそうに見えますが、 *.javaは resourcesからexcludeされている ので、src/main/java内の*.javaがリソースとして扱われることはありません。

gradle 4.4 で動作確認しています。

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?