Gradle - my learnings in progress

Photo by Randy Fath on Unsplash

Gradle - my learnings in progress

·

2 min read

You might have seen a file named - gradlew or gradlew.bat.

What is gradlew ?

  • It is gradle wrapper - that means that gradle is embedded inside this wrapper

  • It means that we don't need gradle to be installed in your machine.

  • So that's why we see that, initially it downloads the wrapper to your machine, which brings gradle along with it.

How to create a new project ?

  • Use the init command to create a new project using gradle

  • gradlew init - this will ask few questions like the following:

  • type of project,

  • build script - whether groovy based or kotlin

  • build generation using APIs & behavior - you can skip for now

project name - it automatically selects the parent foldername

How does a new gradle project look like ?

  • Some files are created automatically such as settings.gradle, build.gradle , .gitignore etc

What is settings.gradle file ?

  • Settings file is to specify which projects are to be included in the gradle build.

  • If you see some popular gradle-based repositories such as ElasticSearch and look at its settings.gradle file, we can see that rootproject is mentioned and also multiple projects are mentioned.

How to add a task to gradle file ?

task copy(type:Copy, description:"Copying files"){
    dependsOn(jar)
    from "build"
    into "dest"
}

You can define a task and can run that task.

gradlew copy

In the definition of your task, you are saying that copy is of type Copy ( which is a predefined task in gradle ) and you are assigning values to variables such as from and into. And also, there is a dependsOn() which indicates which task needs to be run before running this particular task. So jar is again another predefined task in gradle which builds the project and creates a jar inside build directory. So, today I have learnt gradle task. So, now I can look into the tasks and understand that it can be directly invoked from command line and imagine you have a java function and you can just invoke it from command line directly, ( need to see if arguments can be passed or not )