Why Study Gradle
1. Clear Configuration , easy to understand
2. more concise than Maven configuration files
3. support for powerful Groovy scripts
4. There are 3 reasons for this .
Learning Gradle the entrance
just like learning springmvc , the first thing to do is to find out how the Web. XML is configured , and then to find the respective configuration files. Then specifically how to configure another one to view.
Gradle 's master profile is called Build.gradle , and the other configuration files are Setting.gradle. And so on, generally can also derive a convenient work script such as Gradle.bat .
The script files for these gradle , as well as common configurations , are listed as a reference.
Gradle'sJava plugin
Perform gradle build
It will output
: Compilejava
:p rocessresources
: Classes
: Jar
: Assemble
: Compiletestjava
:p rocesstestresources
: testclasses
: Test
: Check
: Build
BUILD Successful
This is its Java plugin .
File structure of the Gradle projectSimple.GradleProject structure
Gradle-project
Src
Resources
Build.gradle
Setting.gradle
Multi-moduleGradleProject structure
Project-parent
Src
Resources
Module1
Src
Resources
Build.gradle
Setting.gradle
Module2
Src
Resources
Build.gradle
Setting.gradle
Module3
Src
Resources
Build.gradle
Setting.gradle
Build.gradle
Setting.gradle
This is a typical multi-module Gradle project . A multi- Module project like Maven .
At the same time, the definition of the submodule is defined in Setting.gradle
Setting.gradle
include ' Module1 ', ' module2 ', ' Module3 '
The build.gradle of the related subproject affects itself and its own sub-project , and the subproject's own Build.gradle overrides the parent configuration. As an example:
The build.gradle in project-parent defines a Task named Hello , when the project-parent When executing gradle Hello in the directory , the sub-module's Task named Hello is also executed together.
Task in the Gradle
Gradle Task is more powerful than maven ant Lvy task , can implement code + script mixed writing , Although the task can be doped with Groovy code , this does not mean that it affects the Gradle Task the extension.
Definition of Task
By adding the following in build.gradle , you are finished defining a Task
Task helloworld{
println "Hello World"
}
Methods and properties of a task
A task is actually an object, it also has methods and properties, and there are types. By default, the defined task inherits from Defaulttask.
Set default Tasks
defaulttasks ' first ', ' second '
Task First {
Dolast {
println "I am First"
}
}
Task Second {
Dofirst {
println "I am Second"
}
}
The default task is performed when no task name is added after the Gradle command.
In a Task<<Operator
In some Gradle build.gradle files , you will see that there is a task definition with a task name followed by a < < such symbols , which are actually derived from Groovy 's rewriting of << , a when defining a Task , followed by 1 <<
Indicates that the current task is added to the end of the execution task list
Task Initializedatabase
Initializedatabase << {println ' Connect to database '}
Initializedatabase << {println ' Update database schema '}
initializedatabase {print ' Configuring '}
initializedatabase {println ' database connection '}
Execute gradle initializedatabase command
Output:
print ' Configuring '
println ' Database connection '
println ' Connect to database '
println ' Update database schema '
Another section of code
//Initial Task definition (maybe not easily editable)
Task setupdatabasetests << {
println ' Load test data '
}
//Our changes to the task (in a place we can edit them)
setupdatabasetests {
Dofirst {
println ' Create database schema '
}
Dofirst {
println ' drop database schema '
}
}
Description :
the first setupdatabasetests << defines a task, the current task Execution Heap the last position inside , and the first position of the actuator is responsible for outputting ' load test data '
in the following setupdatabasetests {:} The first dofirst defined in specifies that the currently responsible content is added to the last position of the execution heap . If this is done gradle setupdatabasetests will output ' Create ... '
in the following setupdatabasetests {:} The Dofirst defined in section two indicates that the currently responsible content is added to the last position of the execution heap .
when the whole plan was executed , of course it was from Heap Top one of the pop 's executed out
Task depends
Task task1{
println ' Task1 '
}
Task Task2 (DEPENDSON:TASK1) {
println ' Task2 '
}
Execute Gradle Task2
Output
Task1
Task2
Task task3{
println ' Task3 '
}
Task task4{
println ' Task4 '
}
Task Task5 (Dependson:[task3,task4]) {
println ' last Task '
}
Execute Gradle TASK5
Output
Task3
Task4
Last Task
Task type
Build.gradle
class Helloworldtask extends Defaulttask {
@Optional
String message = ' I am Davenkin '
@TaskAction
def hello () {
println "Hello World $message"
}
}
Task Hello (type:helloworldtask)
Task Hello1 (type:helloworldtask) {
Message = "I am a programmer"
}
In this case , a Groovy code is written within the current configuration file, and you can see the flexibility to switch the current by adding the type parameter within the Task's parameters. implementation of Task
Copy
Task CopyFiles (type:copy) {
From ' Resources '
Into ' target '
Include ' **/*.xml ', ' **/*.txt ', ' **/*.properties '
}
Jar
Apply plugin: ' java '
Task Customjar (Type:jar) {
Manifest {
Attributes Firstkey: ' Firstvalue ', Secondkey: ' SecondValue '
}
Archivename = ' Hello.jar '
Destinationdir = File ("${builddir}/jars")
From sourceSets.main.classes
}
javaexec
Apply plugin: ' java '
repositories {
Mavencentral ()
}
dependencies {
Runtime ' commons-codec:commons-codec:1.5 '
}
Task encode (type:javaexec, dependson:classes) {
main = ' Org.gradle.example.commandline.MetaphoneEncoder '
args = "The rain in Spain falls mainly in the plain". Split (). ToList ()
Classpath SourceSets.main.classesDir
Classpath Configurations.runtime
}
Gradle CustomTaskClassInBuild.gradleDefined in
class Helloworldtask extends Defaulttask {
@Optional
String message = ' I am Davenkin '
@TaskAction
def hello () {
println "Hello World $message"
}
}
Task Hello (type:helloworldtask)
task Hello1 (type:helloworldtask) {
message = "I am a programmer"
}
In the example above, we have defined a task named Helloworldtask, which needs to inherit from Defaulttask, which is the function of outputting a string to the command line. @TaskAction represents the action to be performed by the task, that is, when the task is invoked, the Hello () method is executed. In addition, the message is marked as @optional, which indicates that the message is optional when the task is configured. After defining the Helloworldtask, we created two task instances, the first Hello used the default message value, and the second hello1 reset the value of message when it was created.
defined in the project
Package Davenkin
Import org.gradle.api.*
Import org.gradle.api.tasks.*
class Helloworldtask extends Defaulttask {
@Optional
String message = ' I am Davenkin '
@TaskAction
def hello () {
println "Hello World $message"
}
}
Here, we define helloworldtask under the Davenkin package, so when we reference the task in the Build.gradle file, we need its full name:
Task Hello (Type:davenkin. Helloworldtask)
Task Hello1 (Type:davenkin. Helloworldtask) {
Message = "I am a programmer"
}
defined in a separate project
Http://www.cnblogs.com/CloudTeng/p/3421934.html?utm_source=tuicool
Some built-in variables in Gradle
${Name}
${path}
def afile = File ('. ')
Afile.file ();
def afiles = Files (' ... ')
Afiles.fileslist ();
Gradle Daemon Process
The Gradle command is executed with the--daemon parameter, or the-m parameter. The way to abort the Gradle Daemon is to execute the gradle-stop command.
Project Properties for Gradle
Gradle has defined many of the property for Project by default , which are more commonly used:
Project:Project itself
Name:Project names
Path: absolute path to Project
Description: description Information for Project
BuildDir:Project Build Results Store directory
Version: build number of Project
Configuration file-type definition
Create a new plain text file under the project directory: Gradle.properties. Write a key-value pair in this file, it is OK.
· Reading configuration information from other build files
· Build.gradle
Apply from: ' Other.gradle '
· Other.gradle
println "Configuring $project"
Task Hello? {
· println ' Hello from other script '
}
· Determine if a property is set in the project
Hasproperty (' PropertyName ')
Gradle Common Commands
Performing Gradle Properties
Perform gradle tasks
Execute Gradle--daemon
Perform gradle build
Executing the gradle jar
Execute Gradle War
Gradle reference configuration File instance
Use Gradle to build your project