2016-04-23 103 views
1

多個子項目gradle這個uploadArchives我有以下的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時子項目中定義的變量未更新。

我該如何解決這個問題?

+0

當您將uploadArchives移到一個子項目部分中時,是否將其移至根build.gradle? – RaGe

+0

當然可以...... – TacB0sS

回答

1

您需要首先配置子項目,以便Gradle在配置root build.gradle之前知道子項目變量。您可以通過使用強制自下而上的配置:

evaluationDependsOnChildren() 
+0

我應該在哪裏放置它?另外我讀過[另一個線程](http://stackoverflow.com/questions/14656389/gradle-use-variables-in-parent-task-that-are-defined-in-child),它是最好的用這個作爲最後的手段,那麼還有另外一種方法嗎? – TacB0sS

+0

直接進入您的根build.gradle(在零縮進級別)。 Gradle默認評估順序是首先評估根項目。我同意扭轉秩序並不理想。它可能會破壞腳本中需要首先評估的腳本的其他部分 - 但我在實踐中沒有看到使用evaluationDependsOnChildren的主要問題。 – RaGe

+0

我強烈建議你移動到更新的'maven-publish'插件而不是'maven'插件 - 它會更好地處理髮布配置。 – RaGe

0

在一個項目中有一個根和兩個子項目,您可以創建在根項目共同gradle這個腳本,並將其應用到各個子項目。

根項目: 的build.gradle

//nothing to see here 

根項目:common.gradle

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


task printCommonVar() { 
    println commonVar 
} 

根項目: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')之間的順序很重要。

相關問題