DEV Community

Query Filter
Query Filter

Posted on

exec-9

How to Trigger Each Option

Option 1: Using the distribution Plugin

Applying id 'distribution' (or id 'application') automatically hooks tasks into your project lifecycle.

  • Triggering explicitly:
  • ./gradlew distTar (generates only the .tar / .tar.gz)
  • ./gradlew distZip (generates only the .zip)
  • ./gradlew assembleDist (generates both zip and tar)

  • Does ./gradlew build create it by default?

  • Yes, indirectly. ./gradlew build triggers assemble, which depends on assembleDist. This will automatically generate both a .zip and a .tar under build/distributions/.

  • Note: If you only want a TAR and want to disable the ZIP output during standard builds, you can add this to your build.gradle:

tasks.named('distZip') { enabled = false }

Enter fullscreen mode Exit fullscreen mode

Option 2: Standalone `tasks.register('buildAppTar', Tar)`

Because this is a custom-registered task, Gradle treats it as an opt-in task.

  • Triggering explicitly:
  • ./gradlew buildAppTar

  • Does ./gradlew build create it by default?

  • No. Custom tasks do not run during standard ./gradlew build execution unless you explicitly wire them into the build lifecycle:

// Wire it so ./gradlew build or ./gradlew assemble triggers it
tasks.named('assemble') {
    dependsOn tasks.named('buildAppTar')
}

Enter fullscreen mode Exit fullscreen mode

Which Option Should You Choose?

Feature / Aspect Option 1: distribution Plugin Option 2: Standalone Tar Task
Setup Complexity Very Low (Idiomatic Gradle) Low to Medium
Convention Support Auto-includes ./src/main/dist content You must manually define every path
Publishing Integration Built-in integration with maven-publish (components.java or distTar artifact) Requires manually attaching the output file to publishing
Granular Control Opinionated defaults Full control over compression, permissions, and paths

Recommendation

  • Choose Option 1 (distribution plugin) if your project is a standard Java/Kotlin application or microservice. It is the Gradle standard, simplifies publishing configurations, and handles file permission metadata (0755 for executables vs. 0644 for configs) out of the box.
  • Choose Option 2 (Custom Tar task) if you have strict, non-standard directory packaging requirements that conflict with Gradle's conventions, or if you want absolute control over when and how the archive is assembled.

For a walkthrough on creating zip/tar archives and managing tasks, see the Gradle Distribution Plugin tutorial.

Top comments (0)