自定义 gradle 插件原因:无法配置 'publishing' 扩展

custom gradle plugin causes: Cannot configure the 'publishing' extension

我有一个自定义 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 方法 return),但它仍然给出相同的错误。

Gradle 关于插件的顺序是脆弱的。在

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

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

"Cool, so this is what happens.

DefaultPublishingExtension which is backing the 'publishing {}' block is a DeferredConfigurable. It's a mechanism that allows to register a configuration block to be executed when an extension is accessed. Unfortunately sometimes it causes unexpected behaviour if you access this kind of extension unawares. This is exactly the case when for example you try to get all the properties of a project (as the release plugin does here: https://github.com/townsfolk/gradle-release/blob/master/src/main/groovy/release/PluginHelper.groovy#L230"

只需替换:

publishing {
    publications {
      ...
  }
}

以下内容:

publishing.publications {
  ...
}

对我有用!

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

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