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

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 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" );
    }
}