Gradle artifactory plugin saying “Cannot cast obje

2019-04-03 03:06发布

问题:

Here's the configuration to get the artifactory plugin:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'http://jcenter.bintray.com' }
    }
    dependencies {
        classpath group:'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1'
    }
}
apply plugin:'com.jfrog.artifactory'
apply plugin:'ivy-publish'

...some publish spec stuff...

I run gradle (2.3) and I get:

> Failed to apply plugin [id 'com.jfrog.artifactory']
   > Cannot cast object 'org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention@6b6c7be4' with class 'org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention' to class 'org.jfrog.gradle.plugin.artifactory.dsl.ArtifactoryPluginConvention'

Certainly looks like a classpath issue, but I literally have this project and a sibling project using this same set of gradle/artifactory configurations and one works and the other does not. Both are part of the same top level project. Same JDK (1.8.0_20). Same Gradle. Same everything.

I'm baffled...

回答1:

The problem was that when I added the various bits to the sibling project that meant I had two projects defining the buildscript {} section.

buildscript {
    ...
    dependencies {
        classpath group:'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1'
    }
}

Apparently that caused two different versions of the dependency to exist in the classpath, hence the error.

The solution was to move the buildscript bit into the master project so those dependencies are only defined once:

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath group:'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1'
    }
}


回答2:

Here's another potential cause. All of this looks to be a problem with rival classloaders defining the class. The full qualified classes include the loader. so, load A foo.bar is not loader B foo.bar and crossing that divide is a complex dance requiring interfaces and careful definition.

So, when using the Jenkins artifactory plugin to build your gradle project with the gradle artifactory plugin, you must add the usesPlugin or jenkins plugin will generate an init script which adds the gradle plugin on to a class loader.

def server = Artifactory.server "artifactory"
def rtGradle = Artifactory.newGradleBuild()
rtGradle.usesPlugin = true // Artifactory plugin already defined in build script
...

My problem was, desktop build OK, jenkins build shows this post's problem



回答3:

I was getting a similar exception when building with Jenkins. For me the conflict was with Jenkin's version and the version in the Build script:

To address this the Artifactory section of the build has a flag you can check specifying that you want to use the version from the gradle file:

This fixed my issue. Hope it helps.



回答4:

I had a similar problem. Gradle seems to try to reach across and do some checking or evaluation across siblings. I have a top level settings.gradle with 10 or so subprojects.

The fix for me was to put the buildscript block and dependencies at the top level build.gradle and put it in each of the individual subprojects build.gradle files where needed.

My guess as to the reason this works is that the plugin gets loaded in the parent which will be a parent classloader, then each child project inherits that classloader such that the declaration in the lower child script uses that classloaders class and CCE does not occur. The problem is they are the same class, but not assignable since the different classloaders per subproject if nothing is declared at the top. This was Gradle 2.4, and using IntelliJ 14.



回答5:

In case it helps someone, I got the same error, but for a different reason.

I had the following in my build.gradle:

dependencies {
    classpath "org.jfrog.buildinfo:build-info-extractor-gradle:+"
}

At some point the artifactory plugin updated itself from version 3.x to version 4.x while building, because no specific version was specified for the dependency. After it updated I got the error (Could not find any convention object of type ArtifactoryPluginConvention).

I guess the problem was that the rest of the configuration in my build script doesn't work with the new plugin version. Setting the dependency to use version 3.x fixed the problem for me:

dependencies {
    classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.+"
}


回答6:

While the currently accepted answer correctly identifies the cause of this issue, the proposed solution doesn't work when you still need to be able to build individual subprojects (because then of course they no longer have access to the buildscript defined repositories and dependencies). The solution that worked for me was to have identical buildscript blocks in each of my subprojects, that seemed to be the key. Any variations would cause the original error.