File dependencies not working


(Steve Cohen) #1

I’m trying to set up a composite build but not getting far. I can’t even build project producer and project consumer separately where project producer places its output somewhere and project consumer tries to read it.

I’m trying to use file dependencies in a custom configuration following the user’s guide model:

        project.configurations.create('natives')
        
        project.dependencies {
            natives files("${pathToNativeTarfile"})
        }

The error message doesn’t like this and I can’t figure out why.
pathToNativeTarFile is being resolved correctly but I get this error message:

Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method files() for arguments [path/to/native/tarfile] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Except for not using one of the predefined configurations, I’m doing this just like the User’s Guide suggests, unless I miss something. Can someone explain?


(Steve Cohen) #2

As usual, a mistake when going from Gradle docs to plugin code. Gradle docs assume dsl. In dsl method names are evaluated against the project. in plugin code you have to specify the project. This works:

     project.configurations.create('natives')
    
    project.dependencies {
        natives project.files("${pathToNativeTarfile"})
    }

(Lance Java) #3

You can project.with { ... } and use the exact same code in your plugin as you do in build.gradle.

project.with {
   configurations.create('natives')
   dependencies {
        natives files("${pathToNativeTarfile"})
    }
} 

(Steve Cohen) #4

Hmm. Thanks for that interesting tip. I suppose this means you could do:

void apply(Project project) {
   project.with {
  // whatever
  }
} 

or is there some other apply code that would not work inside those brackets?


(Lance Java) #5

Correct

AFAIK everything will work


(Steve Cohen) #6

Thanks. This works great. It is a good tip for plugin development and should be better documented, perhaps in Chapter 40 “Writing Custom Plugins”. It doesn’t appear in the Gradle API docs because with is a feature of the Groovy language, not of Gradle itself, but its use makes the porting of code snippets from Gradle examples (written for buildscripts) to plugins much easier.


(Lance Java) #7

Agreed that this should get a mention in the plugin documentation. It’s common for logic to start in build.gradle and be refactored into a plugin and this trick means no tweaking is necessary