Note: Most of these blogs are for my personal reference and at a given time, some of those might just be unpolished drafts.

Some notes on gradle

Java plugin for gradle

Add build.gradle at root of the project

apply plugin: 'java'

This links the gradle. Makes gradle aware of java.

build.gradle is written in groovy Intellij comes with an inbuilt groovy console (interpreter for the language)

Groovy console can also be used as ‘java interpreter’ to quickly evaluate java code and api (akin to jshell)

groovy closures:

class Myclass{
    void doSomething(Closure closure){
        closure.call()
    }
}

obj = new Myclass()
obj.doSomething {
    print(new Date())
}
//passing code as argument to methods.. which can be decided at the run time```

gradle project object model (POM)

list gradle tasks:

graldle tasks
> Task :tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'java-project'.
components - Displays the components produced by root project 'java-project'. [incubating]
dependencies - Displays all dependencies declared in root project 'java-project'.
dependencyInsight - Displays the insight into a specific dependency in root project 'java-project'.
dependentComponents - Displays the dependent components of components in root project 'java-project'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'java-project'. [incubating]
projects - Displays the sub-projects of root project 'java-project'.
properties - Displays the properties of root project 'java-project'.
tasks - Displays the tasks runnable from root project 'java-project'.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

list dependencies (including the transitive ones (referenced))


> Task :dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

annotationProcessor - Annotation processors and their dependencies for source set 'main'.
No dependencies

apiElements - API elements for main. (n)
No dependencies

archives - Configuration for archive artifacts.
No dependencies

compile - Dependencies for source set 'main' (deprecated, use 'implementation' instead).
No dependencies

compileClasspath - Compile classpath for source set 'main'.
No dependencies

compileOnly - Compile only dependencies for source set 'main'.
No dependencies

default - Configuration for default artifacts.
No dependencies

implementation - Implementation only dependencies for source set 'main'. (n)
No dependencies

runtime - Runtime dependencies for source set 'main' (deprecated, use 'runtimeOnly' instead).
No dependencies

runtimeClasspath - Runtime classpath of source set 'main'.
No dependencies

runtimeElements - Elements of runtime for main. (n)
No dependencies

runtimeOnly - Runtime only dependencies for source set 'main'. (n)
No dependencies

testAnnotationProcessor - Annotation processors and their dependencies for source set 'test'.
No dependencies

testCompile - Dependencies for source set 'test' (deprecated, use 'testImplementation' instead).
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testCompileClasspath - Compile classpath for source set 'test'.
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testCompileOnly - Compile only dependencies for source set 'test'.
No dependencies

testImplementation - Implementation only dependencies for source set 'test'. (n)
No dependencies

testRuntime - Runtime dependencies for source set 'test' (deprecated, use 'testRuntimeOnly' instead).
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testRuntimeClasspath - Runtime classpath of source set 'test'.
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
No dependencies

A web-based, searchable dependency report is available by adding the --scan option.

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

resolving dependencies conflict:

what if different versions?

say, you are importing junit 5 and google gson (which internally is importing junit 4)

latest will be taken

viewing project-report (dependency report)

apply the plugin: ‘project-report’

plugins {
    id 'java'
    id 'project-report'
}

command

gradle htmlDependencyReport

gradle build directory

gralde clean removes the build directory (build stores the results as cache)

viewing project structure:

 gradle -q projects #quiet (no verbose output)

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'java-project'
\--- Project ':json-display-module'

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :json-display-module:tasks

gradle wrapper

Using wrapper is always recommended instead of using local installation as there can be multiple gradle projects with different gradle versions.

creating:

 gradle wrapper --gradle-version 5.0

gradle->wrapper->gradle-wrapper.properties

gradlew #for unix machines
gradlew.bat #for windows
  • Gradle wrapper makes sure same versions are used across the teams, CI, build server (thus they maintain uniformity)

Adding custom gradle tasks:

task showDate{
    doLast {
        print("executed custom gradle task at ")
        print(new Date())
    }
}

Listed inside ‘others’

executing:

 gradle showDate

> Task :showDate
executed custom gradle task at Wed Mar 13 14:52:18 IST 2019
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

Maintaining own group and description:

task showDate{
    dependsOn build
    group = 'my tasks'
    description = 'To show various information regarding build'
    doLast {
        print("executed custom gradle task at ")
        print(new Date())
    }
}

extending gradle task

class ShowDate extends DefaultTask{

    @TaskAction
    void showDate(){
        println 'today is'+ new Date()
    }
}

task showDate(type: ShowDate)

profiling gradle build

gradle build --profile #pass this flag

gradle build cloud

reports sent to the cloud

gradle build --scan

accept license agreement: link is sent to email

whole stat, performance is given

Sample url: https://gradle.com/s/zanlhzc5xltq2

eg: I ran with gradle 5.0.0

gradle build scan report

Suggestion:

Build was run with an outdated Gradle version (5.0) At the time the build was run, the version of Gradle used was outdated. Builds are often faster with more recent Gradle versions.

switching gradle version in wrapper:

./gradlew wrapper --gradle-version=5.2.1

performance suggestion

  1. use the latest version
  2. use gradle daemon
  3. enable parallel execution (with flag –parallel)
Written on March 13, 2019