2017-02-20 124 views
0

我有以下設置:搖籃調用不同的任務,每個子項目

項目被打包成jar

  • Android項目

      • 春天啓動的應用程序
      • java類
      • Android項目的應用程序模塊

    我正在嘗試設置gradle構建,以便獲取可運行的spring引導jar應用程序,安裝在本地maven repo中的java jar庫以及android的apk文件。

    • 爲了得到一個apk文件,我需要運行assembleDebug任務
    • 要發佈的Java ibrary我需要運行發佈任務
    • 讓Spring可執行我需要運行compileJave任務

    問題是什麼任務在頂級根目錄gradel.build文件上運行以完成不同的任務?

    的gradle根:

    allprojects { 
        task hello << { task -> println "I'm $task.project.name" } 
    } 
    subprojects { 
        repositories { 
         mavenCentral() 
         jcenter() 
        } 
    } 
    

    春天的Gradle啓動:

    apply plugin: 'java' 
    apply plugin: 'eclipse' 
    apply plugin: 'org.springframework.boot' 
    
    defaultTasks 'clean', 'build' 
    
    buildscript { 
        ext { 
          springBootVersion = '1.5.1.RELEASE' 
         } 
         repositories { 
          mavenCentral() 
         } 
        dependencies { 
         classpath("org.springframework.boot:spring-boot-gradle-   plugin:${springBootVersion}") 
        } 
    } 
    
    jar { 
        baseName = 'demo' 
        version = '0.0.1-SNAPSHOT' 
    } 
    
    sourceCompatibility = 1.8 
    
    repositories { 
        mavenCentral() 
    } 
    
    dependencies { 
        compile('org.springframework.boot:spring-boot-starter-security') 
        compile('org.springframework.boot:spring-boot-starter-web') 
        compile('org.springframework.boot:spring-boot-starter-data-jpa') 
        compile('io.jsonwebtoken:jjwt:0.7.0') 
        compile('mysql:mysql-connector-java:6.0.5') 
    
        testCompile('org.springframework.boot:spring-boot-starter-test') 
    } 
    

    凸出的Java的gradle:

    jar { 
        baseName = 'commonobjects' 
        version = "0.0.1-SNAPSHOT" 
    } 
    
    sourceCompatibility = 1.8 
    targetCompatibility = 1.8 
    
    dependencies { 
        testCompile group: 'junit', name: 'junit', version: '4.11' 
    } 
    

    凸出的Android的gradle:

    buildscript { 
        repositories { 
          jcenter() 
         mavenCentral() 
         } 
        dependencies { 
         classpath 'com.android.tools.build:gradle:2.2.3' 
        } 
    } 
    

    Android應用程序的gradle產出:

    def ANDROID_SUPPORT_DEPENDENCY_VERSION = '25.1.0' 
    def DAGGER_DEPENDENCY_VERSION = '2.8' 
    def OK_HTTP_DEPENDENCY_VERSION = '3.5.0' 
    def RETROFIT_DEPENDENCY_VERSION = '2.1.0' 
    def RETROFIT_JACKSON_DEPENDENCY_VERSION = '2.1.0' 
    def BUTTER_KNIFE_DEPENDENCY_VERSION = '8.5.1' 
    def JAVAX_ANNOTATION_JSR250_API_DEPENDENCY_VERSION = '1.0' 
    def GREEN_ROBOT_EVENT_BUS_DEPENDENCY_VERSION = '3.0.0' 
    def RX_JAVA_DEPENDENCY_VERSION = '2.0.5' 
    def RX_ANDROID_JAVA_DEPENDENCY_VERSION = '2.0.1' 
    
    defaultTasks 'clean', 'assembleDebug' 
    
    buildscript { 
        repositories { 
         mavenCentral() 
         jcenter() 
         maven { 
          url "https://plugins.gradle.org/m2/" 
         } 
        } 
        dependencies { 
         classpath "net.ltgt.gradle:gradle-apt-plugin:0.9" 
        } 
    } 
    
    repositories { 
        jcenter() 
        mavenCentral() 
    } 
    
    apply plugin: "com.android.application" 
    apply plugin: 'idea' 
    
    
    android { 
        compileSdkVersion 25 
        buildToolsVersion "25.0.1" 
    
        defaultConfig { 
         applicationId "..." 
         minSdkVersion 14 
         targetSdkVersion 25 
         versionCode 1 
         versionName "1.0" 
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
        } 
        buildTypes { 
    
    //  debug { 
    //   buildConfigField "String", "PARSE_APPLICATION_ID", "\"1\"" 
    //   buildConfigField "String", "PARSE_API_KEY", "\"1\"" 
    //  } 
    
         release { 
          minifyEnabled true 
          proguardFiles "android-proguard-android.txt", "proguard-rules.txt" 
    
          // buildConfigField "String", "PARSE_APPLICATION_ID", "\"1\"" 
          // buildConfigField "String", "PARSE_API_KEY", "\"1\"" 
         } 
        } 
    
        packagingOptions { 
         pickFirst 'META-INF/LICENSE' 
        } 
    } 
    
    dependencies { 
        // Add jars supplied 
        compile fileTree(dir: 'libs', include: ['*.jar']) 
    
        // Test related 
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
         exclude group: 'com.android.support', module: 'support-annotations' 
        }) 
        testCompile 'junit:junit:4.12' 
    
        androidTestCompile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}" 
        androidTestCompile "com.android.support.test:runner:0.5" 
    
        // Android support libraries 
        compile "com.android.support:appcompat-v7:${ANDROID_SUPPORT_DEPENDENCY_VERSION}" 
        compile "com.android.support:design:${ANDROID_SUPPORT_DEPENDENCY_VERSION}" 
        compile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}" 
    
        // An HTTP & HTTP/2 client for Android and Java applications 
        compile "com.squareup.okhttp3:okhttp:${OK_HTTP_DEPENDENCY_VERSION}" 
    
        // Retrofit: A type-safe HTTP client for Android and Java 
        compile "com.squareup.retrofit2:retrofit:${RETROFIT_DEPENDENCY_VERSION}" 
        compile "com.squareup.retrofit2:converter-jackson:${RETROFIT_JACKSON_DEPENDENCY_VERSION}" 
    
        // Butterknife: Field and method binding for Android views 
        compile "com.jakewharton:butterknife:${BUTTER_KNIFE_DEPENDENCY_VERSION}" 
        annotationProcessor "com.jakewharton:butterknife-compiler:${BUTTER_KNIFE_DEPENDENCY_VERSION}" 
    
        // Dagger DI 
        compile "com.google.dagger:dagger:${DAGGER_DEPENDENCY_VERSION}" 
        annotationProcessor "com.google.dagger:dagger-compiler:${DAGGER_DEPENDENCY_VERSION}" 
        provided "javax.annotation:jsr250-api:${JAVAX_ANNOTATION_JSR250_API_DEPENDENCY_VERSION}" 
    
        // Event bus 
        compile "org.greenrobot:eventbus:${GREEN_ROBOT_EVENT_BUS_DEPENDENCY_VERSION}" 
    
        // RxJava a library for composing asynchronous and event-based programs by using observable sequences. 
        compile "io.reactivex.rxjava2:rxjava:${RX_JAVA_DEPENDENCY_VERSION}" 
        compile "io.reactivex.rxjava2:rxandroid:${RX_ANDROID_JAVA_DEPENDENCY_VERSION}" 
    } 
    
  • 回答

    1

    您可以簡單地創建你的任務,這將取決於三個不同的任務。這可能有跨子項目的依賴,以及:

    task myAllTask { 
    } 
    
    myAllTask.dependsOn assembleDebug, publish, compileJava 
    

    對於來自子項目正在運行的任務:

    myAllTask.dependsOn ':bootProj:assembleDebug', ':javaProj:publish', ':androidProj:compileJava 
    
    +0

    這樣我就可以運行根文件夾gradle這個myAllTask​​。 build.gradle會包含什麼代碼?另一件事是順序也很重要,我必須首先發布javaProj,因爲它是服務器和Android應用程序的依賴項。 @ sm4 –

    +0

    也許試着在你的問題上更具體。草擬您的gradle文件的組織方式。根Gradle將包含任務{},然後包含dependsOn。您還應該依賴於需要在依賴項塊中首先運行的子項目。 – sm4

    +0

    我已經添加了我目前的來源,我的問題確實是工資,因爲我可能會在Gradle中做多個錯誤的事情。但目的是從所有模塊生成所有工件,然後進一步使用它們來上傳,部署等。我也不太明白爲什麼android項目必須被包裝到android模塊中,而不是僅僅具有作爲主要android模塊的應用程序。 @ SM4 –