如何删除依赖项和存储库的重复定义?

时间:2015-12-21 16:49:32

标签: gradle

我有一个构建脚本,它具有存储库和依赖项的重复:

apply plugin: 'groovy'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.codehaus.groovy:groovy-all:2.0.5'
        classpath 'org.apache.knox:gateway-shell:0.6.0'
        classpath 'commons-io:commons-io:2.4'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.0.5'
    compile 'org.apache.knox:gateway-shell:0.6.0'
    compile 'commons-io:commons-io:2.4'
}

如果我注释掉其中任何一个部分,我会收到构建错误。有没有办法使用存储库和依赖项的单一定义?

注意:构建脚本的其余部分......

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

sourceSets {
    main {
        groovy {
            srcDirs = ['scripts']
        }
    }
}

/* Task to run script */
tasks.addRule("Pattern: filename.groovy") { String taskName ->

    if (taskName.endsWith('.groovy')) {
        FileTree tree = fileTree('./scripts/').include('**/*.groovy')
        tree.each {File file ->
            if (file.absolutePath.endsWith(taskName)) {

                task(taskName, dependsOn:'classes', type: JavaExec) {

                    Properties props = new Properties()
                    props.load(new FileInputStream("$projectDir/connection.properties"))

                    environment 'gateway', props.gateway
                    environment 'username', props.username
                    environment 'password', props.password

                    // the classname is the scriptname minus the extension
                    main = org.apache.commons.io.FilenameUtils.getBaseName(taskName)
                    classpath = sourceSets.main.runtimeClasspath
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

如果你有一个多模块项目,你可以添加到root gradle buildscript

allprojects {
    repositories {
        jcenter() or mavenCentral()
    }
}