I have two plugins that I've created for work where one plugin applies the
other. Previous to the M5 release, I had these plugins install themselves
in the gradle.home/lib/plugins directory. This worked like a charm. I'm now
trying to update them to work with M5 by using "apply from" scripts. This
is causing issues with the way we currently use the plugins, so I'm trying
to figure out if there's a way we can fix it in our build.gradle files, or
if this should work as is.

Here's an example of what I'm talking about.

Our Corporate (Parent) plugin:
[code]
class ParentPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task("parentPlugin") << {
            println "This is from the 'parent' plugin: ParentPlugin"
        }
    }
}
[/code]

Our API plugin:
[code]
class SubPlugin implements Plugin<Project> {
    void apply(Project project) {
        // This fails when using "apply from:" script
        //project.apply(plugin: 'corp-plugin')

        // This works as long as ParentPlugin hasn't already been applied
        project.apply(plugin: ParentPlugin)
        project.task('subPlugin') << {
            println "This is from the 'sub' plugin: SubPlugin"
        }
    }
}
[/code]

The problem arises from our common project setup. The Root project will
apply our "corporate" plugin to all sub projects, and the sub projects will
apply the "sub" plugin that fits their project type.

Gradle build file from the root project:
[code]
apply plugin: 'groovy'
apply plugin: 'idea'

group = 'com.example'

dependencies {
   groovy localGroovy()
}

subprojects {
    apply plugin: 'idea'
    apply from: '../../ParentPlugin/installation/apply.groovy'
}
[/code]

Gradle build file from the sub project:
[code]
apply from: '../../SubPlugin/installation/apply-sub.groovy'

group = 'com.example'

dependencies {
   groovy localGroovy()
}
[/code]

This causes the following error when trying to run gradle from the root, or
sub projects.
[quote]
* What went wrong:
A problem occurred evaluating script.
Cause: Cannot add task ':sub:parentPlugin' as a task with that name already
exists.
[/quote]

This fails in both M3, and M5.

I've gotten it to work by adding an if check in the SubPlugin which checks
to see if the ParentPlugin is already applied, but this seems really cloogy
considering I don't have to do this when it's installed in the
gradle.home/lib/plugins directory for M3.
[code src="SubPlugin.apply"]
        if (!project.plugins.findPlugin(ParentPlugin)) {
            println("ParentPlugin not installed, applying it")
            project.apply(plugin: ParentPlugin)
        } else {
            println("ParentPlugin is already installed")
        }
[/code]

Any help here would be greatly appreciated. Our employees absolutely cannot
upgrade to M5 until I can get the plugins working with it.

Thanks,
Eric

-- 
Learn from the past. Live in the present. Plan for the future.
Blog: http://eric-berry.blogspot.com
jEdit <http://www.jedit.org> - Programmer's Text Editor
Bazaar <http://bazaar.canonical.com> - Version Control for Humans

Reply via email to