Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: How do I include ivy artifacts by pattern?

In Ivy I can have dependency declared like this:

<dependency org="org" name="module_name" rev="12" conf="conf_name->*">
  <include name="foo(.*)-bar" ext="zip" matcher="exactOrRegexp"/>
</dependency>

which will download all matching files.

How can I define similar (regex-based) dependency in Gradle?

like image 902
Nikita Skvortsov Avatar asked Jun 11 '15 17:06

Nikita Skvortsov


1 Answers

After some more trial-and-error, I was able to extend Gradle to resolve dependencies with following syntax:

dependencies {
    compile "org:module_name:12" {
        artifact {
            name "foo.*-bar"
            type "zip"
        }
    }
}

For this, one will need project evaluation listener, that will post-process the dependencies. Resolve ivy descriptor for each dependency, parse it, match the artifact names, update the dependency's artifacts descriptors (remove the one with pattern in name and insert matched artifacts with names).

Pros:

  • Properly uses Gradle's artifacts cache.
  • Avoids transfer of exra (not matched) artifacts.
  • Dependencies resolution mechanics is applied.

Pitfalls found during implementation:

  • Copy a configuration before resolving ivy descriptors. Resolved configuration (with dependencies) is considered immutable and wont be resolved again, so matched artifacts will not be downloaded

  • Matching different entities. After an Ivy descriptor is "resolved" and downloaded, it is somewhat tricky to match it with unresolved dependency (to update artifact descriptors), as resolved entity has different type. So far, matching on "group-artifact-version" coordinates works, but it is fragile solution.

A sample code for dependency processor can be found on GitHub (disclamer: provided "as is", no gurantees and responsibilities. But if it blows your project's working copy, please let me know)

like image 88
Nikita Skvortsov Avatar answered Oct 02 '22 16:10

Nikita Skvortsov