# doFirst() does not execute first, but only after task execution

**URL:** <https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129>\
**Category:** Help/Discuss\
**Created:** [August 13, 2018, 1:15pm UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129 "2018-08-13T13:15:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaimg](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jaimg](https://discuss.gradle.org/u/jaimg)\
**Post date:** [August 13, 2018, 1:15pm UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/1 "2018-08-13T13:15:34Z")

</div>

I am in the process of converting a plugin from Groovy to Java.  
I have a Groovy task:

```
task dofirsttest(type: Exec) {
    doLast{
        println "Should be last"
    }
    doFirst{
        println "Should be first"
    }
    commandLine "echo", "Should be between"
}

```

that outputs

```
Should be first
Should be between
Should be last

```

I converted the task to Java:

```
class TestTask extends Exec {

    public TestTask() {
        
        doLast(new Action<Object>(){
            @Override
            public void execute(Object task) {
                System.out.println("Should be last ");
            }
        });

        doFirst(new Action<Object>(){
            @Override
            public void execute(Object task) {
                System.out.println("Should be first ");
            }
        });

        commandLine("echo", "Should be between");
    }
}

```

but this outputs

```
Should be between
Should be first
Should be last

```

The doFirst() gets executed only after task execution. Why does the Java task behave differently?  
Thanks!

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [August 13, 2018, 2:28pm UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/2 "2018-08-13T14:28:19Z")

</div>

This used to be a reported issue, but it didn’t survive the move to github issues.  
[https://issues.gradle.org/browse/GRADLE-2064](https://issues.gradle.org/browse/GRADLE-2064)

---

<div class="post-metadata">

**Author:** ![jaimg](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jaimg](https://discuss.gradle.org/u/jaimg)\
**Post date:** [August 14, 2018, 6:29am UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/3 "2018-08-14T06:29:53Z")

</div>

Thanks for pointing this out. What is the recommended way to implement doFirst actions in subclasses? The proposed workaround seems very unintuitive to me.

---

<div class="post-metadata">

**Author:** ![jaimg](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jaimg](https://discuss.gradle.org/u/jaimg)\
**Post date:** [August 14, 2018, 6:54am UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/4 "2018-08-14T06:54:04Z")

</div>

I just tried the workaround and it doesn’t seem to work anymore. Unless I did something wrong of course.

```
class TestTask extends Exec {

    public TestTask() {

        Task currentTask = this;
        TaskCollection<Task> workaround= getProject().getTasks().matching(new Spec<Task>() {
            @Override
            public boolean isSatisfiedBy(Task t){
                System.out.println(t.toString() + " == " + currentTask.toString()); //current task does not appear here
                return t == currentTask;
            }
        });
        System.out.println(workaround.size()); // is 0
        for(Task t : workaround) {
            t.doFirst(new Action<Object>(){
                @Override
                public void execute(Object task) {
                    System.out.println("Should be first ");
                }
            });
        }
        commandLine("echo", "Should be between");
    }
}

```

I cannot get the currently constructing task from `getTasks()`, because it is not yet in the list of tasks.

---

<div class="post-metadata">

**Author:** ![glward](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/glward/32/5868_2.png) [@glward](https://discuss.gradle.org/u/glward)\
**Post date:** [August 14, 2018, 12:01pm UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/5 "2018-08-14T12:01:03Z")

</div>

Something like this might work:

```
class TestTask extends Exec {
    public TestTask() {
        commandLine("echo", "Should be between");
    }

    @Override
    @TaskAction
    protected void exec() {
        System.out.println("First");
        super.exec();
        System.out.println("Last");
    }
}
```

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [August 17, 2018, 5:29am UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/6 "2018-08-17T05:29:32Z")

</div>

If your new task type does not need to expose the `Exec` API to the users of the task, it may be better to use composition rather than inheritance. [`Project.exec`](https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#exec-org.gradle.api.Action-) can be used to execute something with the same type of configuration as an `Exec` task.

```
public class TestTask extends DefaultTask {
    @TaskAction
    public void doWork() {
        System.out.println( "First" );
        getProject().exec( new Action<ExecSpec>() {
            @Override
            public void execute( ExecSpec execSpec ) {
                execSpec.commandLine( "echo", "Should be between" );
            }
        } );
        System.out.println( "Last" );
    }
}
```

---

<div class="post-metadata">

**Author:** ![jaimg](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jaimg](https://discuss.gradle.org/u/jaimg)\
**Post date:** [August 17, 2018, 6:22am UTC](https://discuss.gradle.org/t/dofirst-does-not-execute-first-but-only-after-task-execution/28129/7 "2018-08-17T06:22:39Z")

</div>

Thank you for your replies. My original intent was to make sure a directory existed before executing a command in it. So far I solved it by splitting it into two tasks and making the Exec task depend on the dir creating one, but I may switch it back to use only one task.
