koala421
koala421

Reputation: 816

get two tasks to apply plugin gradle

Is there a way to have two tasks in gradle and have both return the proper values setting parameters within the plugins

I have:

Build.gradle

apply plugin: 'gradle.plugin'

task FirstTask(type: com.nav.Coding){
  param.dictFile = file ("${projectDir}/src/main/resources/customized_struct.xml")
  param.outputDirectory = file("${buildDir}/generated/"
}

task SecondTask(type: com.nav.Coding){
  param.dictFile = file("${projectDir}/src/main/resources/customized_struct_two.xml")
  param.outputDirectory = file("${buildDir}/generated/"
}

For some reason my output is only taking the second dictFile and not the first when setting the parameters to the JVM and processing the custom plugin.

Command Output

$ gradle -q FirstTask SecondTask

:com:nav:Coding:FirstTask
Processing C:\dev\src\main\resources\customized_struct_two.xml
This is my output dictFile C:\dev\src\main\resources\customized_struct_two.xml
This is my output outputDirectory C:\dev\build\generated

:com:nav:Coding:SecondTask
Processing C:\dev\src\main\resources\customized_struct_two.xml
This is my output dictFile C:\dev\src\main\resources\customized_struct_two.xml
This is my output outputDirectiory C:\dev\build\generated

BUILD SUCCESSFUL

Total time: 12.79 secs

Upvotes: 0

Views: 79

Answers (1)

lance-java
lance-java

Reputation: 27958

I beleive you are falling for the common mistake of putting logic in the configuration phase instead of the execution phase. I beleive you want to:

task FirstTask(type: com.nav.Coding){
    doFirst {
        param.dictFile = file ("${projectDir}/src/main/resources/customized_struct.xml")
        param.outputDirectory = file("${buildDir}/generated/"
    }
}

task SecondTask(type: com.nav.Coding){
    doFirst {
        param.dictFile = file("${projectDir}/src/main/resources/customized_struct_two.xml")
        param.outputDirectory = file("${buildDir}/generated/"
    }
}

This feels wierd to me

  1. Where does param come from?
  2. Why aren't dictFile and outputDirectory properties on the com.nav.Coding task?

Upvotes: 1

Related Questions