【问题标题】:Gradle/Jenkins : Create a gradle file to direct to sub projectsGradle/Jenkins:创建一个 gradle 文件以指向子项目
【发布时间】:2017-03-23 12:38:46
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    您可以在 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

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 2021-06-07
      • 2019-03-26
      • 1970-01-01
      • 2019-10-18
      • 2021-05-17
      • 1970-01-01
      • 2019-12-16
      • 2018-02-18
      相关资源
      最近更新 更多