How to use gradle extension correctly in plugins using GradleBuild task? - gradle

EDIT : I rephrased my question in taken the propositon of David M. Karr into account.
I am writing a gradle plugin. This plugin is launching a task extending GradleBuild. The external gradle build file needs some info as parameters. These parameters are given in project extension.
Plugin code
class MyPlugin implements Plugin<Project> {
def mExt
void apply(Project project) {
mExt = project.extensions.create('myext',MyExt)
project.task('myTask', type:GradleBuild){
def param = new StartParameter()
param.setProjectProperties([target:getTarget()])
// Problem here
startParameter = param
buildFile = getMyBuildPath()
tasks = [
'build',
'generateDebugJavadocJar'
]
}
}
def getMyBuildPath(){
...
}
// Problem here
def getTarget(){
return {mExt.target}
}
}
class MyExt {
def String target = "uninitialised"
}
Gradle build file :
apply plugin : 'com.example.myplugin'
ext{
target = "myTarget"
}
External Gradle build file :
task build(){
println project.target
}
If I put a closure in getTarget(), println project.target shows the closure and not the string.
If I don't put the closure :
// Problem here
def getTarget(){
return mExt.target
}
Then I got "uninitialised" instead of "myTarget".
How can I get the value of myext.target here ?
I am using gradle 2.3

Try this:
Define an instance variable called "myext", of type "MyExt".
In the "apply" method, do this:
myext = project.extensions.create('myext',MyExt)
In the "getTarget" method, return "myext.target".

I have succeeded in getting what I wanted to in using project.afterEvaluate method. Thanks to this question
1) In gradle build task, startParameter.projectProperties is waiting for a map, not a closure. So the idea to put a closure for a lazy definition cannot work.
2) If I put directly in my plugin a reference to mExt.target or project.myext.target, then the initial value is set. The value put in my build.gradle file is not used because the plugin is already evaluated.
3) project.afterEvaluate() solve my problem. The closure ends configuring myTask in using the project's extension.
void apply(Project project) {
project.extensions.create('myext',MyExt)
project.task('myTask', type:GradleBuild){
buildFile = getMyBuildPath()
tasks = [
'build',
'generateDebugJavadocJar'
]
}
project.afterEvaluate { proj ->
proj.myTask.startParameter.projectProperties = [target:proj.myext.target]
}
}

Related

TaskInternal.execute Migration

In a spring boot application in the build.gradle
task loadDbConfigFromSpringProfile {
def activeProfileProperties = new Properties()
file("src/main/resources/application.properties").withInputStream {
activeProfileProperties.load(it)
}
def profileProperties = new Properties()
file("src/main/resources/application-" + activeProfileProperties.getProperty("spring.profiles.active") + ".properties").withInputStream {
profileProperties.load(it)
}
active_db_url = profileProperties.getProperty("spring.datasource.url")
}
loadDbConfigFromSpringProfile.execute()
When i run application with gradle 4.4, I get
The TaskInternal.execute() method has been deprecated and is scheduled
to be removed in Gradle 5.0.
I started to create a class who extends extends DefaultTask but I'm not sure if it's the easier way to fix issue.
If you remove the call to loadDbConfigFromSpringProfile.execute(), your code will have the same effect.
You do not even need the task definition, putting the following in your build file has the same effect:
def activeProfileProperties = new Properties()
file("src/main/resources/application.properties").withInputStream {
activeProfileProperties.load(it)
}
def profileProperties = new Properties()
file("src/main/resources/application-" + activeProfileProperties.getProperty("spring.profiles.active") + ".properties").withInputStream {
profileProperties.load(it)
}
active_db_url = profileProperties.getProperty("spring.datasource.url")
Here is why: The task you are defining has no actions. Actions can be added to a task by using doLast or doFirst as shown in the hello world example.
That also means that everything which is in the body of your task is executed at configuration time - since it is supposed to be configuring the task.
Therefore, loadDbConfigFromSpringProfile.execute() has no effect - the task is skipped since it has no actions.
you can replace by finalizedBy()
https://discuss.gradle.org/t/how-to-call-task-execution-from-outside-task/25971

Are pmd, repositories etc tasks in Gradle

I am creating basic custom tasks in Gradle and learning how to extend them to do more complicated actions (Learning from here: https://docs.gradle.org/current/userguide/tutorial_using_tasks.html).
One of my reference projects, which I am extending to learn Gradle looks something like this
// pmd config
pmd {
ignoreFailures = false
reportsDir = file("$globalOutputDir/pmd")
toolVersion = toolVersions.pmdVersion
}
repositories {
mavenCentral()
}
task listSubProjects{
doLast{
println 'Searching in root dir `'
}
}
My question is around the pmd and repositories sections and why they don't have a clear qualifier like "task" on them but my listSubProjects requires a task qualifier? Are these inherited tasks from plugins and don't need a task qualifier?
The blocks that you see are task extensions, also discussed here.
A plugin creator can define extensions to allow users to configure a plugin:
// plugin code
class GreetingPluginExtension {
// default value
String message = 'Hello from GreetingPlugin'
}
// plugin code
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
def extension = project.extensions.create('greeting', GreetingPluginExtension)
// Add a task that uses configuration from the extension object
...
}
}
In project.extensions.create('greeting',... the greeting block to be used later in build.gradle files is defined.
Then in user build.gradle files
apply plugin: GreetingPlugin
// Configure the extension
greeting.message = 'Hi from Gradle'
// Same effect as previous lines but with different syntax
greeting {
message = 'Hi from Gradle'
}
Often the name of the extension is chosen to be the same as the plugin and/or the task, which can make things confusing.

Customize gradle task properties when dependsOn

I created custom task which should process output of gradle dependencies task ( exactly ./gradlew dependencies --configuration myFlavorDebugRuntimeClasspath). I want to create my custom task for every buildVariant (PassportGenerateTask passportGen = project.tasks.create("pasportGenerate${variantName}", PassportGenerateTask)).
But i cant customize dependenciesReportTask properties for every passportGen. When i call passportGen for speciefic buildVariant it use dependenciesReportTask.setProperty("configurations", ...) of last build variant among all (project.android.applicationVariants.all).
For examlpe if I have following build variants (googleDebug googleRelease samsungDebug samsungRelease) and call passportGen task (./gradlew pasportGenerateGoogleDebug) it will use wrong properties for dependenciesReportTask (configuration will be samsungReleaseRuntimeClasspath configuration )
class AppPlugin implements Plugin<Project> {
void apply(Project project) {
project.afterEvaluate {
// Create tasks for each Build Variant
project.android.applicationVariants.all { ApplicationVariant variant ->
def variantName = variant.name.capitalize()
def variantOutput = variant.outputs.first()
//Generating configuration name for dependency report
def configurationName = ""
if (variant.productFlavors.size() > 0) {
configurationName += variant.productFlavors.get(0).name
configurationName += variant.getBuildType().name.capitalize()
} else {
configurationName += variant.getBuildType().name
}
configurationName += "RuntimeClasspath"
def configurations = project.configurations.collect()
configurations.removeAll {
!it.name.equals(configurationName)
}
//prepare file for output of "dependencies" tasks
def depReportFileName = "dependeciesReport${variantName}.txt"
def dependenciesReportOutputFile = new File(depReportFileName)
//Get "dependencies" task from all project tasks
def dependenciesReportTask = project.tasks["dependencies"]
dependenciesReportTask.setProperty("configurations", configurations)
dependenciesReportTask.setProperty("outputFile", dependenciesReportOutputFile)
//create cutom task for every buildVariant which depends on "dependencies" task
PassportGenerateTask passportGen = project.tasks.create("pasportGenerate${variantName}", PassportGenerateTask)
passportGen.variant = variant
passportGen.configuration = configurations.collect().get(0)
//add dependency on "dependencies"
passportGen.dependsOn dependenciesReportTask
}
}
}
}
The only way i can achieve what i want is call:
def dependenciesReportTask = project.tasks["dependencies"]
dependenciesReportTask.setProperty("configurations", configurations)
dependenciesReportTask.setProperty("outputFile", dependenciesReport)
dependenciesReportTask.execute()
inside my custom PassportGenerateTask task main action method (#TaskAction) but calling execute inside other task is deprecated feature and will be removed in gradle 5.0
It is not only deprecated, it is purely internal implementation detail that most probably does not do what you expect it does and almost always produces many more problems than it tries to solve and actually fails.
You most probably also shouldn't rape the dependencies task. If you need multiple such tasks with different configuration, simply create new tasks of type DependencyReportTask and configure them however you like.

Creating a closure in ext

I am implementing the texturePacker task given in LibGDX's TexturePacker with gradle.
project.ext {
// ...
texturePacker = ["assets", "../android/assets", "texture"]
}
import com.badlogic.gdx.tools.texturepacker.TexturePacker
task texturePacker << {
if (project.ext.has('texturePacker')) {
logger.info "Calling TexturePacker: "+ texturePacker
TexturePacker.process(texturePacker[0], texturePacker[1], texturePacker[2])
}
}
I got it working with the suggested modifications for the classpath and added extension variable. Now I want to modify the textPacker extension variable to be a closure (Is that the right terminology?) with descriptive member names rather than an array. I tried doing this:
project.ext {
// ...
texturePacker {
inputDir = "assets"
outputDir = "../android/assets"
packFileName = "texture"
}
}
This gives the following error:
Error:Could not find method texturePacker() for arguments [build_4dusyb6n0t7j9dfuws8cc2jlu$_run_closure1$_closure7#6305684e] on project ':desktop' of type org.gradle.api.Project.
I am very new to gradle and groovy, so I have no idea what this error means. More importantly, what is the correct way to do what I want?
I suppose, closure is not the thing you need, since it's used not to store variables, but to store some executable code. By the way, if need to store it, you have to add = as follows:
project.ext {
texturePacker = {
inputDir = "assets"
outputDir = "../android/assets"
packFileName = "texture"
}
}
Anyway, if need to store variables within texturePacker variable, you rather have to use a Map type, then a Closure. This could be done like this:
project.ext {
texturePacker = [
inputDir : "assets",
outputDir : "../android/assets",
packFileName : "texture"
]
}
And then you can access this variable just by names, as:
println texturePacker.inputDir
Or, I think you can also go for implementing your own task with those properties. You can use DefaultTask which is a standard implementation of a regular task (and I'm sure it'd be enough for you);
class TexturePacker extends DefaultTask {
String inputDir; // a property - not a field!
String outputDir; // a property - not a field!
...
#TaskAction
void doSth(){
// do sth with properties above - that will be called automatically by gradle as a task-execution
}
}
task packer (type:TexturePacker) {
inputDir '<your-input-dir>'
outputDir '<your-output-dir>'
}
Syntax might not be super correct, but I think you get the idea.

configure custom plugin using params from extension

Hi i'm trying to dynamically create and configure task based on plugin extension values, problem seems to be evaluation order, is there any way to work around it?
apply plugin: SetupPlugin
setup {
destDir = 'some directory set per project in build.gradle'
sourceFile = 'some file set per project in build.gradle'
}
class PluginExtension {
String destDir
String sourceFile
}
class SetupPlugin implements Plugin<Project> {
def placeholders
void apply(Project project) {
project.extensions.create("setup", PluginExtension)
project.task ("setupEnvironment", type: Copy) {
doFirst() {
//computes placeholders <-- project.setup has value here
}
into (project.setup.destDir){ //<-- project.setup is null
from project.setup.sourceFile
}
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: placeholders)
}
}
}
Moving this into the doFirst block can cause some sideeffects, as the gradle up to date task might run into problems as reconfigure the parameters of your copy task at execution time instead of configuration time. A quickfix which should do the trick is to defer the evaluation by using closures:
...
void apply(Project project) {
project.extensions.create("setup", PluginExtension)
project.task ("setupEnvironment", type: Copy) {
doFirst() {
//computes placeholders <-- project.setup has value here
}
into {project.setup.destDir}
from { project.setup.sourceFile }
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: placeholders)
}
}
...
hope that helped!
cheers,
René
It is because apply is called before applying setup settings.
It works for doFirst because it called after apply during build.
Maybe you may wrap your copy into doLast?
It turns out I asked a question that I think is very similar to this one. I may be missing a subtle difference, but in case it helps: here it is.

Resources