Gradle 和多项目结构 [英] Gradle and Multi-Project structure

查看:29
本文介绍了Gradle 和多项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解我应该如何处理以下项目设置:

I'm trying to understand how should I approach the following project setup:

┌Top Android Project
│
├── Project 1 - (Pure Java Modules)
│    │
│    ├── Module A1
│    ├── Module B1
│    :
│    └── Module Z1
│  
├── Project 2 - (Android Libraries Modules)
│    │
│    ├── Module A2
│    ├── Module B2
│    :
│    └── Module Z2
│  
└── Module - Actual Android Project

在当前的设置中,我在每个模块中都有一个 build.gradle,我真正讨厌这种设置的是 build.gradle 的所有内容在模块之间重复.

In the current setup I have there is a build.gradle in each of the Modules, what I really hate about this setup is that all the content of the build.gradle is duplicated between modules.

事实是,我希望在其中的大多数中使用相同的逻辑,纯 Java 模块"都是基础模块,我希望将输出、JavaDoc 和源代码 Jar,以及部署到某个远程存储库(* 默认).

Fact is that I would like the same logic in most of them, 'Pure Java Modules' are all infra modules, which I would like to Jar the output, the JavaDoc, and sources, and deploy to some remote repository (* default).

另一方面,纯 Java 模块"的一些模块我想有第二个议程,例如,除了默认*构建我想部署一个 jar特定项目或类似项目的依赖项.

On the other hand, some modules of the 'Pure Java Modules' I would like to have a second agenda, for example, aside from the default* build I would like to deploy a jar with dependencies for a specific project, or something like that.

在构建实际Android项目时,我希望模块在默认*构建中编译,最后为我所有的Android项目配置一个默认的build.gradle,这是相当很少,我不想复制那个文件.

While building the Actual Android Project, I would like the modules to compile in the default* build, and finally to configure a default build.gradle for all of my Android projects, which are quite a few, and I would not like to duplicate that file.

================================================================

我想我正在寻找的是类似于 Maven parent pom 的东西,但是由于我不完全了解 Gradle 的工作原理,所以我向你们开放这个以分享您的想法......

I guess what I'm looking for is something like Maven parent pom, but since I don't fully understand how Gradle works, I'm opening this to you guys to share your thoughts...

考虑到复制相同的构建文件是(我敢说)不可接受的,因为我可能想要更改所有模块的所有构建逻辑中的某些内容

Taking under consideration that duplicating same build file is (I dare say) unacceptable, due to the fact that I might want to change something in all the build logic of all the modules

处理这种设置的最佳方法是什么?

What would be the best approach to handle this sort of setup?

推荐答案

对于 http://www.gradle.org/docs/current/userguide/multi_project_builds.html 页面.但是你需要添加

Most of this is pretty normal to the http://www.gradle.org/docs/current/userguide/multi_project_builds.html page. However you are going to need to add

evaluationDependsOn(':project1')
evaluationDependsOn(':project2')

这样 gradle 将在模块之前评估 project1 和 project2.在所有包含代码的项目中,您都需要有一个空的 build.gradle 文件.这也将允许您根据需要自定义项目.

so that gradle will evaluate project1 and project2 before module. In all the projects that contain code you will need to have an empty build.gradle file. This will also allow you to customize a project if needed.

示例: https://github.com/ethankhall/AndroidComplexBuild

在项目的根目录添加一个 build.gradle.所以你需要 4 个包含有用信息的.

Add a build.gradle at the root of your projects. So you need 4 that have useful information in it.

/build.gradle
/settings.gradle
/project1/build.gradle
/project2/build.gradle
/module/build.gradle

在/build.gradle 中放入

in /build.gradle put

dependencies {
    project(":module")
}

在/settings.gradle 中

in /settings.gradle put

include ':module'
include ':project1', ':project1:A1', ':project1:B1', ':project1:Z1'
include ':project2', ':project2:A2', ':project2:B2', ':project2:Z2'

在/project1/build.gradle 中放置

in /project1/build.gradle put

apply plugin: 'java'

subprojects {
    apply plugin: 'java'

    sourceCompatibility = JavaVersion.VERSION_1_6
    targetCompatibility = JavaVersion.VERSION_1_6

    repositories{
        mavenCentral()
    }   

    //Anything else you would need here that would be shared across all subprojects
}

/project2/build.gradle

/project2/build.gradle

buildscript {
    repositories {
        mavenCentral()
    }   

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }   
}

subprojects {
    apply plugin: 'android-library'

    android {
        compileSdkVersion 17
        buildToolsVersion "17.0"
    }   

    sourceCompatibility = JavaVersion.VERSION_1_6
    targetCompatibility = JavaVersion.VERSION_1_6

    repositories{
        mavenCentral()
    }   

    //Anything else you would need here that would be shared across all subprojects
}

在/module/build.gradle 中放置

in /module/build.gradle put

buildscript {
    repositories {
        mavenCentral()
    }   

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }   
}

evaluationDependsOn(':project1')
evaluationDependsOn(':project2')

apply plugin: 'android'

android {
    compileSdkVersion 17
    buildToolsVersion "17.0"
}

dependencies {
    compile project(":project1:A1")
    compile project(":project1:B1")
    compile project(":project1:Z1")

    compile project(":project2:A2")
    compile project(":project2:B2")
    compile project(":project2:Z2")
}

这篇关于Gradle 和多项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆