- Gradle is Partly Declarative and Partly Programmatic and Uses Domain Specific Language(DSL) coded in groovy or kotlin in build.gradle
- Gradle Parses build.gradle file and constructs DAG(Directed Acyclic Graph) and constructs a graph of task
- Gradle executes tasks in order.If ouput of task is same as last output, then task wont be executed
- Gradle manages trasitive dependency on its own(Similar to maven) and gradle uses repositories.
- Gradle updates to new version of Dependencies
Teardown of build.gradle file
- To make gradle, java aware we need to add apply plugin:’java’
- Once the above line is added, the following tasks get tagged to project – clean, assemble, compile, test
- dependencies and repositories are added in flower bracket like one below
- under dependencies we can have multiple tasks. In the below code compile is added as a task
build.gradle
group 'com.mugil.employeeManagement' version '1.0-Snapshot' apply plugin: 'java' sourceCompatibility=1.8 repositories { mavenCentral() } dependencies { compile 'com.google.code.gson:gson:2.8.4' }
What is the Difference between gradlew and gradle?
gradlew is a wrapper(w – character) that uses gradle.
Under the hood gradlew performs three main things:
- Download and install the correct gradle version
- Parse the arguments
- Call a gradle task
Using Gradle Wrapper we can distribute/share a project to everybody to use the same version and Gradle’s functionality(compile, build, install…) even if it has not been installed.
To create a wrapper run:
gradle wrapper
This command generate:
gradle-wrapper.properties will contain the information about the Gradle distribution
How to skip Gradle Test?
gradle build -x test
How to supply Argument to Spring Controller during application startup in Gradle Wrapper
gradlew bootRun --args='--welcome.message=Mugil'
WelcomeController.java
In the below code welcomeMsg variable is assigned value during application startup. In similar ways environment variables could be setup
by supplying arguments as parameters while executing gradlew bootrun
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WelcomeController { public String welcomeMsg; @Autowired public WelcomeController(@Value("${welcome.message}") String message) { welcomeMsg = message; System.out.println("Hi "+ message); } @GetMapping("/") public String sayHello() { return "Mugil"; } }