title: In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?
body: <p>In Gradle, how can I generate a POM file with dynamic dependencies resolved to the actual version used?</p>
  
  <pre><code>dependencies {
      testCompile(group: 'junit', name: 'junit', version: '4.+')
  }
  </code></pre>
  
  <p>This is generated from the dependency above.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.+&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>I want to have the <code>+</code> resolved to an accrual version like below.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.12&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>The Gradle guide chapter on <a href="http://www.gradle.org/docs/current/userguide/publishing_maven.html">Maven Publishing</a> talks about doing this, but does not mention how. </p>
  
  <blockquote>
    <p>With this hook, you can modify any aspect of the POM. For example, you could replace the version range for a dependency with the actual version used to produce the build. </p>
  </blockquote>
  
  <h2>Solution</h2>
  
  <p>Using the information in Peter Niederwieser's answer, I created a task that reads a POM that contains dynamic dependencies and overwrites it with a new pom that has the dependencies resolved. </p>
  
  <pre><code>/**
   * Reads and Overwrites POM file resolving dynamic dependencies
   */
  task cleanPom(dependsOn: writeNewPom) &lt;&lt; {
      // Get existing pom file
      Node xml = new XmlParser().parse(pomFileLocation)
  
      // Generate map of resolved versions
      Map resolvedVersionMap = new HashMap()
      Set&lt;ResolvedArtifact&gt; resolvedArtifacts = configurations.compile.getResolvedConfiguration().getResolvedArtifacts()
      resolvedArtifacts.addAll(configurations.testCompile.getResolvedConfiguration().getResolvedArtifacts())
      resolvedArtifacts.each {
          resolvedVersionMap.put(it.getName(), it.getModuleVersion().getId().getVersion())
      }
  
      // Update dependencies with resolved versions
      xml.dependencies.first().each {
          Node artifactId = it.get("artifactId").first()
          def artifactName = artifactId.value().first()
          def artifactVersion = resolvedVersionMap.get(artifactName)
  
          Node version = it.get("version").first()
          version.value = artifactVersion
      }
  
      // Overwrite existing pom file
      new XmlNodePrinter(new PrintWriter(new FileWriter(pomFileLocation))).print(xml)
  }
  </code></pre>