Gradle 5.4.1
tree.
├─sub
│ ├─sub-sample1
│ │ └─src
│ │ ├─main
│ │ │ ├─java
│ │ │ │ └─sample
│ │ │ │ └─subs
│ │ │ │ Sub1.java
│ │ │ │
│ │ │ └─resources
│ │ │ └─subs
│ │ │ subSample1.txt
│ │ └─test
│ │ ├─java
│ │ └─resources
│ └─sub-sample1
│ └─src
│ ├─main
│ │ ├─java
│ │ │ └─sample
│ │ │ └─subs
│ │ │ Sub2.java
│ │ │
│ │ └─resources
│ │ └─subs
│ │ subSample2.txt
│ │
│ └─test
│ ├─java
│ └─resources
└─src
├─main
│ ├─java
│ │ Main.java
│ │
│ └─resources
│ sample.txt
│
└─test
├─java
└─resources
build.gradle
allprojects {
apply plugin: 'java'
group 'sample'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'
repositories { mavenCentral() }
dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' }
}
dependencies {
compile project(':sub:sub-sample1')
compile project(':sub:sub-sample2')
}
def mainProjects = [':sub:sub-sample1', ':sub:sub-sample2']
task allInJar(type:Jar, dependsOn: mainProjects.collect({ it+':compileJava' })){
dependsOn build
baseName = rootProject.name
manifest.attributes 'Main-Class': 'Main'
def javaSubDir = "/classes/java/main"
def resourcesSubDir = "/resources/main"
from(buildDir.path + javaSubDir,
files(mainProjects.collect{ project(it).buildDir.path + javaSubDir }))
from(buildDir.path + resourcesSubDir,
files(mainProjects.collect{ project(it).buildDir.path + resourcesSubDir }))
}
gradlew allInJarで実行.
task:build << allInJar
jar.
│ Main.class
│ sample.txt
│
├─subs
│ sample1.txt
│ sample2.txt
│
├─META-INF
│ MANIFEST.MF
│
└─sample
└─subs
Sub1.class
Sub2.class
参考;
Gradle徹底入門 次世代ビルドツールによる自動化基盤の構築(著 綿引 琢磨, 須江 信洋, 林 政利, 今井 勝信)
java – 複数のプロジェクトを1つのjarファイルにGradleできますか?