The Ultimate Guide to Maven: Everything You Need to Know

Introduction to the Maven:

Maven, A Java-based open source project management and build tool. Maven can manage a project's build, reporting, and documentation based on the concept of a Project Object Model (POM). .net applications will not be supported by Maven. 

One useful feature is the ability to automatically download project dependency libraries from the Maven central repository.

Minimal POM:
Let's talk  about POM for a minute, the minimum requirements for POM are as follows.

<Project>

<modelVersion>4.0.0</modelVersion>

<groupId>com.chandrasekhar.info</groupId>

<artifactId>myapp</artifactId>

<version>1</version>

</project>


In the above example the fully qualified artefact namewas: 
 "com.chandrasekhar.info:myapp:1".   

The project> tag is required in all POM files, as are three mandatory values or attributes: groupId, artifactId, and version.

Here are some connected articles.

groupId: This is the project group's id and relates to a fully qualified organisation.
com.chandrasekhar.info is one group; com.bank.info is another; and so on. 

artifactId: This is the project's identifier or name.

version: This is the project's version, which is used to distinguish it from others in an artefact repository.
1.0 first version; 1.1 revised version.

Maven is heading to Create with three different sorts of packages, as detailed below.

We may create three types of applications with Java.

Standalone -jarThis contains only java code. 

ExampleHelloWorld.java javac is an example. HelloWorld.java will compile and transform to machine-readable code (byte code).  The JVM can only understand byte code.

If you run standalone Jar you will see that our app is running.
java HelloWorld       -Running

Web app-war
Java+UI code –Web Content
Enterprise Applications-ear
We use Enterprise apps when the programme is large and has several modules.

Modules + Jar + War + Web Content
Please bear in mind that when installing any software, the version compatibility must be checked.

Assume you have JDK 8 -maven 3.x and JDK 7 -maven 2.x installed
Maven should be downloaded and installed. While downloading, it will deliver a zip or tar file;

Maven Repository:

Dependencies will be stored in one of these repositories.

1. Maven Local Repository
   C:\Users\chandrasekhar\.m2\repository
   we can change this in settings.xml file in maven conf directory.
   
   <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

2. Maven Central Repository 
Maven will get project dependencies from the Maven Local Repository by default; if the Maven Local Repository is not found, Maven will get it from the Maven Central Repository.
 
    Maven Central Repository URL – https://repo.maven.apache.org/maven2
    Maven Central Repository Search – https://search.maven.org/

3. Maven Remote Repository.
Some libraries are only available in the Java.net or JBoss repository (remote repository) and are not saved in the Maven Central Repository.
   
You must make some changes to the pom.xml file. 
  
   1. Repository for Java.net
   2. repository for JBoss
   3. Spring storage

Maven Life Cycle:

To deploy and distribute the target project, the Maven build goes through a specified life cycle.

There are three pre-programmed life cycles:
The primary life cycle is the default because it is in charge of project deployment.

clean: to remove all files generated by the last build site and clean. 
project: to generate site documentation for the project
   

Default: Phase

validate

Validate the project is correct and all necessary information is available.

compile

Compiles the source code of the project.

test

Test the compiled source code using a suitable unit testing framework

package

Take the compiled code and package it in its distributed format, such as a JAR.

verify

Run any checks on results of integration tests to ensure quality criteria are met.

install

Install the package into the local repository, for use as a dependency in other projects locally.

deploy

Done in the build environment, copies. 



Project execution:
1. Standalone-jar 

  mvn clean package or
  mvn clean package -DskipTests
  mvn clean package -Dmaven.test.skip=true
   
  cd java-project
  mvn package
  java -jar target/java-project-1.0-SNAPSHOT.jar 123456
 
2. Web -war 
    Make sure that packaging as a war in pom.xml file.
 

3. Enterprise 
    Download one example and execute on your own.


Conclusion:

We learned about maven introduction, project object model POM, maven repository, and maven lifecycle methods in this article.

Keep an eye on this site for additional updates!

Chandra Sekhar

A Blogger and Author! This post was written and edited by me, a technologist. I started this site to share my inspirations, work, and free materials, which I hope others may find useful as well. Man with a creative streak who enjoys experimenting with various web design strategies.

Previous Post Next Post

Microservice Communication in a Distributed System