In Maven, you can manage dependencies using a pom.xml file. Here's how you can add dependencies to your pom.xml file:

  1. Open your pom.xml file in a text editor or an integrated development environment (IDE).
  2. Locate the <dependencies> tag in your pom.xml file. If it does not exist, you can add it after the <project> tag.
  3. Add a new <dependency> tag for each dependency you want to add. The <dependency> tag contains information about the artifact that you want to use, such as the group ID, artifact ID, and version.Here's an example of how to add the JUnit dependency:xmlCopy code<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> In this example, we're adding the JUnit dependency with group ID junit, artifact ID junit, and version 4.12. The <scope> tag specifies that this dependency is only needed for testing.
  4. Save your pom.xml file.
  5. Finally, run mvn install or mvn compile command in your terminal or command prompt to download and install the dependencies specified in your pom.xml file.

That's it! Your Maven project now has the necessary dependencies to build and run successfully.

When you build a Java project with Maven, by default it creates a packaged output called a JAR (Java ARchive) file. This JAR file includes all the compiled Java classes and resources, along with any dependencies that you have specified in your pom.xml file.

To include the dependencies in the JAR file, you can use the Maven Assembly Plugin or the Maven Shade Plugin. Here's how you can use each of these plugins:

Maven Assembly Plugin

  1. Add the Maven Assembly Plugin to your pom.xml file by adding the following code to the <plugins> section:xmlCopy code<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> This configures the Maven Assembly Plugin to create a JAR file with dependencies included.
  2. Run the mvn package command to build your project and create the JAR file with dependencies included.

Maven Shade Plugin

  1. Add the Maven Shade Plugin to your pom.xml file by adding the following code to the <plugins> section:xmlCopy code<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.MainClass</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> This configures the Maven Shade Plugin to create a JAR file with dependencies included.
  2. Run the mvn package command to build your project and create the JAR file with dependencies included.

After running either of these commands, you should have a JAR file with all the necessary dependencies included. You can find the JAR file in the target directory of your project.