- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 718字
- 2021-07-02 19:38:22
Installing/configuring Maven
Maven is a tool which can be used for building and managing Java-based projects. The following are some of the key benefits of using Maven as a build tool:
- It provides a simple project setup that follows best practices--it gets a new project or module started in seconds.
- It allows a project to build using its Project Object Model (POM) and a set of plugins that are shared by all projects, providing a uniform build system. POM is defined in an XML file, namely, pom.xml, which defines the project configuration and dependencies along with their versions. One can manage the dependencies version from pom.xml.
- Based on the project templates (also called archtype), Maven supports scaffolding, based on which the standard folder structure is created along with the base controller files, unit tests, and so on.
- It allows usage of the large and growing repository of libraries and metadata to use out of the box. The project dependencies are installed in the local repository specified by path, such as ${user.home}/.m2/repository. Once installed, all the logged-in users can build from this common local repository. The settings related to the local repository can be found in the settings.xml file, which can be found in one of the folders such as ${maven.home}/conf or ${user.home}/.m2. More details related to settings.xml can be found on this page: https://maven.apache.org/settings.html
- Based on model-based builds, it provides the ability to work with multiple projects at the same time. Any number of projects can be built into predefined output types such as a Java archive (JAR), Web archive (WAR), Enterprise archive (WAR), or distribution based on metadata about the project, without the need to do any scripting in most cases.
One can download Maven from https://maven.apache.org/download.cgi. Before installing Maven, make sure that Java is installed and configured (JAVA_HOME) appropriately, as mentioned in the previous section. On Windows, you could check the same by typing the command echo %JAVA_HOME%:
- Extract the distribution archive in any directory. If it works on Windows, install an unzip tool such as WinRAR. Right-click on the ZIP file, and unzip it. A directory (with the name apache-maven-3.3.9, the version of Maven at the time of writing) holding files such as bin, conf, and so on will be created.
- Add the bin directory of the created directory, apache-maven-3.3.9, to the PATH environment variable. To do that, add the bin directory to the user PATH environment variable by opening up the system properties (Windows Key + Pause), select the Advanced tab and the Environment Variables button, and then add or select the PATH variable in the user variables with the value.
- Open a new shell, and type mvn -v. The result should print the Maven version along with details including the Java version, Java home, OS name, and so on.
Now, let’s look at how can we create a Java project using Maven from the command prompt before we get on to creating a Maven project in the Eclipse IDE. Use the following mvn command to create a Java project:
mvn archetype:generate -DgroupId=com.healthapp
-DartifactId=HealthApp
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
Pay attention to some of the following in the preceding command:
- archetype:generate: This is used to create a new project from an archetype. Archetype is a Maven project templating toolkit. Executing a command the mvn archetype:generate asks the developers a set of questions for setting up the project. In order to avoid these questions, Maven provides a flag, interactiveMode, which needs to be set to false.
- groupId: This represents the name of the package.
- artifactId: This represents the name of the project.
- archetypeArtifactId: This represents the name of the template from which the project is scaffolded.
- interactiveMode: This,when set to false, avoids the interactivity of the Maven Archetype plugin.
With the archetype:generate and -DarchetypeArtifactId=maven-archetype-quickstart templates, the following project directory structure is created:

In the preceding diagram, the healthapp folders within the src/main and src/test folders consist of a Hello World program named as App.java, and a corresponding test program such as AppTest.java. Also, a pom.xml file is created in the top-most folder.
In the next section, we will install the Eclipse IDE, and create a maven project using the functionality provided by the IDE.