Maven
Now that you know that most software development projects depend on other external libraries for building, there are some obvious pain points that are associated with this dependency, which are listed below:
- If you are building the software with a few other team members, then every one of them must download the library JAR file and add it to their
srcfolder. Alternatively, you also have to push the JAR files to the repository so that when your team member clones the repository, they get all the JAR files associated with the project. - When the library goes through a revision, you have to delete your current JAR file in the
srcfolder, download the latest version, and re-add it to thesrcfolder. - You have to keep all the various URLs that host the various libraries so that you can go there and fetch them as required.
- Once you are ready to build and deploy, you should run the executables to create
jarand/orwarfiles and copy them over to Development, Test, and Production environments as appropriate for your organization.
All of these steps are pretty painful, tedious, and error-prone. Since these are just tasks, there should be a way to automate them, and that is exactly what Maven does.
Reference: https://maven.apache.org/what-is-maven.html
What to do next?
Converting an existing project to a Maven project: To convert any existing project to a Maven project in Eclipse, just select the project at the top level and then choose 'Configure' --> 'Convert to Maven project'.

The next screen will have auto-populated values for various required fields that it figures out automatically. In case it can't find the values, it will prompt you to fill in those fields. The various mandatory fields are groupId, artifactId, version, and packaging. The 'name' and 'description' are optional. It then creates a pom.xml file in the root directory of the project that contains all the fields that were just set and also creates a target folder where all the compiled classes are stored.
How Maven Helps
- It automatically downloads the specific library file as detailed in the dependency section of the
pom.xmlfile. - The downloaded library JAR files are not part of the project folder but will be kept in a separate .m2 repository that is typically created in the home directory of the computer.
- It helps build the
jar,war, andearfiles as per the JEE standards. It will then grab all the necessary files from the.m2repository and add them to thelibfolder as required by JEE standards. - If multiple projects are using the exact same version of various libraries, then they all use the same copy of the library that is stored in the
.m2repository instead of downloading them for each one separately. - Maven also supports plugins that can help in doing many more tasks, like deploying to cloud servers, etc.
More on the elements and fields in pom.xml
The following are mandatory elements in the pom.xml file:
modelVersion: Maven uses the Project Object Model as a descriptor for the declarative build requirements of a project. The Project Object Model consists of a top-level <project> tag with child elements.
And the correct value to be set is 4.0.0 for Maven 4.0.
groupId: Here you set the package where the main Java files will be placed. The folder structure would be artifactId/src/main/java/groupId-package-setting-value.
This configuration setting, together with artifactId and version, is a unique identifier for your library.
So what it means is, after you package your project as a JAR and publish it to the world, other developers can import it to their project by specifying the correct groupId, artifactId, and version in their pom.xml.
However, note that package is just the internal root package used by your project, and it will not affect how a developer locates and imports your JAR into their project. The package and the groupId can be different.
But care should be taken to ensure that the package does not conflict with other well-known libraries (e.g., don't name it java.lang or org.springframework, etc.).
version: This defines the version of the package. By default, the first version will be 0.0.1-SNAPSHOT, where SNAPSHOT just means that this version is still in development and has not been released yet. Once you decide to release this for other users, then you remove the SNAPSHOT and give it a version number. At that point, everyone has a reference to
all the features and functionalities that are stable in that particular version.
artifactId: This is the name of the web application as well as the directory in which the application is created.
This is also used to name the package, and the extension of this package would be jar, war, or ear, depending upon what is set in the packaging element.
The rest of the elements are optional, and here are a few more elements typically seen in a pom.xml file:
parent: This element defines a parent POM that this POM can inherit from.
packaging: This defines if you want to package the compiled classes as a jar, war, or ear file as appropriate.
dependencies: Although this is an optional element, any real Java project cannot be written without depending upon other libraries, and hence this element will be
there in any meaningful Java project. This top-level dependencies element will have one or more child dependency elements that define the necessary libraries.
Once the initial elements are in place, typically developers tweak this element more frequently than anything else in this file.
Maven downloads the listed dependencies from the Maven Central repository and keeps them in the .m2 folder of the user's home directory. During packaging, Maven drops the listed JAR files
in the lib folder.
If a dependency is depending on other libraries for it to function, Maven brings in the dependencies of those dependencies (transitive dependencies) as well.
Creating a new mvn project: Although you can convert an existing project to a Maven project, all the required folder structure standards are not created automatically. You will have to manually fix the issues.
Instead, you can create a brand new Maven project by choosing one of the starter projects from the various repositories that are registered with Maven Central. Maven Central is a central repository into which all the open-source jar/war files can be uploaded. Once uploaded to Maven Central, anyone can download the JAR files by just adding the dependency in their respective pom.xml.
We will now create a new Spring Boot servlet project by choosing the respective starter artifact, as shown in the screens below.
To get started, choose File --> New --> Maven Project.

Wait for a few seconds for it to fetch all the projects, as shown below:

Use keywords to filter and find the Spring Boot starter project:

Complete the creation by filling in the necessary field values. Upon completion, you will see the file structure below, which is different from our Dynamic Web Project:

Maven Standard File Structure
The following is the file structure in a typical Maven project:
- All the source files are kept in
src/main/java, and all the packages start from this root. - All the Java classes that make up the test cases are saved in
src/test/java, and the packages here should be exactly the same as in thesrcfolder. - The compiled files are in the target folder.
- In Eclipse, you will see a Libraries tab under Java Resources which displays all the JAR files that are inserted in the dependency section of your
pom.xml. They are referenced from the.m2repository.

Once created, you can now deploy.
When you deploy, watch out for the following:
- See all the log messages on the console and try to understand all the different objects that it is instantiating and adding into the runtime.
- As the
pom.xmlhas a dependency with theartifactIdspring-boot-starter-security, Spring Boot automatically adds security features to your web application. - The password for the username 'user' will be printed out on the console. You need to use this to log in to your application.
Now invoke the web application at http://localhost:8080, and when prompted, use:
Username: user Password: whatever is logged on the console
With this, you will be able to see 'Hello World' printed from the Servlet. Note that it is the same servlet that will be invoked for any path you choose, and that is because of the corresponding annotations in the servlet.
Creating a Sample Maven Project from the Terminal
You can also create a Maven project from one of the many available templates. Here is one example:
mvn archetype:generate -DgroupId=com.mbcc.tutorial -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
This will create a folder called my-app under which there will be a pom.xml. It also creates the folder structure as required for Maven.
You will also see many entries in the plugin section of the pom.xml file.
Maven is really a framework that allows you to add many Maven Plugins. Plugins are where much of the real action is performed.
Plugins are used to: create JAR files, create WAR files, compile code, unit test code, create project documentation, and on and on.
Almost any action that you can think of performing on a project is implemented as a Maven plugin.
Even if some of the main plugins (clean, compile, surefire for testing, etc.) are not added, Maven still creates the basic artifacts and executes tasks, for e.g., creating the JAR, running the test cases, etc.
However, if you want a specific version of the plugin to be used for your project, then it is a good idea to have them in the pom.xml.