thanks for your reply. This does not work in the configuration phase. It works in the execution phase and in the taskGraph.whenReady closure.
My task is to add projects in settings.gradle if a task is specified. Or to add the test-jar if the test task is present.