# How do I publish an artifact when I don't know the name until after it has built?

**URL:** <https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086>\
**Category:** Old Forum Archive\
**Created:** [January 15, 2013, 5:54pm UTC](https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086 "2013-01-15T17:54:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Shone](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Adam\_Shone](https://discuss.gradle.org/u/Adam_Shone)\
**Post date:** [January 15, 2013, 5:54pm UTC](https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086/1 "2013-01-15T17:54:00Z")

</div>

Hi,

I have a Java project where the build process goes something like this:

- compile and run tests as usual \* call an external Java program that packages the class files and some other stuff into a zip with a specific structure (not a normal jar)

The external Java program (which is an executable jar called toolkit.jar) produces zips with a version number and timestamp in the file name, something like “MyBlade-1.0.0-20130115-174704.zip”. I won’t know the name of the file that it produces until after the program has run.

This is what I have in my build script:

```gradle
task exportBlade(type: Exec, dependsOn: test ) {
         executable "java"
     args '-jar'
     args "${project.rootDir}/build/toolkit.jar"
     args 'export'
     args "${project.projectDir}"
     args '-inputFiles'
     args sourceSets.main.output.classesDir
     args '-outputDir'
     args "${buildDir}/distributions"
   }
    }
       artifacts {
   bladeZip fileTree("${buildDir}/distributions").files.toArray()[0]
  }
      build.dependsOn exportBlade

```

The idea is that when the exportBlade task runs it will call the Java program (toolkit.jar) and output the zip into the ${buildDir}/distributions directory. I don’t know what filename that zip will have so in my artifacts section I use a fileTree to just grab the first file in that directory. This relies on not having more than one file in the ${buildDir}/distributions directory, but that is taken care of elsewhere.

The problem is that Gradle evaluates my fileTree when it compiles the build script, at which point the ${buildDir}/distributions directory is empty. So it fails with this error:

```gradle
FAILURE: Build failed with an exception.
  * Where:
Build file 'D:\Development\Motifs\fxmotif\server\build.gradle' line: 60
  * What went wrong:
A problem occurred evaluating project ':server'.
> 0

```

How can I define my artifact to be a file that is generated by a task that runs during the build when that file has a dynamic name that I don’t know at compile time?

Cheers

---

<div class="post-metadata">

**Author:** ![Adam\_Shone](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Adam\_Shone](https://discuss.gradle.org/u/Adam_Shone)\
**Post date:** [January 15, 2013, 6:06pm UTC](https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086/2 "2013-01-15T18:06:00Z")

</div>

This looks related so I’ll experiment with it:

[http://forums.gradle.org/gradle/topics/how\_do\_i\_publish\_an\_artifact\_with\_a\_configurable\_file\_name\_from\_a\_custom\_task\_that\_is\_not\_an\_abstractarchivetask](http://forums.gradle.org/gradle/topics/how_do_i_publish_an_artifact_with_a_configurable_file_name_from_a_custom_task_that_is_not_an_abstractarchivetask)

---

<div class="post-metadata">

**Author:** ![daz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daz/32/2475_2.png) [@daz](https://discuss.gradle.org/u/daz)\
**Post date:** [January 15, 2013, 7:16pm UTC](https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086/3 "2013-01-15T19:16:00Z")

</div>

It might be as simple as:

```gradle
task exportBlade(type: Exec, dependsOn: test ) {
    executable "java"
    args '-jar'
    ...
      doLast {
        project.artifacts {
            bladeZip fileTree("${buildDir}/distributions").files.toArray()[0]
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Adam\_Shone](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Adam\_Shone](https://discuss.gradle.org/u/Adam_Shone)\
**Post date:** [January 16, 2013, 12:29am UTC](https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086/4 "2013-01-16T00:29:00Z")

</div>

Worked like a charm. Thanks Daz.

---

<div class="post-metadata">

**Author:** ![Adam\_Shone](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@Adam\_Shone](https://discuss.gradle.org/u/Adam_Shone)\
**Post date:** [January 17, 2013, 11:10pm UTC](https://discuss.gradle.org/t/how-do-i-publish-an-artifact-when-i-dont-know-the-name-until-after-it-has-built/6086/5 "2013-01-17T23:10:00Z")

</div>

Actually there is a bit of a problem with this. The project that builds these zips is actually a sub-project of a parent project. The parent project has a task that is supposed to gather all of these zips (there are multiple sub-projects) and deploy them to a location.

The relevant bits of the parent build looks like this:

```gradle
configurations {
   getBladeZips
}
  dependencies {
   getBladeZips project(path: "mySubProject", configuration: "bladeZip")
}
  task gatherAndDeployBladeZips(dependsOn: configurations.getBladeZips) << {
   println "Deploying blade zips from sub-projects"
}

```

Unfortunately the gatherAndDeployBladeZips runs first, before running the sub-project build task which produces the zip. I think it’s because the sub-projects are now declaring their artifacts at run-time, so when this task is compiled it doesn’t think there are any artifacts in the getBladeZips configuration so it doesn’t need to do anything.

It works fine if I change the task definition to this:

```gradle
task gatherAndDeployBladeZips(dependsOn: "mySubProject:build") << {

```

But that isn’t great because I now have to list my sub-projects twice, once in the dependencies section to add them to the getBladeZips configuration and then I have to list them again in the dependencies for the gatherAndDeployBladeZips task.

Is there a better way to get around this problem?
