5

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

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

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

并且插件本身可以正确构建。

将插件应用到 mydemo-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'
}

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

可能与将依赖项添加到自定义 gradle 插件有关。

编辑:尝试指定不同的依赖项使其工作。看起来这是如何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
4

1 回答 1

3

网络搜索表明 Gradle 使用自定义类加载器来控制该过程。下面是一些用于发现 PropertiesConfiguration 的代码

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

methodThatUsesPropertiesConfiguration()

Thread.currentThread().setContextClassLoader(gradleClassLoader)

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

于 2017-06-21T16:41:11.587 回答