首页 文章

如何组织Gradle for Spring,Android和Plain JAR?

提问于
浏览
1

我想要一个包含3个子模块的项目如下:

proj:spring
proj:plainjava
proj:android
proj:android:app

其中plainjava是安装在本地存储库中的jar . spring android:app依赖于它 .

目前,如果我尝试从根目录运行gradle或将其导入ide,则会导致各种错误 .

Execution failed for task ':android:app:compileDebugJavaWithJavac'.

为android构建单独的任务构建正常 .

正如我所知,我需要按以下顺序执行整个构建的以下任务:

  • proj:plainjava:publishMavenJavaPublicationToMavenLocal

  • proj:spring:bootRepackage

  • ???任务要成功构建android,目前在android studio中明确构建 .

出于开发目的,我还需要自己执行每个任务 . 运行

proj:spring:bootRepackage

以某种方式触发android,app和plainjava的依赖解析

实际文件:

proj settings.gradle

include 'commonobjects', 'server', 'android', 'android:app'

proj build.gradle,具有允许ci构建工件的特定自定义任务 .

task myCleanLibraries {}
myCleanLibraries.dependsOn ':commonobjects:clean'

task myPublishLibraries {}
myPublishLibraries.dependsOn ':commonobjects:publishToMavenLocal'

task myCleanRest {}
myCleanRest.dependsOn ':server:clean', ':android:app:clean'

task myBuildRest {}
myBuildRest.dependsOn ':server:build', ':android:app:assembleDebug'


task myLibraries {}
myLibraries.dependsOn 'myCleanLibraries', 'myPublishLibraries'
myPublishLibraries.mustRunAfter 'myCleanLibraries'

task myRest {}
myRest.dependsOn 'myCleanRest', 'myBuildRest'
myBuildRest.mustRunAfter 'myCleanRest'

task myAll {}
myAll.dependsOn 'myLibraries', 'myRest'
myRest.mustRunAfter 'myLibraries'

proj:spring build.gradle

group 'com.example'
version '0.0.VERSIONNUMBER'

apply plugin: 'java'
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 = 'server'
    version = '0.0.9999'
}

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    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 'org.springframework.boot:spring-boot-starter-logging'
    compile 'org.projectlombok:lombok'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile 'mysql:mysql-connector-java:6.0.5'
    compile 'com.example:commonobjects:0.0.9999'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

proj:commonobjects settings.gradle

rootProject.name = 'commonobjects'

proj:commonobjects build.gradle

group 'com.example'
version '0.0.9999'

apply plugin: 'java'
apply plugin: 'maven-publish'

jar {
    baseName = 'commonobjects'
    version = '0.0.9999'
}


repositories {
    mavenCentral()
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        mavenLocal()
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile 'junit:junit:4.11'
}

proj:android settings.gradle

include ':app'

proj:android build.gradle

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

proj:android:app build.gradle

def ANDROID_SUPPORT_DEPENDENCY_VERSION = '25.1.0'
def DAGGER_DEPENDENCY_VERSION = '2.10'
def OK_HTTP_DEPENDENCY_VERSION = '3.7.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"
        classpath "com.android.tools.build:gradle:2.3.1"
    }
}

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
}

apply plugin: "com.android.application"
apply plugin: "idea"


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "myandroidappid"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 9999
        versionName "0.0.9999"
        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.google.code.findbugs:jsr305:3.0.1'
    androidTestCompile "com.android.support:support-annotations:${ANDROID_SUPPORT_DEPENDENCY_VERSION}"
    androidTestCompile "com.android.support.test:runner:0.5"

    // MYOSCA dependencies
     compile('com.example:commonobjects:0.0.9999')

    // 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}"
    compile "com.google.dagger:dagger-android:${DAGGER_DEPENDENCY_VERSION}"
    annotationProcessor "com.google.dagger:dagger-android-processor:${DAGGER_DEPENDENCY_VERSION}"
    compile "com.google.dagger:dagger-android-support:${DAGGER_DEPENDENCY_VERSION}"

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

    // Google Guava for preconditions
    compile 'com.google.guava:guava:20.0'
}

1 回答

  • 1

    完整解决方案:repo

    感谢您发布 build.gradle 文件 . 我相信你的`settings.gradle是你应该开始的地方 .

    由于您希望/在当前设置中拥有这些模块:

    proj:spring
    proj:plainjava
    proj:android
    proj:android:app
    

    让我们将它们映射到gradle模块:

    proj/
      |-spring/
         |-build.gradle
      |-plain-java/        <-- better naming conventions by separating names
         |-build.gradle
      |-android/
         |-build.gradle
      |-build.gradle       <-- make sure to have root build.gradle
      |-settings.gradle    <-- should be root folder(proj)
    

    让我们做以下事情:

    • 将文件夹"plainjava"重命名为"plain-java"

    • 让"android/app"文件夹合并为"android"

    • 这让我们将所有内容整合到3个项目中

    settings.gradle

    rootProject.name = "proj"
    
    include ":spring"
    include ":plain-java"
    include ":android"
    

    最后,在你的根目录中,移动你的 buildscript 依赖项,这样你就可以从那个目录构建 .

相关问题