Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve a dependency dynamically inside a gradle task

Tags:

gradle

I am trying to build a gradle plugin, which does the following:

  1. As part of one its tasks, it creates a new configuration
  2. It adds a DefaultExternalModuleDependency to this configuration - more specifically, it constructs a dependency to the application server zip file (available on Nexus). This information can be overridden by the invoking project as well.
  3. Tries to resolve this newly added dependency and then unpacks the file to a local folder

All of this was working well when I had the details hard coded in a build file, but it looks like adding dependencies as part of a task are not treated the same way as having that information available at the parsing time.

So my question is, how do I get the project to reload the configurations / dependencies?

The code looks like the following:

@TaskAction
void installAppserver() {
  Dependency dependency = new DefaultExternalModuleDependency(group,name,version)
  Configuration configuration = project.configurations.detachedConfiguration(dependency)
  configuration.setTransitive(false)
  configuration.files.each { file ->
    if (file.isFile() && file.name.endsWith('.zip')) {
      println 'Attempting to unzip: ' + file + ' into folder: ' + appServerFolder
      new Copy().from(project.zipTree(file)).into(appServerFolder).execute()
    }
  }
}

The problem is that the actual artifacts are not getting resolved!

like image 620
Harsha R Avatar asked Oct 19 '25 02:10

Harsha R


1 Answers

A task can't configure the build model (that's what plugins do). It's fine to create and resolve a detached configuration in a task. If this doesn't work, there is likely a problem with the task's code, or the dependency it tries to resolve. Note that dependencies can only be resolved if the correct repository(s) are defined.

Instead of new DetaultExternalModuleDependency() (which is an internal class), project.dependencies.create() should be used. Instead of new Copy().execute() (Task#execute must not be called from user code), project.copy should be used.

like image 187
Peter Niederwieser Avatar answered Oct 22 '25 06:10

Peter Niederwieser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!