MavenDeployer#getModel class type issues

I am trying to write a gradle plug that will configure uploadArchives task and set the right pom properties for the maven deployer.

I want to convert the following to a java plugin:

uploadArchives {
    repositories {
        mavenDeployer {
            pom.project {
                name 'FooBar'
                url 'http://foo.bar.com'
                inceptionYear '2011'

                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
            }
        }
    }
}

I’m doing the following in Plugin apply method:

Upload uploadTask = (Upload) project.getTasks().getByName("uploadArchives");
uploadTask.getRepositories().withType(MavenDeployer.class, new Action<MavenDeployer>() {
    @Override
    public void execute(MavenDeployer mavenDeployer) {
        org.apache.maven.model.Model model = (org.apache.maven.model.Model) mavenDeployer.getPom().getModel();
    }
}

Initially this failed due to org.apache.maven.model.Model not being in the classpath. So I added the following to my buildSrc/build.gradle:

dependencies {
    compile 'org.apache.maven:maven-model:3.0.4'
}

After this, project compiles, but then it fails with:

A problem occurred evaluating project ':fooBarThing'.
> org.apache.maven.model.Model cannot be cast to org.apache.maven.Model

I am guessing that the versions of maven-model are somehow mismatching, but I was not able to get that sorted. Any suggestions how to proceed?

Thanks,
Aurimas

To clarify: I’m using gradle 3.2

Friendly ping? Does anyone have an idea of how to do this?

I tried adding gradleApi() but it seems like it is not included in the gradleApi(). Any other ideas?

It feels like there’s two versions of the maven model classes loaded by two different classloaders. I’m not sure why compile fails when gradleApi() is in the compile dependencies.

A class is only equal to another if getClassLoader() is also equal so one can’t be cast to the other. Perhaps you could try providedCompile configuration for the maven dependency so it’s not on the runtime classpath.

I think a gradle guru needs to comment, perhaps the maven dependency has special classloading