gradle uploadArchives用于具有不同maven组和版本的多个子项目

时间:2016-04-23 15:26:37

标签: gradle

我有以下build.gradle,它适用于单个子项目(如果我复制粘贴它,则适用于所有子项目):

def group = 'com.my.pgk'
def artifact = project.name
def version = '1.0.0'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

uploadArchives {
    repositories {
        mavenDeployer {
            def finalPath = 'file://' + LocalMavenRepoPath.toString()
            println group
            println finalPath
            repository url: finalPath
            pom.groupId = group
            pom.artifactId = artifact
            pom.version = version
            pom.packaging = 'jar'
        }
    }
}

jar {
    archiveName = artifact + "-" + version + ".jar"
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    archiveName = artifact + "-v" + version + "-src.jar"
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    archiveName = artifact + "-v" + version + "-doc.jar"
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

这正是我想要的......

尝试将其移动到根项目build.gradle不会,因为调用uploadArchives时子项目中定义的变量不会更新。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要首先配置子项目,以便gradle在配置root build.gradle之前知道子项目变量。您可以使用以下方法强制自下而上配置:

evaluationDependsOnChildren()

答案 1 :(得分:0)

在具有单个根和两个子项目的项目中,您可以在根项目中创建公共gradle脚本并将其应用于每个子项目。

根项目: 的build.gradle

//nothing to see here

Root Project:common.gradle

if (!project.hasProperty("commonVar")) {
    ext.commonVar = "unset"
}


task printCommonVar() {
    println commonVar
}

Root Project:settings.gradle

rootProject.name = 'GradleGroupProject'

// Find the directories containing a "build.gradle" file in the root directory
// of the project. That is, every directory containing a "build.gradle" will
// be automatically the subproject of this project.

def subDirs = rootDir.listFiles(new FileFilter() {
    public boolean accept(File file) {
        if (!file.isDirectory()) {
            return false
        }
        if (file.name == 'buildSrc') {
            return false
        }
        return new File(file, 'build.gradle').isFile()
    }
});

subDirs.each { File dir ->
    include dir.name
}

子项目1:build.gradle

ext.commonVar = "subproject1"
apply from: rootProject.file('common.gradle')

子项目2:build.gradle

ext.commonVar = "subproject2"
apply from: rootProject.file('common.gradle')

ext.commonVar = "subproject2"apply from: rootProject.file('common.gradle')之间的排序很重要。