Ivy publication with custom configuration

I have two dependencies for my project bar foo-api.jar and foo-impl.jar

that I use like that

dependencies {
 compile("group:foo:version") {
  artifact {
   name = "foo-api"
    type = "jar"
   }
 }
 runtime("group:foo:version") {
  artifact {
   name = "foo-impl"
    type = "jar"
   }
 }
}

As you see, I only need foo-api to compile, but I need foo-impl to run.

When using the ivy-publish plugin, I would like to publish bar with the following ivy.xml

<ivy-module version="2.0">
  <info organisation="group" module="bar" revision="version" status="integration" publication="20150211154334"/>
  <configurations>
    <conf name="default" visibility="public" extends="runtime"/>
     <conf name="compile" visibility="public"/>
    <conf name="runtime" visibility="public"/>
  </configurations>
  <publications>
    <artifact name="bar" type="jar" ext="jar"/>
  </publications>
  <dependencies>
    <dependency org="group" name="foo" rev="version" conf="compile">
      <artifact name="foo-api" type="jar"/>
    </dependency>
    <dependency org="group" name="foo" rev="version" conf="runtime"/>
      <artifact name="foo-impl" type="jar"/>
    </dependency>
  </dependencies>
</ivy-module>

Basically, if I call bar from another project with

runtime "group:bar:version"

I want to retrieve the foo-impl jar dependency. If I call bar from another project with

compile "group:bar:version"

I want to retrieve the foo-api jar dependency.

I tried something like

ivy(IvyPublication) {
      configurations {
          compile{}
          runtime{}
      }
    }

and I would have expected that my dependencies for the “compile” configuration would have the “conf=‘compile’” attribute etc etc, but this does not produced the above ivy.xml. Is it possible with the current ivy-publish plugin ?

Well, I re-read https://gradle.org/docs/current/userguide/publishing_ivy.html carefully. The only example to set the dependencies of a published artifact is with using ‘from components.java’, which includes the runtime dependencies.

If the publication does not rely on ‘components.java’ but only on custom artifacts like

artifact(sourceJar) {
  type "source"
  conf "runtime"
}

how can we define the dependencies to be added in the generated descriptor file ?