Creating a Kafka Java Project using Maven (pom.xml)
Learn how to create a Java project using Maven that can interact with Kafka cluster
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:
Follow these steps to create a Java project with the above dependencies.
In IntelliJ IDEA, create a new Java maven project (File > New > Project)
Then add your Maven project attributes
The build tool Maven contains a
pom.xml file. The
pom.xml is a default XML file that carries all the information regarding the GroupID, ArtifactID, as well as the Version values. The user needs to define all the necessary project dependencies in the
pom.xml file. Go to the
Define the Kafka Dependencies. Create a
<dependencies>...</dependencies> block within which we will define the required dependencies.
Add a dependency for Kafka client as shown below
If the version number appears red in color, it means the user missed to enable the 'Auto-Import' option. If so, go to View > Tool Windows > Maven. A Maven Projects Window will appear on the right side of the screen. Click on the 'Refresh' button appearing right there. This will enable the missed Auto-Import Maven Projects. If the color changes to black, it means the missed dependency is downloaded.
Add another dependency for logging. This will enable us to print diagnostic logs while our application runs.
Now, we have set all the required dependencies. Let's try the Simple Hello World example.
Create a java package say,
While creating the java package, follow the package naming conventions. Finally, create the sample application program as shown below.
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 successfully.
Expand the 'External Libraries' on the Project panel and verify that it displays the dependencies that we added for the project in
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.