7、project

2019-03-30  本文已影响0人  最美下雨天

一个项目中有几个project呢?
运行命令:

./gradlew projects

所有的project

project 相关api

在gradle中project是以树的形式来管理的,一个项目中的project的总数是所有的module的个数再加上根project,其实每个module中的build.gradle就对应与一个project

我们在gradle文件中写方法


image.png

可以看到this调用的方法都是Project类中的,在控制台随便运行个gradlew命令,我们自己定义的方法就会执行


image.png

这些方法都是在配置阶段就执行了

project相关的api

介绍三个方法:

Project project(String path, Closure configureClosure);
void allprojects(Closure configureClosure);
void subprojects(Closure configureClosure);

第一个方法中的参数path表示的是要操作的project(也即是module)的路径,第二个参数是个闭包,也就是要对project的具体操作。
第二个方法是为所有的project执行操作。
第三个方法是为当前project的所有子project执行操作。

比如说我们想要将当前project的所有子project都都上传到maven库(因为一般我们项目只有一个主module,其他的module都是库)

subprojects { project -> project.apply from: '../publishToMaven.gradle' //引入一个写好的脚本
}

给project定义扩展属性

两种方式:
第一种是在gradle.proproties中写代码:


image.png

第二种是在ext代码块中写:

ext{
    complesdkversion=25
}

例如,我们可以在settings.gradle文件中通过在gradle.properties中定义的属性来动态管理库


image.png

文件相关api

image.png
println getRootDir().absolutePath
println getBuildDir().absolutePath
println getProjectDir().absolutePath

def path='build.gradle'
//file()方法中不需要传递绝对路径,path是相对于当前project的
def filename=file(path);
println filename.text

//文件拷贝
copy {
    CopySpec copySpec->
        from path
        into getBuildDir().absolutePath
        //拷贝的时候需要排除的文件
        exclude()
        //拷贝的时候重命名
        rename {}
}

对文件树的遍历

将整个工程文件映射为一颗树,对树进行遍历

//文件树的遍历
fileTree('gradle/wrapper'){
    ConfigurableFileTree configurableFileTree->
        configurableFileTree.visit {
            FileVisitDetails fileVisitDetails->
               println fileVisitDetails.name
                copy{
                    from(fileVisitDetails.file)
                    into(getRootDir().path+'/test')
                }


        }
}

其他API

image.png
buildscript {
    ScriptHandler scriptHandler->
        //配置项目中仓库的地址
        scriptHandler.repositories {
            RepositoryHandler repositoryHandler->
                repositoryHandler.jcenter()
                repositoryHandler.maven {
                    MavenArtifactRepository mavenArtifactRepository->
                        name ''
                        url ''
                        credentials{
                            username = 'joe'
                            password = 'secret'
                        }
                }

        }
        //配置"插件"的依赖,就是gradle所需要依赖的库
        scriptHandler.dependencies {
            DependencyHandler dependencyHandler->

        }
}
compile('org.hibernate:hibernate:3.1') {
        
             //excluding a particular transitive dependency:
             exclude module: 'cglib' //by artifact name
             exclude group: 'org.jmock' //by group
             exclude group: 'org.unwanted', module: 'iAmBuggy' //by both name and group

             //disabling all transitive dependencies of this dependency
             //传递依赖
             transitive = false
           }

执行外部命令

//拷贝生成的apk到外部路径(注意不是项目内部路径)

task('copyapk')
        {
            doLast {
                def src= getBuildDir().path+'/outputs/apk'
                def des='/Users/huozhenpeng/Documents'
                def command="mv -f ${src} ${des}"
                exec {
                    try {
                        executable 'bash'
                        args '-c',command
                        println 'the command is execute success'
                    }
                    catch(GradleException ex)
                    {
                        println 'the command execute failed'
                    }
                }

            }
        }

上一篇 下一篇

猜你喜欢

热点阅读