문제

Basically what I would like to do is to define a group of dependencies and then just call a function or something similar in the individual build scripts to add them. Basically like this:

/build.gradle

apply plugin: 'base' // To add "clean" task to the root project.

subprojects {
    apply from: rootProject.file('common-deps.gradle')
}

/settings.gradle

include ":sub-project"

/common-deps.gradle

def addHttpComponents() {
    dependencies {
        compile     group: 'org.apache.httpcomponents', name: 'httpcore',   version: '4.3'
        compile     group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
    }
}

So then if I want to add the HttpComponents to my build. I was hoping to be able just to write my sub-project's build file as:

/sub-project/build.gradle

apply maven
apply java

addHttpComponents()

Is there a way of doing this as the file above fails to run. Or am I going about this the wrong way entirely.

도움이 되었습니까?

해결책

First, why your approach does not work:

apply from: applies a gradle script to a project object. This means you can configure the project in the script, but a local method defined in that script will not magically be available in a different script.

What you CAN do is set your method as an extension property on the project object. Then your method will become available to other scripts via the project object.

change /common-deps.gradle to

ext.addHttpComponents = {
  dependencies {
    compile     group: 'org.apache.httpcomponents', name: 'httpcore',   version: '4.3'
    compile     group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
 }
}

and things should work better.


Next, your approach seems a bit convoluted to me anyway. Why not just:

common-deps.gradle

dependencies {
    compile     group: 'org.apache.httpcomponents', name: 'httpcore',   version: '4.3'
    compile     group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
}

and then in /sub-project/build.gradle

 apply from: rootProject.file('common-deps.gradle')

Lastly, I think you mean

apply plugin: 'maven'
apply plugin: 'java'

in /sub-project/build.gradle

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top