9

I have a custom gradle plugin in which custom task types and gradle configurations are added. When I apply this plugin before maven-publish it works perfectly fine. But when I add it after apply plugin: 'maven-publish', it fails with error message :

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

Here is the build.gradle file.

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: I tried doing nothing in the custom plugin (i.e., simply return from apply method), but it still gives same error.

kdabir
  • 9,623
  • 3
  • 43
  • 45
Sundeep Gupta
  • 1,887
  • 3
  • 24
  • 35
  • this looks like related to this: http://stackoverflow.com/questions/21190230/how-can-plugin-programmatically-configure-maven-publish-publishing-and-allow-bui – The End Jan 20 '15 at 16:17

4 Answers4

39

Simply replacing:

publishing {
    publications {
      ...
  }
}

with following:

publishing.publications {
  ...
}

worked for me!

Vinay
  • 1,859
  • 22
  • 26
  • 1
    We switched to Android Studio 2.1 and started having this problem with volley added as a submodule. This solution worked for us. – Jorge Garcia May 04 '16 at 23:54
  • 3
    Odd... this fixed it for me too. Others suggesting swapping plugin declaration order, but that did not help my case. Experienced with gradlew 2.13 – javajon May 06 '16 at 05:23
2

Gradle is brittle regarding the order of the plugins. There is a discussion about this exact issue at

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"

aaronvargas
  • 12,189
  • 3
  • 52
  • 52
2

FYI, I use a dynamic version and found that I had to define version prior to apply of my plugins. Probably because java or maven-publish sets the publication details upon application.

Peter Kahn
  • 12,364
  • 20
  • 77
  • 135
0

I upgraded gradle version from 2.1 to 2.12 and it solved this problem, fwiw.

davidjmcclelland
  • 430
  • 9
  • 20