When implementing a Task in Java, how do I push behavior down to the execution phase?

I’m writing a custom extension to the Zip task that creates some additional files (similar to the Jar task). Because I want to maintain backwards compatibility with Gradle 1.12, I am implementing the task in Java.

However, I notice it’s being executed twice and I assume that is done due to configuration/execution steps.

If I was writing the plugin in Groovy, I would just stick that portion of the task inside a doLast{} or doFirst{} block, but that’s not an option. How can I detect which phase is taking place from within Java?

    postMainSpec.addChild().from(new Callable<FileTreeAdapter>() {
        public FileTreeAdapter call() throws Exception {
            MapFileTree m = new MapFileTree(getTemporaryDirFactory(), getFileSystem());

            gridlibSource.add(INDEX_FILE, new MethodClosure(new Action<OutputStream>() {
                public void execute(OutputStream outputStream) {
                    PrintWriter pw = new PrintWriter(outputStream);
                    if(r != null) {
                        for(String ra : r) { 
                           pw.println(ra);
                        }
                    }
                    pw.flush();
                }
            }, "execute"));
            
            return new FileTreeAdapter(m);
        }
    });

So the issue is that r is being generated by the main spec as files are copied in, and I need to execute it afterward. I add it to the rootSpec at the end, but it gets executed beforehand (I’m assuming as part of configuration step).

Ignore wrapping of Action with MethodClosure, purely for backwards compatibility with 1.12.