【问题标题】:custom gradle plugin causes: Cannot configure the 'publishing' extension自定义 gradle 插件原因:无法配置“发布”扩展
【发布时间】:2017-06-21 01:03:56
【问题描述】:

我有一个自定义 gradle 插件,其中添加了自定义任务类型和 gradle 配置。当我在maven-publish 之前应用这个插件时,它工作得非常好。但是当我在apply plugin: 'maven-publish' 之后添加它时,它会失败并显示错误消息:

FAILURE: Build failed with an exception.

* Where:
Build file '/scratch/skgupta/git_storage/emdi/integtest/build.gradle' line: 38

* What went wrong:
A problem occurred evaluating root project 'integtest'.
> Cannot configure the 'publishing' extension after it has been accessed.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 7.6 secs

这是build.gradle 文件。

buildscript {
    repositories {
        maven {
            url = "${artifactory_contextUrl}/repo"
        }
    }

    dependencies {
        classpath group: 'com.mycomp.proj', name: 'MyCustomPlugin', version: "${pluginVersion}", transitive: true
    }   
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'MyCustomPlugin'  // WHen this line is moved above maven-publish, build works fine.

// add jar name and version
jar.archiveName='lrgemaas_small_deploy_3n.jar'
group = 'com.mycom.proj.test'
version = '1.0.3'

dependencies {
    compile group: 'eaas.platform', name: 'registry-lookup-client', version: '0.1'
    compile group: 'eaas.platform', name: 'registry-client', version: '0.1'
    compile configurations.lrgConfig  // This configuration is added by MyCustomPlugin
    compile configurations.webdriver  // This configuration is added by MyCustomPlugin
}

repositories {
    maven {
        url = "${artifactory_contextUrl}/repo"
    }
}

publishing {
    publications {
        myPublicationName(MavenPublication) {
            artifactId 'lrgemaas_small_deploy_3n'
            artifact jar.archivePath
        }
    }
    repositories {
        maven {
            url = "${artifactory_contextUrl}/${artifactory_repoName}"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }
    }
}

// workflow for build 
defaultTasks 'clean','build','publish'

PS:我尝试在自定义插件中什么都不做(即,简单地从 apply 方法返回),但它仍然给出同样的错误。

【问题讨论】:

标签: gradle build.gradle


【解决方案1】:

简单替换:

publishing {
    publications {
      ...
  }
}

以下内容:

publishing.publications {
  ...
}

为我工作!

【讨论】:

  • 我们切换到 Android Studio 2.1 并开始遇到这个问题,将 volley 添加为子模块。这个解决方案对我们有用。
  • 奇怪...这也为我解决了这个问题。其他人建议交换插件声明顺序,但这对我的情况没有帮助。使用过 gradlew 2.13
【解决方案2】:

Gradle 在插件顺序方面很脆弱。在

上有关于这个确切问题的讨论

https://discuss.gradle.org/t/apply-maven-publish-plugin-from-init-script/2460

“很酷,这就是发生的事情。

支持“发布 {}”块的 DefaultPublishingExtension 是 DeferredConfigurable。这是一种允许注册配置块以在访问扩展时执行的机制。不幸的是,如果您在不知情的情况下访问此类扩展程序,有时会导致意外行为。例如,当您尝试获取项目的所有属性时就是这种情况(就像发布插件在这里所做的那样:https://github.com/townsfolk/gradle-release/blob/master/src/main/groovy/release/PluginHelper.groovy#L230"

【讨论】:

    【解决方案3】:

    仅供参考,我使用动态版本,发现我必须在应用插件之前定义版本。可能是因为 java 或 maven-publish 在应用时设置了发布细节。

    【讨论】:

      【解决方案4】:

      我将 gradle 版本从 2.1 升级到 2.12,它解决了这个问题,fwiw。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2021-06-05
      • 2016-01-07
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多