[GRADLE-736] Directory task doesn't allow creation of build/gwt directory Created: 10/Nov/09  Updated: 04/Jan/13  Resolved: 24/Nov/10

Status: Resolved
Project: Gradle
Affects Version/s: 0.8
Fix Version/s: 0.9-rc-1

Type: Bug
Reporter: Kirk Rasmussen Assignee: Hans Dockter
Resolution: Fixed Votes: 0


 Description   

I was attempting to create a new gwtCompile task (hopefully I can contribute a GWT plugin in the future) which I wanted to create a subdirectory inside of the normal build directory to allow easy cleanup with the clean task. However the following results in an exception:

gwtBuildDir = dir("$buildDirName/gwt")

task gwtCompile(dependsOn: gwtBuildDir) << {
...
}

Caused by: org.gradle.api.InvalidUserDataException: Cannot add directory task 'build' as a non-directory task with this name already exists.
at org.gradle.api.internal.project.AbstractProject.dir(AbstractProject.java:764)

Digging deeper it looks like this was not allowed because there is already a non-directory "build" task in the project. I'm not sure what this code is protecting against but it seems like this is a bad idea as it blocks creation of directories in a somewhat random non-deterministic way (each path element is checked against existing tasks). The chances for name collisions seems high.

public Directory dir(String path) {
String[] pathElements = path.split("/");
String name = "";
Directory dirTask = null;
for (String pathElement : pathElements) {
name += name.length() != 0 ? "/" + pathElement : pathElement;
Task task = taskContainer.findByName(name);
if (task instanceof Directory)

{ dirTask = (Directory) task; }

else if (task != null)

{ throw new InvalidUserDataException(String.format("Cannot add directory task '%s' as a non-directory task with this name already exists.", name)); }

else

{ dirTask = taskContainer.add(name, Directory.class); }

}
return dirTask;
}



 Comments   
Comment by Hans Dockter [ 16/Nov/09 ]

I'm not sure about the future of directory tasks in Gradle and if they make it into 1.0. The user's guide should be clearer about that. There are other ways to achieve what you want to achieve. For the typical use case that a task wants to create output into a directory, you can use annotations.

public GwtCompile extends DefaultTask {
   private File outputDir
   
   @OutputDirectory
   public File getOutputDir() { ...
}

Gradle will now automatically create the directory when the task is executed. Additionally (in Gradle trunk), Gradle will check if the content of the output directories has changed sinced the last execution. Gradle does the same for properties annotated as InputDirectories/Files/.... If no content has changed the task is not executed. This is what we call incremental build.

Generated at Wed Jun 30 11:37:32 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.