最近想深刻的学习一下工程化方面相关的东西,在maven和gradle直接纠结不已,由于maven的扩展性太差劲了,学习成本颇高,因此最后投入了gradle的怀抱中,之后有时间再从新学习一下maven吧shell
最近的学习笔记是基于gradle 5 系列,其中各类教程和例子大都是来源于官方文档或者网络上的博客。内容涵盖我在学习gradle过程当中的各类心得和gradle的一些使用方法网络
注意: 这里使用的配偶语言世kotlin 而不是使用groovyapp
一个命令maven
gradle init
用户能够经过这个命令建立一个基本的gradle项目包括gradle项目的目录结构函数
├── build.gradle (1) ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar (2) │ └── gradle-wrapper.properties (3) ├── gradlew (4) ├── gradlew.bat (5) └── settings.gradle (6)
gradle 方便用户进行配置的特性是源于gradle提供了方便使用task参数
这里编写一个很基本的copy文件的权限,在路径中添加一个src文件夹和dest文件夹,在src文件中添加一个文件markfile.txt 而且里面有一个内容hello world!工具
而后在build.gradle 中编写一个任务学习
task copy(type:Copy,group:"custom",description:"test one"){ from "src" into "dest" }
其中的type 字段将会调用系统中的Copy函数,而group和description 只是描述这个过程的描述符,只会影响系统的log输出,并不会影响实际的效果gradle
./gradlew Copy
运行对应的命令就能运行对应的copy方法ui
在项目中使用插件标签plugins添加指定的base插件到系统中this
plugins { id "base" }
而后在指定的位置咱们添加一个插件(和任务的使用方法相同,我怀疑其实gradle的插件就是打包好的任务)
task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") { from "src" setArchiveName "basic-demo-1.0.zip" }
而后运行对应的命令,就能够在对应的目录 build/distributions 中找到 src目录下的压缩文件了
./gradlew Zip
gradle 有一个内置的命令
./gradlew task
这条命令将会将全部的当前的gradle项目中拥有的构建命令所有列出来
> Task :tasks ------------------------------------------------------------ Tasks runnable from root project ------------------------------------------------------------ Archive tasks ------------- zip - Archives sources in a zip file Build tasks ----------- assemble - Assembles the outputs of this project. build - Assembles and tests this project. clean - Deletes the build directory. Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Custom tasks ------------ copy - Copies sources to the dest directory Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle'. components - Displays the components produced by root project 'gradle'. [incubating] dependencies - Displays all dependencies declared in root project 'gradle'. dependencyInsight - Displays the insight into a specific dependency in root project 'gradle'. dependentComponents - Displays the dependent components of components in root project 'gradle'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'gradle'. [incubating] projects - Displays the sub-projects of root project 'gradle'. properties - Displays the properties of root project 'gradle'. tasks - Displays the tasks runnable from root project 'gradle'. Verification tasks ------------------ check - Runs all checks. Rules ----- Pattern: clean<TaskName>: Cleans the output files of a task. Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration. Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration. To see all tasks and more detail, run gradlew tasks --all To see more detail about a task, run gradlew help --task <task> BUILD SUCCESSFUL in 1s 1 actionable task: 1 executed <-------------> 0% WAITING > IDLE
咱们在使用构建命令的时候能够在命令的后面添加一个 --scan命令,经过这个命令能够连接到gradle官方的view显示仓库或者链接到自定义的连接仓库中,而后轻松的查看项目使用的构建任务,构建时间等等信息
./gradlew Zip --scan
而后能够登入命令行打出的一个网址,在其中的form表单中填写邮箱地址,而后就会将构建的信息发送到邮箱中了
注意:这个功能是收费的
$ ./gradlew properties > Task :properties ------------------------------------------------------------ Root project ------------------------------------------------------------ buildDir: /Users/.../basic-demo/build buildFile: /Users/.../basic-demo/build.gradle description: null group: name: basic-demo projectDir: /Users/.../basic-demo version: unspecified BUILD SUCCESSFUL