自定义gradle插件原因:无法配置“发布”扩展

时间:2015-01-19 08:28:36

标签: gradle build.gradle

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

4 个答案:

答案 0 :(得分:37)

简单地替换:

publishing {
    publications {
      ...
  }
}

以下:

publishing.publications {
  ...
}

为我工作!

答案 1 :(得分:2)

Gradle对于插件的顺序很脆弱。

讨论了这个问题

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

  

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

     

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

答案 2 :(得分:2)

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

答案 3 :(得分:0)

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