Step 1: Installing Gradle
Before you can use Gradle, you need to install it on your system.
Windows
Download Gradle:
- Go to the Gradle releases page and download the latest binary release.
Extract the archive:
- Extract the downloaded ZIP file to a location of your choice (e.g.,
C:\Gradle
).
- Extract the downloaded ZIP file to a location of your choice (e.g.,
Configure environment variables:
- Open the Environment Variables window.
- Add a new system variable
GRADLE_HOME
with the valueC:\Gradle\gradle-x.x.x
(replacex.x.x
with the version number). - Add
%GRADLE_HOME%\bin
to thePath
system variable.
Verify the installation:
- Open a new Command Prompt window and type
gradle -v
to verify that Gradle is installed correctly.
- Open a new Command Prompt window and type
Mac
Using Homebrew:
- Open a terminal window and run the following command:
brew install gradle
- Open a terminal window and run the following command:
Verify the installation:
- Run
gradle -v
to ensure Gradle is installed correctly.
- Run
Linux
Download Gradle:
- Go to the Gradle releases page and download the latest binary release.
Extract the archive:
- Extract the downloaded ZIP file to
/opt/gradle
.
- Extract the downloaded ZIP file to
Configure environment variables:
- Add the following lines to your
.bashrc
or.bash_profile
:export GRADLE_HOME=/opt/gradle/gradle-x.x.x export PATH=$PATH:$GRADLE_HOME/bin
- Add the following lines to your
Reload the profile:
- Run
source ~/.bashrc
orsource ~/.bash_profile
.
- Run
Verify the installation:
- Run
gradle -v
to ensure Gradle is installed correctly.
- Run
Step 2: Setting Up a New Gradle Project
To create a new Gradle project, follow these steps:
Create a new directory for your project:
mkdir gradle-java-example cd gradle-java-example
Initialize a new Gradle project:
gradle init --type java-application
This command sets up a basic Java project with the necessary files and directories.
Step 3: Configuring the build.gradle
File
The build.gradle
file is the core of your Gradle project. It contains the configuration and dependencies for your project. Open the build.gradle
file and add the following content:
plugins {
id 'java'
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.google.guava:guava:31.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
application {
mainClassName = 'com.example.App'
}
test {
useJUnitPlatform()
}
Explanation:
- Plugins: Apply the Java and application plugins.
- Repositories: Use Maven Central to resolve dependencies.
- Dependencies: Define dependencies for your project.
- Application: Specify the main class of your application.
- Test: Configure the test task to use JUnit 5.
Step 4: Creating the Application Source Code
Create the application source code in the src/main/java/com/example
directory. Create a file named App.java
with the following content:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step 5: Creating Test Cases
Create a test class in the src/test/java/com/example
directory. Create a file named AppTest.java
with the following content:
package com.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AppTest {
@Test
void testApp() {
assertEquals("Hello, World!", "Hello, World!");
}
}
Step 6: Building the Project
To build your project, run the following command:
gradle build
Gradle will compile the source code, run the tests, and package the application into a JAR file.
Step 7: Running the Application
To run the application, use the following command:
gradle run
This will execute the main
method in the App
class, and you should see the output Hello, World!
in the console.
Step 8: Generating JAR Files
To generate a JAR file for your application, run the following command:
gradle jar
The JAR file will be created in the build/libs
directory.
Step 9: Running Tests
To run the tests, use the following command:
gradle test
Gradle will execute the tests and generate a report in the build/reports/tests/test
directory.
Conclusion
Automating your Java builds with Gradle is straightforward and provides many benefits, including improved build performance and dependency management. By following this guide, you have set up a new Gradle project, configured dependencies, created source and test code, and automated the build process. This setup will help you streamline your development workflow and ensure your project builds and tests run smoothly.
For more detailed information on Gradle, refer to the Gradle User Guide.
Comments
Post a Comment
Leave Comment