首页 文章

Gradle / Jenkins:创建一个gradle文件以指向子项目

提问于
浏览
1

我有一个包含三个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项目?

1 回答

  • 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

相关问题