The Project Operations API
Because Gradle Configuration Cache will make it impossible to use the Project
instance after the configuration phase has ended, plugin authors can no longer rely on calling methods ion this class.
The only exceptions are within constructors of tasks and within extensions.
Plugin authors should take extra care as to not call the Task.getProject()
method anywhere except in the constructor.
In order to help with compatibility across version that has a configuration caching and those that don’t, the ProjectOperations was originally introduced.
It was subsequently found that some of the operations referenced within the instantiation was still not configuration cache-compatible and thus the ConfigurationCacheSafeOperations API was introduced in Grolifant 5.
Instances of the latter can be made members of tasks and their operations can be called within task actions.
The original ProjectOperations
can still be used, but should not be referenced from with tasks (except in constructors).
Usage
It can either be used as an extension or as a standalone instance.
import org.ysb33r.grolifant.api.core.plugins.GrolifantServicePlugin (1)
import org.ysb33r.grolifant.api.core.ProjectOperations
class MyPlugin implements Plugin<Project> {
void apply(Project project) {
project.pluginManager.apply(GrolifantServicePlugin) (2)
final po = project.extensions.getByType(ProjectOperations) (3)
final ccso = ConfigurationCacheSafeOperations.from(project) (4)
}
}
1 | Required import |
2 | Install the core plugin. |
3 | It is now possible to access the extension. |
4 | How to obtain a configuration cache-compatible set of operations. |
Legacy migration
If you are still using the below bootstrap, then change it to use the plugin instead.
import org.ysb33r.grolifant.api.core.ProjectOperations
ProjectOperations grolifant = ProjectOperations.maybeCreateExtension(project)
If you any detached usages like below , remove them. .Usage as a detached instance (legacy)
ProjectOperations grolifant = ProjectOperations.create(project)
Obtaining access to methods
Outside a task you can do this
ProjectOperations grolifant = ProjectOperations.find(project) (1)
1 | Shortcut for fining the project extension. Will throw an exception if the extension was not attached to the project. |
Inside a task you should not refer to the ProjectOperations
task.
Instead, use any of the methods available on ConfigurationCacheSafeOperations
.
GrolifantDefaultTask
import org.ysb33r.grolifant5.api.core.runnable.GrolifantDefaultTask (1)
class MyTask extends GrolifantDefaultTask { (2)
String showString() {
stringTools().stringize(123) (3)
}
}
1 | Required imports |
2 | Use GrolifantDefaultTask instead of DefaultTask |
3 | Use method such as GrolifantTaskMethods.stringTools() to access to the methods you are familiar with |