Welcome to our comprehensive guide on Maven Dependencies! In this lesson, we'll delve into the world of Maven, a popular build tool for Java projects. Let's get started!
Maven Dependencies are external libraries or projects that your Java project relies on. They are managed and automated by Maven, making it easier to manage project dependencies.
pom.xml file. The pom.xml file is the project object model in Maven.<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-artifact</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>In the above example, com.example is the group ID, example-artifact is the artifact ID, and 1.0.0 is the version of the dependency.
mvn install
Now, let's see a practical example of using Maven Dependencies with Apache Commons Lang.
Let's create a simple Java project that uses the StringUtils class from Apache Commons Lang to reverse a string.
pom.xml file:<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>StringReverser:import org.apache.commons.lang3.StringUtils;
public class StringReverser {
public static void main(String[] args) {
String input = "Hello, World!";
String reversed = StringUtils.reverse(input);
System.out.println(reversed);
}
}StringReverser class with the following command:mvn exec:java -Dexec.mainClass="StringReverser"
Upon execution, the program should output: !dlrow ,olleH.
What does the `groupId` represent in a Maven Dependency?
Stay tuned for the next part of our Java Tutorial, where we'll explore how to manage and resolve dependency conflicts in Maven! 📝