Repository navigation
Description
Andrew Schurman opened MINVOKER-182 and commented
When addTestClassPath is set and you have a different version of groovy than invoker on your classpath, you will run into ClassCastException when
pre-/post- build hooks are run for tests. This occurs due to invoker creating groovy scripts in its version of groovy, but using a different version of groovy as the runtime (since test classpath elements are loaded first when addTestClassPath=true).
In my specific case, I had a transitive dependency on groovy 1.7, but invoker uses groovy 2.0. Being transitive does make it harder to spot, but
more importantly you may not have access to the dependency that depends on groovy. This could make swapping groovy versions impossible depending on the gap between versions.
Stack trace:
groovy.lang.GroovyRuntimeException: Failed to create Script instance for class: class Script1. Reason: java.lang.ClassCastException: Script1 cannot be
cast to groovy.lang.GroovyObject
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:443)
at groovy.lang.GroovyShell.parse(GroovyShell.java:625)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:516)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:556)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:527)
at org.apache.maven.shared.scriptinterpreter.GroovyScriptInterpreter.evaluateScript(GroovyScriptInterpreter.java:83)
at org.apache.maven.shared.scriptinterpreter.ScriptRunner.executeRun(ScriptRunner.java:249)
at org.apache.maven.shared.scriptinterpreter.ScriptRunner.run(ScriptRunner.java:177)
at org.apache.maven.plugin.invoker.AbstractInvokerMojo.runBuild(AbstractInvokerMojo.java:1692)
at org.apache.maven.plugin.invoker.AbstractInvokerMojo.runBuild(AbstractInvokerMojo.java:1360)
at org.apache.maven.plugin.invoker.AbstractInvokerMojo.runBuilds(AbstractInvokerMojo.java:1210)
at org.apache.maven.plugin.invoker.AbstractInvokerMojo.execute(AbstractInvokerMojo.java:723)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.ClassCastException: Script1 cannot be cast to groovy.lang.GroovyObject
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:421)
... 32 more
Affects: 1.8
Issue Links:
- MINVOKER-274 Use Groovy 3.x to prevent Java9+ warnings about Groovy 2.x using illegal reflection
1 votes, 7 watchers
Activity
jira-importer commentedon Feb 15, 2015
Andrew Schurman commented
As a not-so-nice workaround, one can add a (test-scoped) version of groovy that invoker is using to swap out the groovy version of the dependency. This won't help if you do depend on API in that version of groovy which is missing from the one invoker is using.
jira-importer commentedon Jun 5, 2016
Martin Todorov commented
I am getting this as well with:
The exception:
Plugin configuration:
I also have:
And that doesn't seem to help.
Is there a way around this? I mean, this was all working fine for our project till a few days ago. There have been no changes to the said project in a long while and the code was running these integration tests just fine for a long time (we're using the
maven-invoker-pluginto simulate Maven behavior against another project, so our CI was set up to invoke these test when there were changes to the other project).I've also posted this on Stackoverflow, as I wasn't initially sure whether it was a bug, or not:
From the discussion in this bug report and this discussion, I am under the impression that other people have been able to find a workaround, but adding the
groovy-alldependency doesn't seem to be making a difference in my case at all.Please advise!
jira-importer commentedon Jun 5, 2016
Martin Todorov commented
barrie, Robert Scholte,
Perhaps you have some ideas?
jira-importer commentedon Jun 5, 2016
Martin Todorov commented
...Right... It only now, when re-reading this, that I realized this is being caused by a transitive dependency of a dependency we recently introduced. Thanks for the hint!
jira-importer commentedon Jun 5, 2016
Robert Scholte commented
I've never been a huge fan of the
addTestClassPath-parameter, it simply adds everything even when only a couple of classes are required. A more pure solution is to have a separate source folder for IT classes and to have separate dependencies with their own scope. Actually, you might argue if it should be a project dependency or a plugin dependency. But these are future solutions.For now I would switch to bash scripts to prevent these kind of clashes.
jira-importer commentedon Dec 23, 2019
Elliotte Rusty Harold commented
another instance of https://jlbp.dev/JLBP-1.html#separate-the-tool-classpath-from-the-product-classpath
jira-importer commentedon Feb 2, 2021
Bertrand Martin commented
Suggestion: Use groovysh to run verify.groovy, so there is no conflict with the current JVM and whatever dependencies are loaded by the project being tested with maven-invoker-plugin.