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

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

问题描述

我想知道我应该如何处理以下项目设置:

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,和来源,以及部署到一些偏远库(*默认)。

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模块我想有第二个议程,例如,除了默认的*构建我想部署一个罐子依赖于具体的项目,或者类似的东西。

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项目,我想这些模块在默认*构建编译,最后配置默认build.gradle我所有的Andr​​oid的项目,这是一个相当很少,而我不希望复制该文件。

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的父POM,但因为我不完全理解摇篮是如何工作的,我开这你们分享您的想法......

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?

推荐答案

这其中大部分是pretty的正常的<一个href="http://www.gradle.org/docs/current/userguide/multi_project_builds.html">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')

,以便将摇篮模块之前评估PROJECT1和项目2。在所有包含code中的项目,你需要有一个空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.

示例: <一个href="https://github.com/ethankhall/AndroidComplexBuild">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")
}

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

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