# Task with one or more output files

**URL:** <https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998>\
**Category:** Help/Discuss\
**Created:** [October 9, 2018, 11:11pm UTC](https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998 "2018-10-09T23:11:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoUsername](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@NoUsername](https://discuss.gradle.org/u/NoUsername)\
**Post date:** [October 9, 2018, 11:11pm UTC](https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998/1 "2018-10-09T23:11:38Z")

</div>

I have a task that takes an input source file and has one or more output files. The very first time the task is executed, everything is fine. But if I do a clean, gradle doesn’t see that the output files are gone and the task needs to be rerun. The confusion is understandable since I’m using a wildcard to specify the output files and beyond that I don’t know the output file names in advance.

Here is a minimal example (in Kotlin):

task(“clean”) {  
delete(project.buildDir)  
}

task(“one2many”) {  
val source = “one.txt”  
val manyFiles = “${project.buildDir}/many/\*.txt”

```
inputs.file(source)
outputs.file(manyFiles)

commandLine("touch",
    "build/many/one-1.txt",
    "build/many/one-2.txt",
    "build/many/one-3.txt")

```

}

If I execute the clean command after one2many and then run one2many again, gradle says the task is up-to-date.

Is there a way to make this task run if there are no files that match “${project.buildDir}/many/\*.txt”?

Thanks!

---

<div class="post-metadata">

**Author:** ![TWiStErRob](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/twisterrob/32/7261_2.png) [@TWiStErRob](https://discuss.gradle.org/u/TWiStErRob)\
**Post date:** [October 9, 2018, 11:41pm UTC](https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998/2 "2018-10-09T23:41:47Z")

</div>

Try using `outputs.dir(project.buildDir.resolve("many"))` to specify that your output is every file in that directory. Not sure if this helps, but it may be a step in the right direction.

---

<div class="post-metadata">

**Author:** ![NoUsername](https://avatars.discourse-cdn.com/v4/letter/n/b9bd4f/32.png) [@NoUsername](https://discuss.gradle.org/u/NoUsername)\
**Post date:** [October 10, 2018, 12:56am UTC](https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998/3 "2018-10-10T00:56:19Z")

</div>

Thanks, Róbert! That does work. Although I don’t understand what role resolve is playing. I’m going to tinker with this some more.

It would also be nice to be able to specify a pattern for specific files in the directory and not just all files in the directory. But that’s a nice-to-have, not a requirement.

---

<div class="post-metadata">

**Author:** ![TWiStErRob](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/twisterrob/32/7261_2.png) [@TWiStErRob](https://discuss.gradle.org/u/TWiStErRob)\
**Post date:** [October 10, 2018, 10:43am UTC](https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998/4 "2018-10-10T10:43:20Z")

</div>

Note: I"m just a user like you.

`File.resolve("foo")` extension function in Kotlin is a nicer way of doing `"${file}/foo"`, more typed and safe. For example if `file` is null `resolve` will throw NPE, but interpolation will give you `null/foo` and probably create a folder named `null` later on instead of failing. `buildDir` is non-null in this particular case, but in general it’s a better pattern I found.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [October 10, 2018, 11:13pm UTC](https://discuss.gradle.org/t/task-with-one-or-more-output-files/28998/5 "2018-10-10T23:13:28Z")

</div>

You could do

```gradle
outputs.files fileTree("${project.buildDir}/many").matching {
   include '*.txt'
} 

```
