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 }))
}
Run with gradlew all In Jar. task:build << allInJar
jar.
│ Main.class
│ sample.txt
│
├─subs
│ sample1.txt
│ sample2.txt
│
├─META-INF
│ MANIFEST.MF
│
└─sample
└─subs
Sub1.class
Sub2.class
reference; Thorough introduction to Gradle Building an automation platform with next-generation build tools (written by Takuma Watabiki, Nobuhiro Sue, Masatoshi Hayashi, Katsunobu Imai) java – Can I Gradle multiple projects into one jar file?
Recommended Posts