# Customise distZip to change root directory name

**URL:** <https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622>\
**Category:** Help/Discuss\
**Created:** [May 17, 2016, 1:34pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622 "2016-05-17T13:34:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![traggatt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/traggatt/32/4861_2.png) [@traggatt](https://discuss.gradle.org/u/traggatt)\
**Post date:** [May 17, 2016, 1:34pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/1 "2016-05-17T13:34:04Z")

</div>

I’m using distZip to produce a zipped distribution of my project. The current task creates a zip file with a single directory at the root called `MyProject-version`. Is there a way to change this to `MyProject`? (i.e. lose the trailing version number)

Thanks

---

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [May 17, 2016, 5:03pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/2 "2016-05-17T17:03:52Z")

</div>

See [https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html)

```groovy
archiveName = '[baseName].[extension]'

```

---

<div class="post-metadata">

**Author:** ![traggatt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/traggatt/32/4861_2.png) [@traggatt](https://discuss.gradle.org/u/traggatt)\
**Post date:** [May 17, 2016, 5:06pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/3 "2016-05-17T17:06:16Z")

</div>

Thanks for your reply.

Changing `archiveName` appears to set both the zipfile’s name and the internal root directory. I was hoping to keep the zipfile versioned and the internal root directory not. Is this possible?

---

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [May 17, 2016, 5:24pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/4 "2016-05-17T17:24:43Z")

</div>

Sorry, I misunderstood you. I assume you using `distZip` from `application` or `distribution` plugins?

That might be a bit harder due to the way the `copySpec` is created when the plugin is applied. I have not tested the following, but maybe that works for you.

```groovy
into('MyProject') {
  distributions.main.contents
}

```

---

<div class="post-metadata">

**Author:** ![amake](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/amake/32/5097_2.png) [@amake](https://discuss.gradle.org/u/amake)\
**Post date:** [May 18, 2016, 8:01am UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/5 "2016-05-18T08:01:57Z")

</div>

A simple workaround would be to set the `archiveName` to be what you want the internal root to be named, and then change the archive file name in a `doLast` at the end.

---

<div class="post-metadata">

**Author:** ![traggatt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/traggatt/32/4861_2.png) [@traggatt](https://discuss.gradle.org/u/traggatt)\
**Post date:** [May 26, 2016, 9:48am UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/6 "2016-05-26T09:48:23Z")

</div>

Thanks, I took the approach of renaming the archive in a `doLast` like so:

```
distZip {
  //The target zipfile name should be versioned but 
  //internal root directory not. By default the distZip
  //task uses the same property (archiveName) for
  //both. To workaround, suppress the version in
  //the archiveName property (by setting version = null)
  //then, once the task is complete, we rename the
  //zip as a post-processing step to add the version
  //back on.

  //note that archivePath is a derived property.
  //its return value is sensitive to the version
  //property (among others).

  def versionedPath = archivePath
  version = null
  doLast {
    archivePath.renameTo(versionedPath)
  }
}

```

I’m not a huge fan of this approach since it relies on mutating external variables to infer the archive name, but it seems to be the simplest/most generic. It would be nicer to have a way of just configuring these values once.

---

<div class="post-metadata">

**Author:** ![Schalk\_Cronje](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/schalk_cronje/32/6511_2.png) [@Schalk\_Cronje](https://discuss.gradle.org/u/Schalk_Cronje)\
**Post date:** [May 26, 2016, 8:07pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/7 "2016-05-26T20:07:38Z")

</div>

Does that not cause your task to always be out of date?

---

<div class="post-metadata">

**Author:** ![eric-creekside](https://avatars.discourse-cdn.com/v4/letter/e/7993a0/32.png) [@eric-creekside](https://discuss.gradle.org/u/eric-creekside)\
**Post date:** [February 18, 2025, 5:48pm UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/8 "2025-02-18T17:48:10Z")

</div>

Is there a up-to-date solution for this for Gradle 8.x? I’ve tried this and several other suggestions I found but they all fail with various Gradle errors about unknown properties.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [February 19, 2025, 12:05am UTC](https://discuss.gradle.org/t/customise-distzip-to-change-root-directory-name/17622/9 "2025-02-19T00:05:42Z")

</div>

The proper way to achieve this is to properly translate the paths during artifact creation back then as well as today.

Back then in 2016 when this thread was created it would have been for example this in Groovy DSL:

```groovy
tasks.distZip {
    eachFile {
        relativePath = new RelativePath(
            relativePath.isFile(),
            baseName,
            *relativePath.segments.drop(1)
        )
    }
}

```

Today and with Kotlin DSL it would for example be this:

```kotlin
tasks.distZip {
    eachFile {
        relativePath = RelativePath(
            relativePath.isFile,
            archiveBaseName.get(),
            *relativePath.segments.drop(1).toTypedArray()
        )
    }
}

```
