Skip to main content

Gradle Basic Start

Henry...About 2 min

Gradle Basic Start

Gradleis the origin of Apache Antopen in new window and Apache Mavenopen in new window The project automates construction of open source tools using a field language (DSL) based on Groovyopen in new window to declare project settings and discard a very burdensome XML-based configuration for Java apps.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift.Currently supported languages are Java, Groovy, Kotlin, Scala, Android, C++ and Swift. Below is a comparison between Gradle and Apache Maven:

Gradle vs Mavenopen in new window
Gradle vs Maven

Configure wrapper

It is recommended to use wrapper to configure the Gradle environment to keep the project environment unified, add the gradle/wrapper/gradle-wrapper.properties configuration file in the root directory of the project, see the introductory document for detailed configuration, the configuration content is as follows:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip

gradle-wrapper.jar, gradlew, gradlew.bat files can be automatically generated by command. Project structure below:

.
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat

Configure setting.gradle

settings.gradle and Settings Instances are used to declare instantiation and configuration Projects, which can be used for multiproject management and project plugin versions etc. Configure examples below:

// include two projects, 'Foo' and 'Foo:bar'
// directories are referred by replacing ':' with '/'
include 'fo:bar'

// include one project whose project dir does not match the logical project path
include 'baz'
project(':baz'). rojectDir = file('foo/baz')

// include many projects whose project irs do not match the logical project paths
file('subjects'). achDir LOR dir ->
    include dir.name
    project(":${dir.name}").projectDir = dir
}

Project structure below:

.
├── settings.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat

Configure build.gradle

Used to configure project dependencies, required plugins etc. Configure examples below:

// 添加插件
plugins {
    id 'groovy-gradle-plugin'
    id 'maven-publish'
}

// 配置项目信息
group = 'com.myorg.conventions'
version = '1.0'

// 配置组件仓库
repositories {
    gradlePluginPortal() // so that external plugins can be resolved in dependencies section
}

// 配置依赖
dependencies {
    implementation 'gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.6.2'
    testImplementation platform("org.spockframework:spock-bom:2.0-groovy-3.0")
    testImplementation 'org.spockframework:spock-core'
    testImplementation 'org.spockframework:spock-junit4'
    testImplementation 'junit:junit:4.13.1'
}

// 配置插件
publishing {
    repositories {
        maven {
            // change to point to your repo, e.g. http://my.org/repo
            url = layout.buildDirectory.dir("repo")
        }
    }
}

// 配置任务
tasks.named('publish') {
    dependsOn('check')
}

Project structure below:

.
├── build.gradle
├── settings.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat

Configure gradle.properties

Configure some Gradle environment properties, e.g.:

org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configureondemand=true
org.gradle.daemon=false
org.gradle.daemon.performance.enable-monitoring=false

Project structure below:

.
├── gradle.properties
├── build.gradle
├── settings.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat