Essential Gradle Commands
I use some of the gradle commands everyday in my project and many times I had to randomly search in the internet (as I don't remember these commands by heart). This article is for my personal reference, so that I don't have to search it in random places. These commands include running tests, generating binaries, filtering tests, changing property values during runtime, listing available tasks for a project, executing task in a subproject, printing dependencies information etc..
If you have reached this paragraph and your are still interested in reading further down, then happy reading.. :)
For developers working on Windows platform, please replace './gradlew' with gradlew.bat in the following commands
If you have reached this paragraph and your are still interested in reading further down, then happy reading.. :)
For developers working on Windows platform, please replace './gradlew' with gradlew.bat in the following commands
General gradle tasks
List available projects
./gradlew projects
List available properties
./gradlew properties
List available tasks for root project and sub-project
./gradlew tasks :SomeProject:tasks --all
Build a project
Clean the build directory
./gradlew clean
Generate binaries
./gradlew assemble
Run the build (test and assemble)
./gradlew build
Run a smart build for a project
Build the specified project and project it depends on and project which depends on the specified project
./gradlew :SomeProject:buildNeeded :SomeProject:build :SomeProject:buildDependents
Dependencies
Get all Dependencies of subproject
Includes compile, testCompile and runtime
./gradlew :SomeProject:dependencies
Get all compile dependencies
./gradlew :SomeProject:dependencies --configuration compile
Get all test dependencies
./gradlew :SomeProject:dependencies --configuration testCompile
Get all runtime dependencies
./gradlew :SomeProject:dependencies --configuration runtime
Detailed information about a dependency
The dependency resolution or behaviour could vary between compile, testCompile and runtime. Hence we need to analyse it closely to see the behaviour for different context.
Example:
./gradlew :SomeProject:dependencyInsight --dependency selenium-java
./gradlew :SomeProject:dependencyInsight --dependency selenium-java
./gradlew :SomeProject:dependencyInsight --dependency selenium-java --configuration compile
./gradlew :SomeProject:dependencyInsight --dependency selenium-java --configuration testCompile
./gradlew :SomeProject:dependencyInsight --dependency selenium-java --configuration runtime
Tests and filtering
Run the tests
./gradlew test
Run specific test class or test methods by absolute names
./gradlew test --tests com.example.integration.AllMethodsTest --tests "com.example.integration.SpecificMethodTest.testOne" --tests "com.example.integration.SpecificMethodTest.testTwo"
Run the tests with wildcard filtering
./gradlew test --tests "*FirstTest*" --tests "*SecondTest*.firstTestMethod" --tests "*Third*Test.*test*Methods*" --tests "com.example.package.*FourthTest*"
Disable test task from build and run the tests on demand
In build.gradle file, make the following changes
test.enabled = false
task('endToEndTest', type:Test) {
}
Now to run the end to end tests
./gradlew clean assemble endToEndTest
And the end to end tests will not run as part of build
./gradlew clean build
Handling properties
Passing command line system properties to test
Sometimes it is handy to pass system properties from command line to test java class. This is done to override some default values assumed by the test. But gradle doesn't pass the command line system properties to test, we have to do a small configuration change in order to make it work.
In build.gradle file, make a change to test configuration as below
test {
systemProperties = System.properties.stringPropertyNames().collectEntries {
[(it): System.getProperty(it)]
}
}
systemProperties = System.properties.stringPropertyNames().collectEntries {
[(it): System.getProperty(it)]
}
}
Then when executing test through gradle, pass system properties to test by
./gradlew test -Dproperty1=value1 -Dproperty2=value2
Override properties in gradle
The default value for a property (like application's version) can be stored in gradle.properties file on the root directory of your project and the value can be overridden while executing gradle command using -P option. This can be set to git commit sha in continuos delivery environment.
For example:
In gradle.properties
appVersion=1.0
In build.gradle
apply plugin: 'java'
jar {
baseName = 'sample-application'
version = "$appVersion"
}
Then in command line, execute to override 'appVersion' property
./gradlew assemble -PappVersion=2.0
Run tasks in parallel
Parallel running of tasks is particularly useful when you want to start multiple spring boot application in your repository (e.g web application and rest api)
./gradlew :web-application:bootRun :rest-api:bootRun --parallel
Controlling the verbosity of the output
Prints only the task related information
./gradlew clean build
Prints the output of the task or test on the console
./gradlew clean build --info
Prints the debug related information of the task on the console
./gradlew clean build --debug
Comments
Post a Comment