Commonly used summary
gradle common commands
//List all the properties of the project. Then you can see the properties added by the plug-in and their default values gradle properties //List all tasks for the project gradle -q tasks --all
Character string
-
'' only represents a string
-
"String splicing, variable variable splicing or task splicing $task splicing
-
'' '' 'can be used to wrap lines
Define constants
A project corresponds to a project instance. Gradle instantiates the project instance according to the build.gradle configuration content
The project property has group,name,version
Methods include apply, repositories, dependencies, task
Properties can be configured by ext, gradle.properties
ext.property1 = "this is property1" //You can also use closures ext { property2 = "this is property2" }
Defining tasks
task hello { doLast { println 'Hello world!' } }
Use plug-ins
//One way plugins { id 'war' id 'org.hidetake.ssh' version '2.9.0' } //Mode two //Plug-in unit apply plugin: 'java-library' apply plugin: 'eclipse' apply plugin: 'maven' apply plugin: 'java'
Use warehouse
repositories { //Third party warehouse maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'} //maven central warehouse mavenCentral() jcenter() }
Add dependency
dependencies { compile group: 'commons-collections', name: 'commons-collections', version: '3.2' testCompile group: 'junit', name: 'junit', version: '4.+' }
Custom project
//jdk version sourceCompatibility = 1.8 //Code [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8' //Project version version = '1.0'
Upload jar package
uploadArchives { repositories { flatDir { dirs 'repos' } } } uploadArchives { repositories { mavenDeployer { //userName and password are the user name and password of maven repository(url: "http://172.30.10.160:8081/nexus/content/repositories/snapshots/") { authentication(userName: "deploy", password: "deploy123") } pom.project { name=project.name packaging='jar' description='a test' } } } }
You can also use the Maven publish plug-in
Multi project construction
//settings.gradle //Build multiple projects at the upper and lower levels rootProject.name = '111' include '111-1' include '111-2' //Build multiple projects at the same level rootProject.name = '111' includeFlat '111-1' includeFlat '111-2'
Dependencies between projects
dependencies { compile project(":111-2") }
Resolving version conflicts
-
- View dependency reports
-
- Exclude transitive dependency
-
- Force a version
groovy uses the highest version by default to help us resolve version conflicts
//Modify the default policy. If there is a version conflict, the build fails configurations.all { resolutionStrategy { failOnVersionConflict() } } //Solution //1. Exclude transitive dependency dependencies { compile (''){ exclude } } //2. Force a version configurations.all { resolutionStrategy { failOnVersionConflict() force '' } }
Multi project construction public configuration
All projects is valid for all projects
Projects are effective for all projects and can be customized
allprojects { apply plugin: 'java' sourceCompatibility = 1.8 //Modify the default policy. If there is a version conflict, the build fails configurations.all { resolutionStrategy { failOnVersionConflict() } } uploadArchives { repositories { flatDir { dirs 'repos' } } } } //Warehouse, dependency can also be placed in all projects subprojects { //Warehouse repositories { jcenter() } //rely on dependencies { } } //Properties can also be defined in gradle.properties group=com.home.woms version=1.1.1
Multi project construction - exclusive configuration
// Configure child project in parent project bulid.gradle project(':core') { ext{ hibernateVersion = '4.2.1.Final' } dependencies { compile "org.hibernate:hibernate-core:${hibernateVersion}" } }
Build script dependencies
In addition to project dependencies, build scripts can also have their own dependencies. When you use a plug-in that is not officially provided by Gradle, you need to specify its dependency in the build script and, of course, the Repository of the plug-in. In Gradle, use the buildscript block to configure dependencies for the build script.
For example, in the project, we use the cucumber JVM as the BDD tool of the project, but Gradle doesn't provide its plug-in officially. Fortunately, someone in the open source community provides the cucumber plug-in. Add the following code to the build script:
buildscript { repositories { mavenCentral() } dependencies { classpath "gradle-cucumber-plugin:gradle-cucumber-plugin:0.2" } } apply plugin: com.excella.gradle.cucumber.CucumberPlugin
apply other Gradle files
When a project is very complex, the Gradle script is also very complex. In addition to moving the configuration of subprojects to the corresponding project's build script, you can also split the complex build script into small build scripts according to different functions, and then use apply from in build.gradle to introduce these small build scripts into the overall build script. For example, if you use Jetty and Cargo plug-in to start JBoss in a project, you can refer them to jetty.gradle and jboss.gradle respectively, and then use the following code in build.gradle to introduce them:
apply from: "jetty.gradle" apply from: "jboss.gradle"
Define resource directory
sourceSets { main { java { srcDir 'src/main/java' // Specify source directory } resources { srcDir 'src/main/resources' //Resource directory } } } //perhaps sourceSets { main.java.srcDirs = ['src/main/java'] main.resources.srcDirs = ['src/main/resources'] }