Creating and Executing a Gradle Project
Creating and Executing a Gradle Project
To Create a Gradle Project:
- Navigate to the required directory >> Open Command prompt >> Run "gradle init" command
- Next it will ask for the type of build to generate: 1. Application 2. Library 3. Gradle plugin 4. Basic
- Next it will ask to select Implementation language: 1. Java 2. Kotlin 3. Groovy 4. Scala 5. C++ 6. Swift
- Next, it will ask for Target java version
- Next, Select Application structure: 1. Single Application Project 2. Application and Library Project
- Select Build Script DSL: 1. Kotlin 2. Groovy >> Select Groovy here
- 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:
- Open command prompt and run cmd - 'gradlew build'
- 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
- Running the application from the jar: Run the command - 'java -jar build/libs/app.jar'. Notice following error is displayed:
- Now, Re-build the project by running command 'gradlew build' again.
- Run command - 'java -jar build/libs App.jar'
Comments
Post a Comment