Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle throws ClassNotFoundException using a plugin with dependencies

I am experiencing ClassNotFoundExceptions when using a Gradle plugin with compile dependencies in a project in the same root folder.

The directory structure is rather simple, with a plugin and the project that will use it:

  • root
    • buildSrc
    • demo-project

The build.gradle in buildSrc is as follows:

apply plugin: 'groovy'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}

dependencies {
    compile gradleApi()
    compile localGroovy()

    compile 'org.apache.commons:commons-configuration2:2.0'
    compile 'commons-beanutils:commons-beanutils:1.9.2'
}

And the plugin itself builds correctly.

When applying the plugin to my demo-project, the afterEvaluate part fails because it can't find the commons-configuration2 dependencies (so the plugin executes, than fails at the incriminated line):

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':demo-project'.
> java.lang.ClassNotFoundException: org.apache.commons.configuration2.PropertiesConfiguration

Just to complete my question, here's the build.gradle in my demo-project:

apply plugin: 'java'
apply plugin: 'com.sample.myplugin'

thisIsMyPluginConfiguration {
    something = 'here'
}

Is this a known issue or is there a workaround that I can use?

Might be related to Adding dependencies to a custom gradle plugin.

EDIT: Trying to specify a different dependency makes it work. It looks like it's an issue of how the culprit PropertiesConfiguration class is loaded. The full stacktrace is:

* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':demo-java'.
        at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:79)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:74)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:61)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:510)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:90)
        at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:47)
        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.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
        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:78)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:48)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:51)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:28)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:43)
        at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:170)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.Main.doAction(Main.java:33)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
        at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Caused by: org.apache.commons.configuration2.ex.ConfigurationRuntimeException: java.lang.ClassNotFoundException: org.apache.commons.configuration2.PropertiesConfiguration
        at org.apache.commons.configuration2.beanutils.BeanHelper.fetchBeanClass(BeanHelper.java:549)
        at org.apache.commons.configuration2.beanutils.BeanHelper.createBeanCreationContext(BeanHelper.java:616)
        at org.apache.commons.configuration2.beanutils.BeanHelper.createBean(BeanHelper.java:456)
        at org.apache.commons.configuration2.beanutils.BeanHelper.createBean(BeanHelper.java:479)
        at org.apache.commons.configuration2.beanutils.BeanHelper.createBean(BeanHelper.java:492)
        at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResultInstance(BasicConfigurationBuilder.java:447)
        at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResult(BasicConfigurationBuilder.java:417)
        at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.getConfiguration(BasicConfigurationBuilder.java:285)
        at org.apache.commons.configuration2.builder.ConfigurationBuilder$getConfiguration.call(Unknown Source)
        at com.novoda.buildproperties.BuildProperties.<init>(BuildProperties.groovy:23)
        at com.novoda.buildproperties.BuildPropertiesPlugin$_apply_closure1.doCall(BuildPropertiesPlugin.groovy:17)
        at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(ClosureBackedMethodInvocationDispatch.java:40)
        at org.gradle.listener.ClosureBackedMethodInvocationDispatch.dispatch(ClosureBackedMethodInvocationDispatch.java:25)
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:44)
        at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:79)
        at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:30)
        at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
        at com.sun.proxy.$Proxy11.afterEvaluate(Unknown Source)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:67)
        ... 44 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.configuration2.PropertiesConfiguration
        at org.apache.commons.lang3.ClassUtils.getClass(ClassUtils.java:828)
        at org.apache.commons.lang3.ClassUtils.getClass(ClassUtils.java:894)
        at org.apache.commons.lang3.ClassUtils.getClass(ClassUtils.java:877)
        at org.apache.commons.configuration2.beanutils.BeanHelper.loadClass(BeanHelper.java:507)
        at org.apache.commons.configuration2.beanutils.BeanHelper.fetchBeanClass(BeanHelper.java:545)
        ... 62 more
like image 469
frapontillo Avatar asked Oct 18 '22 09:10

frapontillo


1 Answers

A search of the web suggested that Gradle uses custom class loaders to control the process. Here is some code used that enabled the PropertiesConfiguration to be discovered

ClassLoader gradleClassLoader = Thread.currentThread().getContextClassLoader()
Thread.currentThread().setContextClassLoader(PropertiesConfiguration.class.getClassLoader())

methodThatUsesPropertiesConfiguration()

Thread.currentThread().setContextClassLoader(gradleClassLoader)

Not being intimately familiar with the class loader mechanism of Gradle, this may have ramifications that others may share. However, the method called during this temporary switch does not interact with other Gradle functionality and so deemed it relatively safe.

like image 77
bkeyser5280 Avatar answered Oct 27 '22 10:10

bkeyser5280