在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):alikslee/gradle-tips开源软件地址(OpenSource Url):https://github.com/alikslee/gradle-tips开源编程语言(OpenSource Language):开源软件介绍(OpenSource Introduction):Gradle TipsOver last year or so I have started using Gradle as my primary build tool for JVM based projects. Before using Gradle I was an Apache Maven user. Gradle takes best from both Apache Maven and Apache Ant providing you best of both worlds. Gradle borrows flexibility from Ant and convention over configuration, dependency management and plugins from Maven. Gradle treats task as first class citizen just like Ant. A Gradle build has three distinct phases - initialization, configuration, and execution. The initialization phase determine which all projects will take part in the build process and create a Project instance for each of the project. During configuration phase, it execute build scripts of all the project that are taking part in build process. Finally, during the execution phase all the tasks configured during the configuration phase are executed. In this document, I will list down tips that I have learnt over last year or so. Contributing to Gradle tipsPlease contribute if you see an error or something that could be better! Raise an issue or send me a pull request to improve. Contributions of all kinds, including corrections, additions, improvements, and translations, are welcome! Tip 1: Use Gradle WrapperOne of the Gradle features that impressed me a lot when I was starting with Gradle was support for wrapper scripts. Gradle wrapper makes your project self contained and independent of build tool installation. It lets you run Gradle builds without a previously installed Gradle distribution in a zero configuration manner. This will ensure everyone use same version of the build tool. To create Gradle wrapper scripts for your Grade project you can run following command. $ gradle wrapper --gradle-version 2.14.1
This will generate few files in your project --
At any point in time, if you wish to upgrade the Gradle version just regenerate the Gradle wrapper scripts passing it the Gradle version you want to use. Let's suppose we want to upgrade to Gradle $ gradle wrapper --gradle-version 3.0-milestone-2 Also, it is a good idea to set an alias for alias gradle="./gradlew" Tip 2: View Dependency GraphTo view a dependency graph for your project you can run the following command. $ gradle dependencies Tip 3: Build a single projectGradle supports both single and multi-project builds. Let's suppose our multi-project structure looks like as shown below.
To build the $ gradle api:rest:build Tip 4: Exclude tasksTo execute a task you use $ gradle clean build -x test Tip 5: Profile your buildGradle has in-built support for profiling. If you are facing performance issues, you should use the $ gradle --profile build This will generate report in the Tip 6: Perform dry runThere are times when you wish to see all the tasks that will be executed during the build but don't want to execute them. For this scenario Gradle provides $ gradle build --dry-run Tip 7: Install project jars into local Maven repository$ gradle install Tip 8: View Gradle tasks$ gradle tasks The above does not list all the tasks. To view all the tasks you have to pass $ gradle tasks --all Tip 9: Use Gradle daemonOne of the easiest way to speed your Gradle build is to use Gradle daemon to run your build. Gradle daemon is a long-lived background process that performs bootstrapping only once during its lifetime. Gradle daemon is not enabled by default. To use Gradle daemon, you can use $ gradle build --daemon
Passing the
Tip 10: Parallelize the buildOpen your
Tip 11: Customize Gradle tasksYou can customize any Gradle tasks by overriding its apply plugin:'java'
test.doFirst {
println("running tests...")
}
test.doLast {
println("done executing tests...")
} Tip 12: Provide JVM arguments to Gradle daemonYou can specify JVM arguments to Gradle daemon by entering a line in
Tip 13: Run in offline mode$ gradle build --offline Tip 14: Enable configure on demandConfiguration on demand is an incubation feature of Gradle, so it’s not enabled by default yet. $ gradle clean build --configure-on-demand If you want to make this a default option, then you can provide this option globally by adding a line to
Tip 15: Refresh Gradle dependency cache$ gradle clean build --refresh-dependencies You can also delete the cached files under Tip 16: Define a local jar dependencyLet's suppose you have a dependencies {
compile files('libs/myjar.jar')
} This can also be done by following. repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile name: 'myjar'
} Tip 17: Define all jars in a local directory as dependencyIf you need to add all the libraries in a directory then you can do the following: dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
} Tip 18: Build project and all project it depends on$ gradle api:model:buildNeeded Tip 19: Build project and all its dependents
Tip 20: Provide default tasks to your build scriptIt is a good practice to define default tasks for your project so that a first time user of your project can easily get started. In your Gradle script, define defaultTasks "clean","build" Now, if a user will run Tip 21: Create checksum for a fileapply plugin: 'java'
archivesBaseName = 'checksum-sample'
jar.doLast { task ->
ant.checksum file: task.archivePath
} Tip 22: Using different name for build fileBy default build file has rootProject.buildFileName = "gradle-tips.gradle" Now, rename your build file Tip 23: Using different names for build script in multi-project Gradle projectBy convention, we use
By default, all of these sub projects will have rootProject.children.each {
it.buildFileName = it.name + '.gradle'
} Now, you can use build.gradle for root project. Sub projects will have Tip 24: Using Gradle guiYou can use Gradle gui by launching it via command-line as shown below. $ gradle --gui This will open the Gradle gui as shown below. Tip 25: Create untar tasktask untar( type : Copy) {
from tarTree(‘dist.tar.gz’)
into ‘destFolder’
} Tip 26: Fail configuration on version conflictIn your build script, define a configuration block as shown below. configurations {
compile.resolutionStrategy.failOnVersionConflict()
} Tip 27: Using provided scope in GradleYou can use maven like dependencies {
compileOnly 'javax.servlet:servlet-api:3.0-alpha-1'
} Tip 28: Set Java compile encoding explicitlyIn your compileJava.options.encoding = 'UTF-8' Tip 29: Disable transitive dependencies resolutionTurn transitive dependencies off for a whole configuration: configurations {
compile.transitive = false
} Tip 30: Viewing Gradle versionYou can view Gradle version $ gradle -v
You can view the Gradle version that your current build is running by using task gradleVersion {
group = "help"
description = "Prints Gradle version"
doLast {
logger.quiet("You are using [${GradleVersion.current()}]")
}
} When you will run it you will see the following: $ gradle gradleVersion
Tip 31: Disable a tasktaskName.enabled = false If you want to disable test task then you can disable it as shown below. test.enabled = false Tip 32: Init a Gradle projectTo create Java Gradle project that uses testng testing framework you can use the following command. $ gradle init --type java-library --test-framework testng If you want to use JUnit, then don't specify $ gradle init --type java-library You can also create groovy and scala projects as well. $ gradle init --type scala-library $ gradle init --type groovy-library Tip 33: Sign artifactsapply plugin: 'signing'
signing {
sign configurations.archives
} If you only want to sign releases not snapshots then you can do following apply plugin: 'signing'
signing {
required { !version.endsWith("SNAPSHOT”) }
} Tip 34: Running tests in paralleltest {
maxParallelForks = 2
} Tip 35: Set memory for teststest {
minHeapSize = ‘512m'
maxHeapSize = ‘1024m'
} Tip 36: Using short names for tasksIf you have a task $ gradle bSD You have to make sure it is unique among all tasks. If there is another task $ gradle bSeD Tip 37: Learn about a Gradle task$ gradle help --task <task name> $ gradle help --task dependencies Tip 38: Run gradle in debug mode
Tip 39: Continue task execution after task failure
Tip 40: Convert Maven project to GradleGo to your Maven project and run the following command. $ gradle init --type pom Tip 41: Force Gradle to rerun tasks even if they are UP-TO-DATE$ gradle build --rerun-tasks Tip 42: Use exact version numbers in dependenciesWhen you are declaring dependencies don't use + in your dependencies, rather use exact version numbers. This will make your build faster and reproducible. Tip 43: Enable continuous buildIf you wish to continuously run your build then you can use
Tip 45: Run a single test caseThere are times when we only when to run a single test case rather than running the full test suite. This can be accomplished by following command. $ gradle test --tests tips.CalculatorTest To run only a single test case of $ gradle test --tests tips.CalculatorTest.shouldAddTwoNumbers You can also use regex to specify multiple tests
You can also use
To run a single test in a submodule you can do following
Tip 45: Generate source and javadoc jartask sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论