Using a gradle plugin in a project where its dependency also use the same plugin

99封情书 提交于 2021-01-27 06:24:00

问题


I am new to gradle (leaving from maven). Now I have a problem. I have a gradle build where I want to use the com.bmuschko.nexus plugin. But my project depends on another project where I also want to use the com.bmuschko.nexus plugin.

So when I build I get an Exception:

Plugin 'com.bmuschko.nexus' is already on the script classpath. Plugins on the script classpath cannot be applied in the plugins {} block. Add  "apply plugin: 'com.bmuschko.nexus'" to the body of the script to use the plugin.

But when I do so -> Add apply plugin: 'com.bmuschko.nexus' to the body of the script to use the plugin I get another exception:

> Failed to apply plugin [id 'com.bmuschko.nexus']
   > Plugin with id 'com.bmuschko.nexus' not found.

How can I solve this?

settings.gradle

include ':config'
project(':config').projectDir = new File(settingsDir, '../zConfig')

build.gradle

plugins {
        // id "com.bmuschko.nexus" version "2.3" // already in classpath
        id "me.champeau.gradle.antlr4" version "0.1"
}

apply plugin: 'java'
apply plugin: 'maven'
//apply plugin: 'com.bmuschko.nexus'

dependencies {
        compile project(':config')
}

EDIT: to reproduce just clone the repo https://github.com/KIC/stackoverflow/tree/master/gradleproblem and try gradle tasks in the bar directory

EDIT2: Seems that I could solve the nexus upload problem by omitting the plugin and follow this answer https://stackoverflow.com/a/16766946/1298461

But since I have also an antlr project and a second antlr project extending the grammar of the first, I have the very same problem just with another plugin. I think I could solve this when I use a parent build.gradle and subprojects{}. But this is exactly the reason why I leave maven and move to gradle. My sub-modules can and should also live independently with different versions.


回答1:


'com.bmuschko.nexus' plugin is already on config project. That's the reason of the following error: Plugin 'com.bmuschko.nexus' is already on the script classpath. Plugins on the script classpath cannot be applied in the plugins {} block. Add "apply plugin: 'com.bmuschko.nexus'" to the body of the script to use the plugin.

I did the following:

Edit build.gradle (config project)

apply plugin: 'java'
apply plugin: 'maven'
apply from: '../repos.gradle'

Edit build.gradle (bar project)

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-nexus-plugin:2.3'
    }
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'com.bmuschko.nexus'
apply from: '../repos.gradle'

dependencies {
        compile project(':config')
}

Then I run gradle tasks and everything is ok.



来源:https://stackoverflow.com/questions/28741188/using-a-gradle-plugin-in-a-project-where-its-dependency-also-use-the-same-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!