创建一个项目
你可以通过创建一个build.gradle的文件来开始一个项目,然后可以向文件中加入构建逻辑。
$ mkdir basic-demo$ cd basic-demo$ touch build.gradle
运行 tasks 命令
一个项目包含了一个多个tasks的集合,每个tasks都完成一个基本的操作
通过运行tasks命令可以查看项目中有哪些可用的tasks
$ gradle tasks:tasks------------------------------------------------------------All tasks runnable from root project------------------------------------------------------------Build Setup tasks-----------------init - Initializes a new Gradle build.wrapper - Generates Gradle wrapper files.Help tasks----------buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'.components - Displays the components produced by root project 'basic-demo'. [incubating]dependencies - Displays all dependencies declared in root project 'basic-demo'.dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]help - Displays a help message.model - Displays the configuration model of root project 'basic-demo'. [incubating]projects - Displays the sub-projects of root project 'basic-demo'.properties - Displays the properties of root project 'basic-demo'.tasks - Displays the tasks runnable from root project 'basic-demo'.To see all tasks and more detail, run gradle tasks --allTo see more detail about a task, run gradle help --task
创建gradle文件夹
wrapper task 创建一个gradle wrapper文件,这个wrapper文件包含一个小脚本、jar文件和属性文件,
这个wrapper文件可以让用户在没有装Gradle的情况下运行
产生的目录结构如下:
.├── build.gradle├── gradle│ └── wrapper│ ├── gradle-wrapper.jar│ └── gradle-wrapper.properties├── gradlew //Wrapper script for Unix-based systems└── gradlew.bat //Wrapper script for Windows
运行 properties 命令
properties 命令告诉用户项目有哪些属性
:properties------------------------------------------------------------Root project------------------------------------------------------------buildDir: /Users/.../basic-demo/buildbuildFile: /Users/.../basic-demo/build.gradledescription: nullgroup:name: basic-demoprojectDir: /Users/.../basic-demoversion: unspecifiedBUILD SUCCESSFUL
你可以改变许多用户属性,例如可以添加如下属性到build.gradle文件中:
description = 'A trivial Gradle build'version = '1.0'
配置Gradle核心任务
Gradle本身有一个task库,你可以在自己的项目中配置这些task。例如,Copy是一个核心task,作用是复制文件。
- 创建一个路径:src
- 创建一个叫myfile.txt的文件到src下
- 定义一个类型是Copy的叫做copy的任务,语法如下
task copy(type: Copy) { from 'src' into 'dest'}
执行copy任务:
$ ./gradlew copy:copy
加入一个Hello World!任务
task hello { doLast { println 'Hello, World!' }}
执行,输出 :
$ ./gradlew hello:helloHello, World!BUILD SUCCESSFUL