Gradle使用具有依赖项的插件抛出ClassNotFoundException

时间:2016-05-12 08:41:02

标签: gradle apache-commons gradle-plugin apache-commons-config

在同一根文件夹中的项目中使用带有编译依赖项的Gradle插件时,我遇到ClassNotFoundException个问题。

目录结构相当简单,有一个插件和将使用它的项目:

    • buildSrc
    • demo-project

build.gradle中的buildSrc如下:

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'
}

插件本身构建正确。

将插件应用到demo-project时,afterEvaluate部分失败,因为它无法找到commons-configuration2依赖项(因此插件执行,而不是在被控制的行中失败) ):

FAILURE: Build failed with an exception.

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

为了完成我的问题,请点击build.gradle中的demo-project

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

thisIsMyPluginConfiguration {
    something = 'here'
}

这是一个已知问题还是我可以使用的解决方法?

可能与Adding dependencies to a custom gradle plugin有关。

编辑:尝试指定不同的依赖项使其有效。看起来它是如何加载罪魁祸首PropertiesConfiguration类的问题。完整的堆栈跟踪是:

* 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

1 个答案:

答案 0 :(得分:1)

对网络的搜索表明Gradle使用自定义类加载器来控制进程。以下是一些使用的代码,可以发现PropertiesConfiguration

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

methodThatUsesPropertiesConfiguration()

Thread.currentThread().setContextClassLoader(gradleClassLoader)

不熟悉Gradle的类加载器机制,这可能会产生其他人可能分享的后果。但是,在此临时切换期间调用的方法不会与其他Gradle功能交互,因此认为它相对安全。