Search in sources :

Example 1 with TaskInstantiationException

use of org.gradle.api.tasks.TaskInstantiationException in project gradle by gradle.

the class TaskFactory method create.

@Override
public <S extends Task> S create(String name, final Class<S> type, final Object... args) {
    if (!Task.class.isAssignableFrom(type)) {
        throw new InvalidUserDataException(String.format("Cannot create task of type '%s' as it does not implement the Task interface.", type.getSimpleName()));
    }
    NameValidator.validate(name, "task name", "");
    final Class<? extends Task> generatedType;
    if (type.isAssignableFrom(DefaultTask.class)) {
        generatedType = generator.generate(DefaultTask.class);
    } else {
        generatedType = generator.generate(type);
    }
    return type.cast(AbstractTask.injectIntoNewInstance(project, name, type, new Callable<Task>() {

        public Task call() throws Exception {
            try {
                if (args != null) {
                    return instantiator.newInstance(generatedType, args);
                }
                return instantiator.newInstance(generatedType);
            } catch (ObjectInstantiationException e) {
                throw new TaskInstantiationException(String.format("Could not create task of type '%s'.", type.getSimpleName()), e.getCause());
            }
        }
    }));
}
Also used : Task(org.gradle.api.Task) AbstractTask(org.gradle.api.internal.AbstractTask) DefaultTask(org.gradle.api.DefaultTask) ObjectInstantiationException(org.gradle.api.reflect.ObjectInstantiationException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) DefaultTask(org.gradle.api.DefaultTask) Callable(java.util.concurrent.Callable) TaskInstantiationException(org.gradle.api.tasks.TaskInstantiationException)

Example 2 with TaskInstantiationException

use of org.gradle.api.tasks.TaskInstantiationException in project gradle by gradle.

the class IncludedBuildTaskReferenceResolver method constructTask.

@Override
public Task constructTask(final TaskReference reference, TaskContainer tasks) {
    if (!(reference instanceof IncludedBuildTaskReference)) {
        return null;
    }
    final IncludedBuildTaskReference ref = (IncludedBuildTaskReference) reference;
    final BuildIdentifier sourceBuild = buildIdentity.getCurrentBuild();
    final BuildIdentifier targetBuild = new DefaultBuildIdentifier(ref.getBuildName());
    includedBuilds.addTask(sourceBuild, targetBuild, ref.getTaskPath());
    String delegateTaskName = ref.getName();
    Task task = tasks.findByName(delegateTaskName);
    if (task == null) {
        return tasks.create(delegateTaskName, CompositeBuildTaskDelegate.class, new Action<CompositeBuildTaskDelegate>() {

            @Override
            public void execute(CompositeBuildTaskDelegate compositeBuildTaskDelegate) {
                compositeBuildTaskDelegate.setBuild(targetBuild);
                compositeBuildTaskDelegate.setTaskPath(ref.getTaskPath());
            }
        });
    }
    if (task instanceof CompositeBuildTaskDelegate) {
        return task;
    }
    throw new TaskInstantiationException("Cannot create delegating task '" + delegateTaskName + "' as task with same name already exists.");
}
Also used : Task(org.gradle.api.Task) DefaultBuildIdentifier(org.gradle.api.internal.artifacts.DefaultBuildIdentifier) BuildIdentifier(org.gradle.api.artifacts.component.BuildIdentifier) DefaultBuildIdentifier(org.gradle.api.internal.artifacts.DefaultBuildIdentifier) TaskInstantiationException(org.gradle.api.tasks.TaskInstantiationException)

Aggregations

Task (org.gradle.api.Task)2 TaskInstantiationException (org.gradle.api.tasks.TaskInstantiationException)2 Callable (java.util.concurrent.Callable)1 DefaultTask (org.gradle.api.DefaultTask)1 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 BuildIdentifier (org.gradle.api.artifacts.component.BuildIdentifier)1 AbstractTask (org.gradle.api.internal.AbstractTask)1 DefaultBuildIdentifier (org.gradle.api.internal.artifacts.DefaultBuildIdentifier)1 ObjectInstantiationException (org.gradle.api.reflect.ObjectInstantiationException)1