How to filter artifacts on compile configurations?

Hello,

I’m migrating a large enterprise build (ant + ivy) to gradle.
I have some issues on the compile and testCompile classpath where it has duplicated jars (jar + source jar), looking at the existing ant script they were using something like this to “filter” the dependencies on the classpath .

<ivy:cachepath pathid="module.compile.path" conf="runtime" type="xx,jar,xx,xx,zip"/>
<ivy:cachepath pathid="module.test.path" conf="test" type="xx,jar,xx,xx,test"/>

Is there a way I can do the same in gradle? I already tried to do this:

  compile(group: 'xx', name: 'DependencyName', version: '0.0.+', configuration: 'compile(*)') {
    artifact {
      name = 'DependencyName'
      type = 'jar'
    }
  }

but didn’t work, it still includes the source jar in the classpath.

By the way this is the how the DependencyName ivy.xml is defined.

  <publications>
    <artifact name="DependencyName" type="jar" conf="compile"/>
    <artifact name="DependencyName" type="source" ext="jar"/>
    <artifact name="DependencyName" type="javadoc" ext="zip"/>
  </publications>

Thank you!

Using

dependencies {
compile group: ‘xx’, module:‘DependencyName’, version:‘0.0.+’, configuration: ‘anIvyConf’
}

definitely works.
Note that multiple ivy conf is not supported at the moment.
As suggested here, I emitted the possibility of having this included in the short notation, as many ivy.xml of large entreprise artifacts are using the conf attribute to do the sort of things you do as well (sources jar, tests jar, dependOnDependencyFooVersionBar, dependOnDependencyFooVersionBaz, etc etc)

Thank you for your answer Francois.

Sorry but I think my question wasn’t clear, let me try to explain better.
It isn’t about the conf its about the type.

I know using

dependencies {
  compile group: 'xx', module:'DependencyName', version:'0.0.+', configuration: 'anIvyConf'
}

works.

But the problem is that gradle is also including the source jar in the classpath, if I do println compile.asPath it will display the DepdencyName jar twice (because the source jar and the jar itself have the same name).

How can I exclude the source jar from the compile classpath ? The only way to identify the source jar is through the type attribute. As you can see:

<publications>
    <artifact name="DependencyName" type="jar" conf="compile"/>
    <artifact name="DependencyName" type="source" ext="jar"/>
    <artifact name="DependencyName" type="javadoc" ext="zip"/>
</publications>

That’s why I tried to do this:

compile(group: 'xx', name: 'DependencyName', version: '0.0.+', configuration: 'compile(*)') {
    artifact {
      name = 'DependencyName'
      type = 'jar'
    }
  }

To put only the type=‘jar’ in the classpath and not the type=‘source’, but it’s still putting the source jar in the classpath

I hope it is clear now!

Thank you!

Hi Carlos, sorry about the confusion.
I should have read your question more deeply.

I’m using the artifact {} block myself at work.

compile("group:module:version") {
    artifact {
        name = "artiName"
        type = "jar"
    }
}
runtime("group:module:version") {
    artifact {
        name = "artiName2"
        type = "jar"
    }
}

It’s working in my case, but probably because the artifacts’ names are not the same. To be confirmed by a gradle dev, but maybe the type attribute of DependencyArtifact is not well checked.
When you speak about putting the sources in the classpath, you speak about the Gradle Eclipse plugin that do not fill the .classpath file correctly ?

Hi Francois,

No, I’m not using the Gradle Eclipe plugin, I’m only using the java plugin.

In the compile and compileTest tasks, the classpath is wrong leading to some compile errors/test failures. That’s why in the current ant script they do this:

<ivy:cachepath pathid="module.compile.path" conf="runtime" type="xx,jar,xx,xx,zip"/>
<ivy:cachepath pathid="module.test.path" conf="test" type="xx,jar,xx,xx,test"/>

to put in the classpath only the types they want. But I can’t find a way to do the same thing in gradle.

Thanks!