【问题标题】:Gradle invoking different tasks for each subprojectGradle 为每个子项目调用不同的任务
【发布时间】:2017-02-20 13:12:52
【问题描述】:

我有以下设置:

项目

  • 春季启动应用程序
  • 打包到 jar 中的 java 类
  • 安卓项目
    • android项目的app模块

我正在尝试设置 gradle build,以便获得可运行的 spring boot jar 应用程序、安装在本地 maven repo 中的 java jar 库和 android 的 apk 文件。

  • 要获取 apk 文件,我需要运行 assembleDebug 任务
  • 要发布 java 库,我需要运行发布任务
  • 要获得 spring 可执行文件,我需要运行 compileJave 任务

问题是在顶级根gradel.build 文件上运行什么任务来完成不同的任务?

根等级:

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

Spring Boot 分级:

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 Proj 毕业:

jar {
    baseName = 'commonobjects'
    version = "0.0.1-SNAPSHOT"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

Android Proj 分级:

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

Android 应用分级:

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}"
}

【问题讨论】:

    标签: java android gradle spring-boot


    【解决方案1】:

    您可以简单地创建依赖于三个不同任务的任务。子项目之间也可能存在依赖关系:

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

    从子项目运行任务:

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

    【讨论】:

    • 所以我可以在根文件夹中运行 gradle myAllTask​​ 。 build.gradle 将包含哪些代码?另一件事是顺序也很重要,我必须先发布 javaProj,因为它是服务器和 android 应用程序的依赖项。 @sm4
    • 也许尝试在您的问题中更具体。起草你的 gradle 文件的组织方式。根 gradle 将包含任务 {},然后是dependsOn。您还应该对需要在依赖项块中首先运行的子项目具有依赖项。
    • 我已经添加了我当前的来源,我的问题确实是工资,因为我可能在 gradle 中做错了很多事情。但目的是从所有模块生成所有工件,然后进一步使用它们来上传、部署等。我也不太明白为什么 android 项目必须被包装到 android 模块中,而不是仅仅将应用程序作为主要的 android 模块。 @sm4
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2019-07-23
    • 2020-08-04
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多