Creating a Kafka Java Project using Gradle (build.
gradle)
Learn how to create a Java project using Gradle that can interact with Kafka cluster.
Gradle is a popular choice for Kafka projects in Java
Before developing Kafka producers and consumers in Java, we’ll have to set up a simple Kafka Java
project that includes common dependencies that we’ll need, namely:
Kafka dependencies
Logging dependencies
Follow these steps to create a Java project with the above dependencies.
Creating a Kafka Gradle project with [Link] and setting up dependencies
In IntelliJ IDEA, create a new Java Gradle project (File > New > Project) Then add your Gradle
project attributes The build tool Gradle contains a **[Link]** file. The [Link] is a
default Gradle file that carries all the information regarding the Group and Version values . The user
needs to define all the necessary project dependencies in the [Link] file. Go to
the [Link] file. Define the Kafka Dependencies in the dependencies { ... } [Link] a
dependency for Kafka client and logging dependencies as shown below
Copy
Ask AI
dependencies {
// [Link]
implementation '[Link]:kafka-clients:2.8.1'
// [Link]
implementation 'org.slf4j:slf4j-api:1.7.32'
// [Link]
implementation 'org.slf4j:slf4j-simple:1.7.32'
testImplementation '[Link]:junit-jupiter-api:5.6.0'
testRuntimeOnly '[Link]:junit-jupiter-engine'
Load the Gradle changes with the menu from the right hand side to import the dependencies
Now, we have set all the required dependencies. Let’s try the Simple Hello World example.
Creating your first class
Create a java package say, [Link] While creating the java
package, follow the package naming conventions. Finally, create the sample application program as
shown below.
Copy
Ask AI
package [Link];
import [Link];
import [Link];
public class HelloWorld {
private static final Logger log = [Link]([Link]);
public static void main(String[] args) {
[Link]("Hello World");
Run the application (the play green button on line 9 in the screenshot below) and verify that it runs
and prints the message, and exits with code 0. This means that your Java application has run
[Link] the ‘External Libraries’ on the Project panel and verify that it displays the
dependencies that we added for the project in [Link] file We have created a sample Java
project that includes all the needed dependencies. This will form the basis for creating Java producers
and consumers next.