Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle configuration "builtBy" a task

Tags:

Consider the following buildscript where

  • addToMyConfig adds a dependency to a Configuration named myConfig
  • useMyConfig consumes the myConfig Configuration and forces it to resolve()
configurations {
    myConfig
}
task addToMyConfig {
    doLast {
        println "Doing some work"
        dependencies {
            myConfig 'log4j:log4j:1.2.17'
        }
    }
}
task useMyConfig {
    doLast {
        println "myConfig = $configurations.myConfig.files"
    }
}

Question: Is there a way to fire addToMyConfig every time configurations.myConfig is resolved without adding a task dependency where useMyConfig depends on addToMyConfig?

I would like to say:

configurations.myConfig.builtBy addToMyConfig

** I do NOT want to say **

useMyConfig.dependsOn addToMyConfig 

I want to avoid useMyConfig.dependsOn addToMyConfig because there may be many tasks which consume configurations.myConfig

Note: There is a ConfigurableFileCollection.builtBy(Object... tasks) method which would solve my problem, if only it existed on the Configuration interface (configuration extends FileCollection)

like image 869
lance-java Avatar asked Mar 15 '18 09:03

lance-java


1 Answers

Do you really need a task to populate the configuration with dependencies?

Configuration#withDependencies can be used to add dependencies during resolution. Like this:

configurations {
  myConfig
}

configurations.myConfig.withDependencies {deps ->
  println "Resolving dependencies"
  dependencies {
    myConfig "log4j:log4j:1.2.17"
  }
}

task useMyConfig {
    doLast {
        println "myConfig = $configurations.myConfig.files"
    }
}
like image 115
Eloff Avatar answered Oct 14 '22 03:10

Eloff