Maven Tutorial
Maven in IDE
In this tutorial, you will learn about different methods of delivering dependencies in a Maven project. Delivering dependencies means packaging them with your project so that they can be used in various environments, such as production or deployment.
1. Including dependencies in the packaged artifact (default)
By default, Maven includes all compile
and runtime
scoped dependencies in the packaged artifact (e.g., JAR, WAR). This ensures that your application has all the required libraries when deployed or executed in a different environment.
For a WAR project, the dependencies are placed in the WEB-INF/lib
directory of the WAR file. For a JAR project, the dependencies are stored in the local Maven repository, and the classpath is set up to include them when running the application.
2. Creating an "uber" or "fat" JAR
An "uber" JAR, also known as a "fat" JAR, is a single JAR file that contains your application's compiled classes along with all its dependencies. This method makes it easy to distribute and run your application, as the user only needs to have Java installed and run the uber JAR.
To create an uber JAR, you can use the maven-shade-plugin
. Add the following configuration to your pom.xml
file:
<build> <plugins> <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> </plugins> </build>
Replace com.example.MainClass
with the fully-qualified name of your main class. Build your project using mvn clean package
, and Maven will create an uber JAR in the target
directory.
3. Using Maven Dependency Plugin to create a directory with dependencies
You can use the Maven Dependency Plugin to create a directory that contains your project's dependencies. This is useful if you want to distribute your application with its dependencies without bundling them into a single JAR.
Add the following configuration to your pom.xml
file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
Build your project using mvn clean package
, and Maven will create a lib
directory in the target
directory with all your project's dependencies.
In conclusion, this tutorial covered different methods of delivering dependencies in a Maven project, including the default method, creating an uber JAR, and using the Maven Dependency Plugin to create a directory with dependencies. Choose the method that best suits your project's needs and deployment requirements.