android - Gradle / Jenkins : Create a gradle file to direct to sub projects

标签 android jenkins gradle android-gradle-plugin build.gradle

我有一个包含三个 android 项目的目录。 MainDir 看起来像这样:

/.gradle
/.git
/project1
/project2
/project3
.gitignore
.Jenkinsfile
.README.md

在 jenkins 中,我无法在构建期间运行 shell 脚本来为这些项目中的每一个启动 gradle 任务,因为他不知道这些是项目(他说“没有子项目”)。

在项目目录中它看起来像:

/.gradle
/app
/build
/gradle
.gitignore
.build.gradle
.gradle.properties
.gradlew

有没有办法让 Jenkins 明白这是他可以在其中启动 gradle taks 的三个项目?就像在主目录中创建一个 build.gradle 文件一样? 或者我应该只创建 3 个 Jenkins 项目吗?

最佳答案

您可以在 jenkins 中进行三个构建,但除非需要单独构建库,否则最终可能只是额外的工作。听起来您真正想要的是多项目构建 [1]。一个简单的示例可以作为两个文件位于 lib 项目上方的文件夹中,build.gradlesettings.gradle

settings.gradle 将定义构建范围中包含的项目。

例如,给定您的 project1project2project3 示例,您的 settings.gradle 可能如下所示。

rootProject.name = 'myRootProjectName'

// note the name is not required to match the actual path
include ":project1"
// but if the name is not the same as the path then we can just
// let gradle know where the project is expected
project(":project1").projectDir = new File(settingsDir, "pathToProject1")

include ":project2"
project(":project2").projectDir = new File(settingsDir, "pathToProject2")

include ":project3"
project(":project3").projectDir = new File(settingsDir, "pathToProject3")

//##### below would be instead of the code above, same thing just manual
// project setup vs letting gradle find the subprojects
// note sometimes you have lots of subprojects in that case it's sometimes
// easier to just use a little logic for finding and setting up the subprojects.
// don't use the code above ##### and below only use one or the other
// or you will have errors.  The method below is the most scaleable since
// adding projects requires zero modifications to the root project
rootProject.name = 'myRootProjectName'

// set up a couple file filters to find the dirs we consider subprojects
FileFilter projectFilter = { File pathname ->
    FileFilter gradleProjectFilter = { File file -> file.name == 'build.gradle' }
    // add this folder if is a directory and that directory contains a build.gradle file
    // here note `File#listFiles` is true if it's `size() > 0` due to
    // groovy's concept of truth (details: http://groovy-lang.org/semantics.html#Groovy-Truth)
    return pathname.isDirectory() && pathname.listFiles(gradleProjectFilter)
}

settingsDir.listFiles(projectFilter).each { dir ->
    include ":$dir.name"
    project(":$dir.name").projectDir = dir
}

现在运行 gradle projects 任务应该显示三个子模块。

至于您的 build.gradle 文件,您可以根据需要为所有模块指定一些通用属性,或者将文件留空,它必须存在但可以为空。如果你想分享一些配置,那么你可以用这样的东西设置 build.gradle

project.subprojects { Project subproject ->
    // anything that is defined here will be executed before the subproject's build.gradle file
    subproject.buildscript {
        repositories {
            jcenter()
            // your private maven repo if needed
            maven { url 'http://1.2.3.4:8081/nexus/content/repositories/release' }
        }
        dependencies {
            // some plugin that is now available to be applied in any subproject
            classpath 'my.sweet.gradle:plugin:0.1'
        }
    }
    subproject.afterEvaluate {
        // this block is executed after the subproject's build.gradle file
        if (project.tasks.withType(org.gradle.jvm.tasks.Jar)) {
            // for example you might want to set the manifest for each subproject
            manifest {
                attributes 'Implementation-Title': "Lib $subproject.name",
                        'Implementation-Version': version
            }
        }
    }
}

[1] https://docs.gradle.org/current/userguide/multi_project_builds.html

关于android - Gradle / Jenkins : Create a gradle file to direct to sub projects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511435/

相关文章:

android - 使用Android NDK交叉编译

Android SDK Manager,包的本地副本

android - 在 ViewPager 中使用 EventBus 时获取混合数据

android - 如何全屏显示对话框 fragment ?

java - 我将如何添加注释以从 jacoco 代码覆盖率报告中排除方法?

Jenkins bitbucket oauth 插件 - 限制登录团队

selenium - 运行 Selenium Test 时覆盖窗口显示在屏幕外

jenkins - 为什么 Jenkins 在 jacoco 报告代码覆盖率摘要中显示没有 (0%) 代码覆盖

android - Gradle 更新后不再显示 dagger2 组件图

gradle - Gradle CustomTask不考虑依赖的InputFiles