Java.lang.ClassCastException: com.ford.it.gradle.plugins.builddependencies.VersionSuffixTask_Decorated incompatible with java.lang.Class


(Curtis Reinke) #1

I’ve written a simple Gradle plugin and task to add a version suffix to the version property of a project as follows:

package com.ford.it.gradle.plugins.builddependencies;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class VersionSuffixPlugin implements Plugin{
@Override
public void apply(Project project) {
project.getTasks().create(“VersionSuffixTask”, VersionSuffixTask.class);
}
}

package com.ford.it.gradle.plugins.builddependencies;

import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;

public class VersionSuffixTask extends DefaultTask {

@TaskAction
public void setVersionUsingBuildTypeSuffix() { 
    final Project project = getProject();
    final String buildType = (String) project.getProperties().get("buildType");
    final String version = (String) project.getProperties().get("version");
    
    System.out.println("Test");
    String versionSuffix = null;
    
    if (buildType == null || buildType.equals("SNAPSHOT")) {
        versionSuffix = "-SNAPSHOT";
    } else if (buildType.equals("PRODUCTION_RELEASE_CANDIDATE")) {
        versionSuffix = "-PRODUCTION_RELEASE_CANDIDATE";
    } else if (buildType.equals("PRODUCTION")) {
        versionSuffix = "";
    } else {
        versionSuffix = "-" + buildType;
    }
    project.setProperty("version", version + versionSuffix);
}

}

To test it, I’ve created another simple Gradle project containing the following build.gradle script:

apply plugin: 'maven’
apply plugin: 'java’
apply plugin: ‘com.ford.it.gradle.plugins.builddependencies.versionsuffix’

version = ‘1.2.0.0’

task versionSuffixTask(type: VersionSuffixTask)
versionSuffixTask.setVersionUsingBuildTypeSuffix()
println version

buildscript {
ext.defaultRepository = {
mavenLocal()

    maven {
        url "http://www.nexus.ford.com/content/repositories/ford_experimental_repository/"
    }
    
    maven {
        url "http://www.nexus.ford.com/content/groups/public/"
    }
}
repositories defaultRepository

dependencies {
    classpath "com.ford.it.gradle.plugins.builddependencies:GradlePlugins:1.0.0-SNAPSHOT"
}

}

When I try running the script, I get the following. Using a 1.6 JDK if that matters:

Working Directory: C:\Projects\GradlePluginsTester
Gradle User Home: C:\Users\CREINKE3.gradle
Gradle Distribution: Local installation at C:\ford\gradle\gradle-2.8
Gradle Version: 2.8
Java Home: C:\IBM\WAS80\java
JVM Arguments: None
Program Arguments: --stacktrace
Gradle Tasks: clean build

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘C:\Projects\GradlePluginsTester\build.gradle’ line: 7
  • What went wrong:
    A problem occurred evaluating root project ‘GradlePluginsTester’.

com.ford.it.gradle.plugins.builddependencies.VersionSuffixTask_Decorated incompatible with java.lang.Class

  • Try:
    Run with --info or --debug option to get more log output.
  • Exception is:
    org.gradle.api.GradleScriptException: A problem occurred evaluating root project ‘GradlePluginsTester’.
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
    at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$1.run(DefaultScriptPluginFactory.java:148)
    at org.gradle.configuration.ProjectScriptTarget.addConfiguration(ProjectScriptTarget.java:72)
    at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:153)
    at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:38)
    at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:25)
    at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)
    at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:55)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:499)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:86)
    at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:42)
    at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
    at org.gradle.initialization.DefaultGradleLauncher$2.run(DefaultGradleLauncher.java:125)
    at org.gradle.internal.Factories$1.create(Factories.java:22)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:122)
    at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGradleLauncher.java:32)
    at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:99)
    at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:93)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94)
    at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:46)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.tooling.internal.provider.runner.SubscribableBuildActionRunner.run(SubscribableBuildActionRunner.java:58)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:77)
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:47)
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:52)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
    at org.gradle.util.Swapper.swap(Swapper.java:38)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:47)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:71)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:246)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    Caused by: java.lang.ClassCastException: com.ford.it.gradle.plugins.builddependencies.VersionSuffixTask_Decorated incompatible with java.lang.Class
    at org.gradle.api.internal.project.taskfactory.TaskFactory.createTask(TaskFactory.java:76)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory.createTask(AnnotationProcessingTaskFactory.java:100)
    at org.gradle.api.internal.project.taskfactory.DependencyAutoWireTaskFactory.createTask(DependencyAutoWireTaskFactory.java:39)
    at org.gradle.api.internal.tasks.DefaultTaskContainer.create(DefaultTaskContainer.java:62)
    at org.gradle.api.internal.project.AbstractProject.task(AbstractProject.java:882)
    at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:232)
    at org.gradle.api.internal.BeanDynamicObject.invokeMethod(BeanDynamicObject.java:127)
    at org.gradle.api.internal.CompositeDynamicObject.invokeMethod(CompositeDynamicObject.java:150)
    at org.gradle.groovy.scripts.BasicScript.methodMissing(BasicScript.java:79)
    at build_29l5mumczv69elh12l9usq7no.run(C:\Projects\GradlePluginsTester\build.gradle:7)
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:91)
    … 60 more

BUILD FAILED

Total time: 0.167 secs


(Stefan Oehme) #2

These two lines produce a conflict. The first creates an uppercase variable that looks like a class literal, but isn’t :wink:

You probably wanted to have a lower-case task name and then configure that task instead of creating another one. Also, you should not call the tasks action yourself. This is Gradle’s job.

A task that modifies the project model is usually a bad idea, because that configuration change will come long after everyone else has read the version property. You probably just want a lazy Version class.

Also, use project.setVersion(), not project.setProperty("version", version + versionSuffix)