使用apply from的gradle 插件脚本 不能访问某些class(Task)
Hi,
We have some plugins on our own, which we apply in our build.gradle. Also we apply a second buildscript afterwards, which handles our tests, the following way:
apply {
plugin 'ownplugin’
from ‘tests.gradle’
}
In the tests.gradle we want to create a task of a type, which classfile is located in the ownplugin.
task executeImportBla(type: mycompany.plugin.BlaTask) {
… some configuration
}
This works, if the task creation happens in the build.gradle but doesn’t work if it happens in the tests.gradle. The error is:
Caused by: groovy.lang.MissingPropertyException: Could not find property ‘mycompany’ on root project ‘project’.
at org.gradle.api.internal.AbstractDynamicObject.propertyMissingException(AbstractDynamicObject.java:43)
at org.gradle.api.internal.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:3
If I add an import of the taskclass before and only use simplename when creating task, the class could not be found when importing. And again: in build.gradle the whole stuff is working.
Is this a bug or do I try to do something that is not possible?
Any help welcome,
Thank you
Markus
回答
Script plugins do not have access to the main buildscript classpath.
You could:
- add a dependency on mycompany.plugin to tests.gradle using the buildscript {} syntax
- push the logic from tests.gradle into a plugin class in the buildSrc folder of your project, giving you the added benefit of more IDE support when editing it