Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling missing configuration in dependency in Gradle build

Tags:

gradle

I have a Gradle build which produces the main deliverable artifact (an installer) of my product. The Gradle project which models this has a number of different dependencies in different configurations. Many of those dependencies are on the default configuration of external modules, and some of those modules have a testResults configuration that contains the (zipped) results of the test task.

It's important that those test results for all dependencies, where they exist, be published as artifacts of the main product build (to use as evidence that testing took place and was successful). It's not an issue if they don't exist.

I tried to do this by iterating over all configurations of the product build, iterating over the dependencies in each and adding a programmatically created dependency (in a new configuration created for this purpose) on the testResults configuration of the module.

In other words, I create dependencies like this:

def processDependencyForTests( Dependency dependency ) {

    def testResultsDependency = [
      'group' : dependency.group,
      'name' : dependency.name,
      'version' : dependency.version,
      'configuration' : 'testResults'
      ]

    project.dependencies.add 'allTestResults', testResultsDependency

This populates that configuration just fine, but of course when I try to do anything with it, it fails the first time I encounter a dependency on a module that doesn't actually have a testResults configuration:

   def resolvedConfiguration = configurations.allTestResults.resolvedConfiguration

Results in this:

Build file 'C:\myproduct\build.gradle' line: 353

* What went wrong:
Execution failed for task ':myproduct:createBuildRecord'.
> Could not resolve all dependencies for configuration ':myproduct:allTestResults'.
   > Module version group:mygroup, module:myproduct, version:1.2.3.4, configuration:allTestResults declares a dependency on configuration 'testResults' which is not declared in the module descriptor for group:mygroup, module:mymodule, version:1.0

It's not really practical to instead explicitly list the dependencies in a declarative fashion, because I want them to be derived from "whatever real dependencies the product project has".

How can I ensure that such expected missing configurations don't derail my build? I thought something to do with lenient configurations might be the answer, but I haven't even got that far here (I need to get a ResolvedConfiguration first, as far as I can tell). Alternatively, if the way I'm doing this is insane, what's a more natural Gradle idiom to achieve this?

like image 319
Andy McKibbin Avatar asked Mar 07 '13 17:03

Andy McKibbin


People also ask

Can't resolve all dependencies for configuration Gradle?

This is because of slow internet connection or you haven't configure proxy settings correctly. Gradle needs to download some dependencies , if it cant access the repository it fires this error. All you have to do is check your internet connection and make sure gradle can access the maven repository. Save this answer.

How do I resolve Gradle dependencies?

Given a required dependency, with a version, Gradle attempts to resolve the dependency by searching for the module the dependency points at. Each repository is inspected in order. Depending on the type of repository, Gradle looks for metadata files describing the module ( .

How do you update dependencies in build Gradle?

Perform a Gradle sync/reloadThe file versions. properties is part of the Gradle build. Consequently, after you have edited it, you need to ensure the IDE gets the changes. Android Studio: Run the “Sync Project with Gradle Files” action (via ctrl / cmd + shift + A ), or click the elephant + arrow icon in the toolbar.


1 Answers

You'll need to check for the existence of the configuration before referencing it. In cases like this, the gradle DSL documentation is your friend. In fact, the gradle project is one of the most well-documented open source projects I've ever worked with.

Here, you'll find that configurations is simply a container of configuration objects. They are instances of ConfigurationContainer and Configuration respectively. Knowing this, all you need to do is to check whether the configurations container contains a configuration named "testResults".

This can be achieved by the following code:

if (configurations.find { it.name == 'testResults' }) {
    // do your stuff
}
like image 124
Steinar Avatar answered Oct 20 '22 15:10

Steinar