Gradle settings memo (multi-project to all in one) for myself

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

Gradle settings memo (multi-project to all in one) for myself
Eclipse Pleiades All in One for Mac released
Gradle settings for using JUnit 5
Spring Boot 2 multi-project in Gradle
What is object-oriented after all or just one thing to watch out for in programming
Set up Gradle multi-project in IntelliJ to build JAR file
Gradle TIPS collection (for myself)
eclipse all in one installation
How to run Ant in Gradle
Make multi-project executable JAR in Gradle
Docker execution memo summarized for myself
Settings for SSL debugging in Java
Best practice to change settings for each environment in iOS app (Swift)
One liner to delete all Docker images
Settings related to connection pool in Tomcat.jdbc
How to filter JUnit Test in Gradle
Settings for running CGI programs locally (for myself)
Configure a multi-project with subdirectories in Gradle
Memo for migration from java to kotlin
English translation of Pleiades All in One
[For beginners] How to debug in Eclipse
For my son who started studying Java with "Introduction to Java" in one hand