This plugin provides the capability to package the artifact in an uber-jar, including its dependencies, and shade - i.e., rename - the packages of some of the dependencies. We need to add a plugin that will achieve this task. We got this error because Maven, by default, doesn't package external dependencies. jar file we got the output below: Error due to missing dependency in packaged file Package the application and run the launch the generated. Static Logger logger = LoggerFactory.getLogger(App.class)
Update the App.java to make it look like this: package com.tericcabrel Run mvn dependency:resolve to install the dependency. Update the pom.xml to add these two dependencies required for SLF4J: We will install SLF4J who is one of the most popular libraries.
Let's say we want to add a library to improve logging into the application. To execute the generated file, run the command below: java -cp target/ Run the JAR file in the command line Work with external dependency Maven will run the tests, and if all pass, it will package the application as a JAR and make it available in the folder called target. Run the command below to do that: mvn package
Build and run the projectĪfter writing the code in your project, you need to package and distribute it. Run mvn install to make sure there are no errors. Replace the content of the file with the code below: package com.tericcabrel Since the Junit version has been updated, the imports in the file AppTests.java are broken. Run the command below to update the dependency: mvn dependency:resolve Let's update it to the latest version and also set the JDK version to operate. If we open the file pom.xml, we will see that it contains one dependency called Junit used to run unit tests. Structure of a simple Java project generated with Maven The project was created successfully, and here is the structure. mvn archetype:generate -DgroupId=com.tericcabrel -DartifactId=starter -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false Let's create the project with the command below you can replace the groupId and the artifactId on your own. The interactive mode set to false prevents Maven from asking questions about project configuration properties like the application version. The command signature to create a project is: mvn archetype:generate This page list some Maven archetypes for a simple project, the Archetype maven-archetype-quickstart is enough. It is helpful in quickly bootstrap a project in an organization where many teams are working on a project and need a standard structure to stay consistent in all the projects. The Maven Template is also known as Archetype, is a project with a predefined structure. Create a Java project from Maven Template Maven CLI successfully installed! We also see the JDK version installed. Now verify that your system recognizes the Maven command by running. Save and exit, then reload your profile file: source. bash_profileĪppend the following lines at the end of the file : export M2_HOME=$HOME/apache-maven-3.8.3 zshrc if you use Oh My Zsh as the custom shell. The file containing these variables can be. The next step is to register Maven in the environment variables path. You will find a folder called apache-maven-3.8.3 in your home directory.
Once the download is completed, let's unpack the file in the home directory: # we are inside the download directory Go to the download section of the Maven website and click on the link to download the compressed version of Maven. I will show the installation process for MacOS. Maven can be used on Linux, MacOS, Windows. Check out this link to see how the install it on your operating system. To follow this tutorial, you need to have the JDK greater or equal to version 8. In this post, we will see how to set up a Java project with Maven. It provides predefined steps where specific tasks can be executed like run unit and integration tests, package the application for product, generate the project documentation, etc. Typically, it manages project dependencies and application configuration. Maven is a build automation tool used for Java projects.