Creating and Executing a Gradle Project

 Creating and Executing a Gradle Project


To Create a Gradle Project:
  1. Navigate to the required directory >> Open Command prompt >> Run "gradle init" command
  2. Next it will ask for the type of build to generate: 1. Application 2. Library 3. Gradle plugin 4. Basic
  3. Next it will ask to select Implementation language: 1. Java 2. Kotlin 3. Groovy 4. Scala 5. C++ 6. Swift
  4. Next, it will ask for Target java version
  5. Next, Select Application structure: 1. Single Application Project 2. Application and Library Project
  6. Select Build Script DSL: 1. Kotlin 2. Groovy >> Select Groovy here
  7. Select Test Framework: 1. JUnit 2. TestNG 3. Spock 4. JUnit Jupiter


A Gradle project containing following files will be created:


Executing/Building a Gradle Project

  • The Gradle Build process Compiles, Tests and Packages a project into a deployable unit that can be run in the target environment
  • Gradle expects Java code to be present in src/main/java folder and tests in src/test/java project
  • After creating the Gradle project using 'gradle init' you will see gradlew file which contains the gradle wrapper. This allows end-user to run Gradle applications without installing Gradle on the target environment

Steps to build a Gradle Project:

  1. Open command prompt and run cmd - 'gradlew build' 
  2. Notice that a new folder named 'build' is added to the project after build process. This build folder contains all the compiled classes and a jar with the name of the Java Class. Consider, we have a class named App.java in src folder, then a jar with name App.jar will be created in the build/libs folder
  3. Running the application from the jar: Run the command - 'java -jar build/libs/app.jar'. Notice following error is displayed: 
     To fix this error write this code to in build.gradle file. Main-class attribute tells what jar file to pickup


  4. Now, Re-build the project by running command 'gradlew build' again.
  5. Run command -  'java -jar build/libs App.jar'
  6. Output: 



           

Comments

Popular posts from this blog

Jenkins CICD in One Page

Why do we need a build tool?

Deutsche Bank Interview Questions - 2024