Using ToString annotation on custom task in a plugin fails with NPE in gradle 1.7

(Steinar Haugen) #1

After upgrading from gradle 1.6 to 1.7, a custom plugin we’re using in our build started failing with a NullPointerException.

It turned out to happen because we were using the groovy ToString annotation on the class.


import groovy.transform.ToString
import org.gradle.api.DefaultTask
class FailingTask extends DefaultTask {
    String myProp="hello"

And a test for this task:

import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
  class TaskSpec extends Specification {
    @Rule public TemporaryFolder folder = new TemporaryFolder();
      def "can add task with ToString annotation to project"() {
        ProjectBuilder builder = ProjectBuilder.builder()
        Project project = builder.withProjectDir(folder.newFolder()).build()
        Task task = project.task('task1', type: FailingTask)
        task instanceof FailingTask

Fails with a NullPointerException. If the ToString annotation is replaced with an explicit toString implementation, then everything works as expected.