https://docs.gradle.org/current/dsl/
DSL(Domain Specified Language)领域专用语言。
Gradle scripts are configuration scripts. As the script executes, it configures an object of a particular type. For example, as a build script executes, it configures an object of type Project
. This object is called the delegate object of the script. The following table shows the delegate for each type of Gradle script:Build script——Project,Init script——Gradle,Settings script——Settings
配置类型脚本,每个类型脚本作用于特定的类型delegate object
each Gradle script implements the Script
interface. This interface defines a number of properties and methods which you can use in the script.
A script block is a method call which takes a closure as a parameter.
script block以闭包作为参数
Build script structure
A build script is also a Groovy script
allprojects { }
—— Configures this project and each of its sub-projects;
configurations { }
—— Configures the dependency configurations for this project.√
This method executes the given closure against the ConfigurationContainer(
for this project. The configurations
)ConfigurationContainer
is passed to the closure as the closure‘s delegate
A ConfigurationContainer
is responsible for declaring and managing configurations. See also Configuration
.
A Configuration
represents a group of artifacts and their dependencies.Configuration is an instance of a FileCollection
that contains all dependencies
dependencies { }
—— Configures the dependencies for this project;
This method executes the given closure against the DependencyHandler
for this project. The DependencyHandler
is passed to the closure as the closure‘s delegate.
A DependencyHandler
is used to declare dependencies. Dependencies are grouped into configurations (see Configuration
).
-
compile:编译时是必须的。
-
runtime:运行时是必须的,默认包含编译时依赖。
-
testCompile:编译测试是必须的,默认包含编译产品依赖和编译时依赖。
-
testRuntime:测试运行是必须的,默认包含编译、运行时、测试编译依赖。
apply plugin: ‘java‘ //so that we can use ‘compile‘, ‘testCompile‘ for dependencies
dependencies {
//External dependencies represented by aExternalModuleDependency
. compile ‘commons-lang:commons-lang:2.6‘ //configurationName "group:name:version:classifier@extension"
compile group: ‘com.google.code.guice‘, name: ‘guice‘, version: ‘1.0‘ //configurationName group: group, name: name, version: version, classifier: classifier, ext: extension
//advanced
compile(‘org.hibernate:hibernate:3.1‘) { //configurationName(dependencyNotation){configStatement1 configStatement2} //Forcing certain dependency version in case of the conflict 强制版本 force = true //in case of versions conflict ‘3.1‘ version of hibernate wins //excluding a particular transitive dependency 排除传递性依赖 ——ModuleDependency.exclude(java.util.Map)
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 }
//configuring dependency on ‘someLib‘ module
compile(group: ‘org.myorg‘, name: ‘someLib‘, version:‘1.0‘) { //explicitly adding the dependency artifact: artifact {//Adds an artifact to this dependency. The given action is passed aDependencyArtifact
instance, which it can configure.———See alsoModuleDependency.artifact(groovy.lang.Closure)
. //useful when some artifact properties unconventional 非常规的 name = ‘someArtifact‘ //artifact name different than module name extension = ‘someExt‘//Often the extension is the same as the type, but sometimes this is not the case. For example for an ivy XML module descriptor, the type is ivy and the extension is xml 扩展名 type = ‘someType‘ classifier = ‘someClassifier‘
//classifier元素用来帮助定义构件输出的一些附属构件。附属构件与主构件对应.
//比如主构件是 kimi-app-2.0.0.jar, 还有kimi-app-2.0.0-javadoc.jar、kimi-app-2.0.0-sources.jar 这样两个附属构件。这时候,javadoc,sources就是这两个附属构件的classifier。 } }
//In both below notations, all properties, except name, are optional.
//Project dependencies are represented using aProjectDependency
.
// declare dependency to the ‘default‘ configuration of the projectA
//configurationName project(‘:someProject‘)
//If you need to depend on a specific configuration of projectA, use map notation for projects:
//configurationName project(path: ‘:projectA‘, configuration: ‘someOtherConfiguration‘)
//File dependencies are represented using aSelfResolvingDependency
.
compile files(‘hibernate.jar‘, ‘libs/spring.jar‘) //declaring arbitrary files as dependencies
compile fileTree(‘libs‘)//putting all jars from ‘libs‘ onto compile classpath
//both beolw is file dependency }
repositories { } —— Configures the repositories for this project.√
sourceSets { } —— Configures the source sets of this project √;
subprojects { } —— Configures the sub-projects of this project.√;
buildscript { } —— Configures the build script classpath for this project;
artifacts { } —— Configures the published artifacts for this project;
publishing { } —— Configures the PublishingExtension added by the publishing plugin.
Core types
—— used in Gradle scripts:
Project —— This interface is the main API you use to interact with Gradle from your build file. From a Project, you have programmatic access to all of Gradle‘s features.√
Task —— A Task represents a single atomic piece of work for a build, such as compiling classes or generating javadoc.√
Gradle —— Represents an invocation of Gradle.
Settings —— Declares the configuration required to instantiate and configure the hierarchy of Project instances which are to participate in a build.
Script —— This interface is implemented by all Gradle scripts to add in some Gradle-specific methods. As your compiled script class will implement this interface, you can use the methods and properties declared by this interface directly in your script.
SourceSet —— A SourceSet represents a logical group of Java source and resources.
JavaToolChain —— A set of tools for building from Java source.