Adding task of type Jar via a custom plugin

I have a custom plugin contained in a standalone project, and am successfully building it and using it in other projects.

I am currently trying to extend the plugin to add a task that will publish sources.

The (working) code in the target build script that I am trying to pull into the plugin:

task sourcesJar(type: Jar) {
        classifier = 'sources'
        from sourceSets.main.allJava
    }

I have tried several ideas, but I continually receive the following error when running the unit test:

groovy.lang.MissingPropertyException: Could not find property 'Jar' on project ':testSubProject'.

Two examples of plugin code that gives this error (I’ve snipped out most of the plugin code)::

project.subprojects {
            apply plugin: 'java'
              task sourcesJar(type: Jar) {
                classifier = 'sources'
                from sourceSets.main.allJava
            }
        }

and

project.subprojects {
            apply plugin: 'java'
              task(['type': Jar], 'sourcesJar') {
                classifier = 'sources'
                from sourceSets.main.allJava
            }
        }

Any thoughts?

You’ll need to import the Jar class is your plugin class. The reason you don’t need to do this in the build script is that Jar is one of the many classes imported by default.

That worked perfectly; thanks!