Dependencies on projects with artifact configurations


(Michael Brand) #1

I have a multi-project scenario where one subproject generates multiple jars. I want to set up dependencies so that a second project is able to depend on just one jar in the multipi-jar subproject. It looks like Gradle handles this nicely, but I can’t get the syntax to work.

Here’s a sample:

apply plugin: 'java'
  dependencies {
 compile project(path: "subproject", configuration: "config1")
 }
  project('subproject') {
 configurations {
  config1
  config2
 }
   task createArtifact1(type: Jar) {
  from('subproject')
 }
   task createArtifact2(type: Jar) {
  from('subproject')
 }
   artifacts {
  config1 createArtifact1
  config2 createArtifact2
 }
}

When I run this I get the following results:

A problem occurred evaluating root project 'artifactTest'.
> Could not resolve all dependencies for configuration ':compile'.
   > Module version group:, module:artifactTest, version:unspecified, configuration:compile declares a dependency on configuration 'config1' which is not declared in the module descriptor for group:artifactTest, module:subproject, version:unspecified

What am I doing wrong?


(Luke Daley) #2

I think you’re hitting GRADLE-2713 (which is particularly nasty).

This happens when you end up with a project dependency on something that has the same name/group as a “normal” dependency.

Could this be the case?


(Michael Brand) #3

I’m not sure I understand GRADLE-2713, but I’ve renamed everything to see if it resolves it:

apply plugin: 'java'
  dependencies {
 compile project(path: "sp1", configuration: "myConfig1")
 }
  println configurations.compile.asPath
  project('sp1') {
 configurations {
  myConfig1
  myConfig2
 }
   task createArtifact1(type: Jar) {
  from('sp1files')
 }
   task createArtifact2(type: Jar) {
  from('sp1files')
 }
   artifacts {
  myConfig1 generateSp1Config1
  myConfig2 generateSp1Config2
 }
}

I get the same error:

FAILURE: Build failed with an exception.
  * Where:
Build file '/Users/mbrand/tmp/artifactTest/build.gradle' line: 7
  * What went wrong:
A problem occurred evaluating root project 'artifactTest'.
> Could not resolve all dependencies for configuration ':compile'.
   > Module version group:, module:artifactTest, version:unspecified, configuration:compile declares a dependency on configuration 'myConfig1' which is not declared in the module descriptor for group:artifactTest, module:sp1, version:unspecified
  * Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  BUILD FAILED
  Total time: 3.166 secs

If I haven’t addressed this correctly could you provide an example of what I can do to get this working?


(Luke Daley) #4

I don’t know exactly what is going wrong.

Can you provide an executable project that I can run that has this behaviour?


(Michael Brand) #5

I found it. The subproject did not have the line “apply plugin: ‘java’”. The error messages were not intuitive and were different depending on whether I used the configuration or not.

Here’s an example of the working code:

apply plugin: 'java'
  defaultTasks = ['tasks']
  configurations {
 myConfig1
}
  project('sp1') {
 apply plugin: 'java'
 configurations {
  myConfig1
  myConfig2
 }
   task generateSp1Config1(type: Jar) {
  from('sp1files')
 }
   task generateSp1Config2(type: Jar) {
  from('sp1files')
 }
   artifacts {
  myConfig1 generateSp1Config1
  myConfig2 generateSp1Config2
 }
}
  dependencies {
 compile project(path: ":sp1" , configuration: "myConfig1")
 }

(Michael Brand) #6

Thanks for your help, Luke. I was going to upload a zip in case you needed it, but it looks like more work than it’s worth. Let me know if you’d like to see it after all.