How to build distribution package in multi-module Gradle project?

Kiran Mohan :

I have seen this post Gradle multi project distribution but still have some doubts.
I would like to create the following project layout

root
  |--lib-java-module
  |--spring-boot-module
  |--3PP_A_module         # not java 
  |       |-- custom scripts, config
  |--3PP_B_module         # not java 
  |       |-- custom scripts, config
  |--dist-module

As you might have guessed, I want the dist-module to build myapp-dist.tar.gz with libjava.jar, sprintbootapp.jar, 3pp-a.tar, 3pp-b.tar.

myapp-dist.tar.gz
   libjava.jar
   sprintbootapp.jar
   3pp-a.tar
   3pp-b.tar.

The 3pp-a-module and the 3pp-b-module only contain some configuration files and startup scripts. No java or any compiled code. How to package them individually into tar files (no compression)?

How to define dependencies in dist-module to the other modules? Is it possible to get the other modules built when build is triggered from dist-module?

Update:

I setup my test project based on @marco-r's answer and it works except for packaging the war file. Checkout the test project from github https://github.com/KiranMohan/study-spring-boot.

This is the project setup of interest.

include ':sb-2.1-multi-package', ':sb-2.1-multi-package:hello-rest-lib', 
        ':sb-2.1-multi-package:hello-rest-standalone-jar',
        ':sb-2.1-multi-package:hello-rest-war'
include 'sb-2.1-3pp-resources'
include 'sb-2.1-build'

However adding hello-rest-war to sb-2.1-build.tar.gz fails.
Instead of war files, its the dependencies that are getting packaged.

dependencies {
    archivesDeps    project(path: ':sb-2.1-3pp-resources', configuration: 'archives')
    javaDeps project(":sb-2.1-multi-package:hello-rest-war")
}

...    
task copyJavaDeps(type: Copy) {
    inputs.files(configurations.javaDeps)
    from configurations.javaDeps
    into "${ARCHIVE_DIRECTORY}/lib"
}

...
// create distribution bundle
distributions {
    main {
        contents {
            from ARCHIVE_DIRECTORY
            into "/springapp/multimodule"
        }
    }
}

Contents of the package

springapp/multimodule/lib/classmate-1.4.0.jar
springapp/multimodule/lib/hello-rest-lib-0.0.1-SNAPSHOT.jar
springapp/multimodule/lib/hibernate-validator-6.0.16.Final.jar
...
springapp/multimodule/lib/tomcat-embed-websocket-9.0.17.jar
springapp/multimodule/lib/validation-api-2.0.1.Final.jar    
springapp/multimodule/sb-2.1-3pp-resources/config/3pp.json

How to package war file (hello-rest-war module) and without all the transitive dependencies?

Marco R. :

This is multiple question scenario, so I am going to address it in parts.

  1. Since all 3PP_X_module have the same building requirements create a build.gradle in each of the submodules that refer to an actual build gradle that have the common functionality required:
apply from: '../tarArtifact.gradle'
  1. In the parent folder create the previously referred tarArtifact.gradle to have the functionality to TAR the contents of a subfolder (arbitrarily chosen as contents) of a referring subproject:
apply plugin: 'base'

task tarContents(type: Tar) {
    from 'contents'
    archiveName = "${project.name}.tar"
    destinationDir file('build/tar')
}

artifacts {
    archives file: tarContents.archivePath, type: 'tar', builtBy: tarContents
}

Since the archives configuration is wired to the output of the tarContents (builtBy: tarContents), then the archives configuration can be used to retrieve the desired TAR as the output of building this project naturally.

  1. Create in dist-module the following build.gradle file:
apply plugin: 'distribution'

plugins.withType(DistributionPlugin) {
    distTar {
        compression = Compression.GZIP
        extension = 'tar.gz'
    }
}

configurations {
    wholeProjectDist
}

dependencies {
    wholeProjectDist project(path: ':3pp-a-module', configuration: 'archives')
    wholeProjectDist project(path: ':3pp-b-module', configuration: 'archives')
    wholeProjectDist project(':lib-java-module')
    wholeProjectDist project(':spring-boot-module')
}

distributions {
    main {
        contents {
            from configurations.wholeProjectDist
        }
    }
}

This gradle file includes the following:

  • Applies the Distribution plugin, so we can generate the final tar.gz file from the artifacts generated by all the other subprojects.
  • Configures the distTar task (of the DistributionPlugin plugin) to compress any generated TAR using it by using GZIP.
  • Creates the configuration wholeProjectDist to capture the dependencies of dist-module itself; which we will use with the distribution plugin's tasks.
  • Declares the dependencies of dist-module as the artifacts output by the siblings' subprojects; using the newly created wholeProjectDist.
  • Configures the distribution's plugin main configuration to have as contents all the files from configurations.wholeProjectDist
  1. Create a settings.gradle file under dist-module to allow it to access its siblings modules using includeFlat:
includeFlat '3pp-a-module', '3pp-b-module', 'lib-java-module', 'spring-boot-module'
  1. Include in the parent folder a settings.gradle file to include all children submodules (as the root project):
includeFlat '3pp-a-module', '3pp-b-module', 'lib-java-module', 'spring-boot-module'
  1. Build the desired tar.gz files by invoking the gradle command (from the root folder):
gradle :dist-module:distTar

Hope this helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=78942&siteId=1