{"record":{"id":"85aa2689b9c4bd60","repo":"apache/maven","slug":"a-type-incompatibility-occurred-while-executing","errorCode":null,"errorMessage":"A type incompatibility occurred while executing ${mojoDescriptor.getId()}: ${e.getMessage()}","messagePattern":"A type incompatibility occurred while executing (.+?): (.+?)","errorType":"exception","errorClass":"PluginExecutionException","httpStatus":null,"severity":"error","filePath":"impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java","lineNumber":195,"sourceCode":"        } catch (LinkageError e) {\n            mojoExecutionListener.afterExecutionFailure(\n                    new MojoExecutionEvent(session, project, mojoExecution, mojo, e));\n            ByteArrayOutputStream os = new ByteArrayOutputStream(1024);\n            PrintStream ps = new PrintStream(os);\n            ps.println(\"An API incompatibility was encountered while executing \" + mojoDescriptor.getId() + \": \"\n                    + e.getClass().getName() + \": \" + e.getMessage());\n            pluginRealm.display(ps);\n            Exception wrapper = new PluginContainerException(mojoDescriptor, pluginRealm, os.toString(), e);\n            throw new PluginExecutionException(mojoExecution, project, wrapper);\n        } catch (ClassCastException e) {\n            mojoExecutionListener.afterExecutionFailure(\n                    new MojoExecutionEvent(session, project, mojoExecution, mojo, e));\n            ByteArrayOutputStream os = new ByteArrayOutputStream(1024);\n            PrintStream ps = new PrintStream(os);\n            ps.println(\"A type incompatibility occurred while executing \" + mojoDescriptor.getId() + \": \"\n                    + e.getMessage());\n            pluginRealm.display(ps);\n            throw new PluginExecutionException(mojoExecution, project, os.toString(), e);\n        } catch (RuntimeException e) {\n            mojoExecutionListener.afterExecutionFailure(\n                    new MojoExecutionEvent(session, project, mojoExecution, mojo, e));\n            throw e;\n        } finally {\n            mavenPluginManager.releaseMojo(mojo, mojoExecution);\n            scope.exit();\n            Thread.currentThread().setContextClassLoader(oldClassLoader);\n            legacySupport.setSession(oldSession);\n        }\n    }\n\n    /**\n     * TODO pluginDescriptor classRealm and artifacts are set as a side effect of this\n     *      call, which is not nice.\n     * @throws PluginResolutionException\n     */\n    @Override","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java#L177-L213","documentation":"A ClassCastException escaped a mojo execution. In Maven this almost always means class identity broke across classloaders: the same class name is loaded both from the plugin realm and from Maven core (or another realm), so a JVM-level cast between 'identical' types fails. Maven dumps the plugin realm and fails the build with PluginExecutionException.","triggerScenarios":"A plugin depends on a library that Maven core also exports at a different version, and an object created in one classloader is passed to code that casts it in the other. Also: two plugin dependencies ship the same classes in different artifacts and class resolution picks the incompatible copy.","commonSituations":"Plugins bundling guava/commons-*/plexus-utils versions that clash with Maven's own; shaded or relocated jars duplicating class names; extensions and plugins sharing types across realms.","solutions":["Read the cast message: 'class X cannot be cast to class X' with two different ClassLoaders named in the detail confirms a realm conflict.","Find the duplicate artifact in the realm dump and exclude it from the plugin, or align its version with the one Maven core provides.","If you own the plugin, depend on the shared API with provided scope instead of bundling it.","Upgrade to a plugin version tested against your Maven release."],"exampleFix":"<!-- before: plugin drags its own commons-cli 1.3, clashing with another copy in the realm -->\n<plugin>\n  <groupId>org.apache.maven.plugins</groupId>\n  <artifactId>maven-antrun-plugin</artifactId>\n  <dependencies>\n    <dependency>\n      <groupId>commons-cli</groupId>\n      <artifactId>commons-cli</artifactId>\n      <version>1.3</version>\n    </dependency>\n  </dependencies>\n</plugin>\n<!-- after: drop the redundant dependency so a single copy loads -->\n<plugin>\n  <groupId>org.apache.maven.plugins</groupId>\n  <artifactId>maven-antrun-plugin</artifactId>\n</plugin>","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean realmCompatible(Object value, Class<?> target) {\n    return value == null\n        || (target.getClassLoader() == value.getClass().getClassLoader() && target.isInstance(value));\n}","tryCatchPattern":"catch (ClassCastException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"cannot be cast to\")) {\n        // the detail line names both ClassLoaders: a cross-realm leak, fix dependencies\n        log.error(\"classloader clash: {}\", e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Do not bundle libraries Maven core already provides into your plugin","Use provided scope for maven-plugin-api and shared core libraries","Run dependency:tree and check for overlapping classes before shipping a plugin"],"tags":["maven","plugin","classcastexception","classloader","dependency-conflict"],"backgroundTag":"classcastexception-classloader-conflict","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}