title: Handling missing configuration in dependency in Gradle build
body: <p>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 <code>testResults</code> configuration that contains the (zipped) results of the test task.</p>
  
  <p>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.</p>
  
  <p>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 <code>testResults</code> configuration of the module.</p>
  
  <p>In other words, I create dependencies like this:</p>
  
  <pre><code>def processDependencyForTests( Dependency dependency ) {
  
      def testResultsDependency = [
        'group' : dependency.group,
        'name' : dependency.name,
        'version' : dependency.version,
        'configuration' : 'testResults'
        ]
  
      project.dependencies.add 'allTestResults', testResultsDependency
  </code></pre>
  
  <p>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 <strong>have</strong> a <code>testResults</code> configuration:</p>
  
  <pre><code>   def resolvedConfiguration = configurations.allTestResults.resolvedConfiguration
  </code></pre>
  
  <p>Results in this:</p>
  
  <pre><code>Build file 'C:\myproduct\build.gradle' line: 353
  
  * What went wrong:
  Execution failed for task ':myproduct:createBuildRecord'.
  &gt; Could not resolve all dependencies for configuration ':myproduct:allTestResults'.
     &gt; 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
  </code></pre>
  
  <p>It's not really practical to instead explicitly list the dependencies in a declarative fashion, because I want them to be derived from "whatever <strong>real</strong> dependencies the product project has".</p>
  
  <p>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 <code>ResolvedConfiguration</code> 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?</p>