MAVEN FOR JAVA

What is Maven?

It is a build and dependency management for Java

Let's get started 

Maven is a built tool used as java's build management tool.


Let's say you want to create a new project, in which you want a project structure, you want something that will compile your files, that will test your applications and something which will give you the required libraries. So for that Maven is the answer.

Java's most commonly used build management tool is Apache Maven.

The main problem arises when you want to build an application using spring or hibernate. For those, we require some dependencies i.e, libraries. So for that one can go to a website spring.io to download all the jar files. But the problem arises if you download the libraries for the current version say 4.2.4 and in future, it is updated to say 5, then again you have to download the entire library jar files by yourself as a developer.
So Maven is someone to whom you can mention to all that downloads in your system.


HOW MAVEN WORKS

Unlike other great tools, Maven takes what was once too hard (configuration hell) and makes it easier for digestible parts to navigate. Maven is made up of three constituents:

  • The POM(project object model ): The file detailing a Maven project and its addictions.                 All 
    Java project that uses Maven has its root directory with a POM file.
     
    The pom.xml 
    describes the dependencies of  the 
    project, and tells us how to create it.
A SIMPLE MAVEN POM :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javaworld</groupId>
  <artifactId>what-is-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Simple Maven Project</name>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

  • The directory: The structured format used to define a Maven project within POM.

  • Repositories: Where software is stored and found by third parties.

Hosting your project in a Maven repository

While building a project that will be a dependency--say, creating a library for other projects to use-- So one  will need to make it available in one of four ways:

  1. Make it available locally.
  2. Publish to a privately managed remote repository.
  3. Publish to a cloud-based private repository.
  4. Publish to a public repository like Maven Central.

Build the Maven package

If we create a pom.xml and put it in a directory, we will be able to run Maven commands against it. Maven has multiple numbers of commands, and more are available via a plugin, but we only need to know a handful to start.

For your first command,  we can try executing mvn package

Maven's directory structure

When the command is done, we can see that Maven has created a /target directory, which is the standard location for any of your project's output. Dependencies that we've downloaded will reside in the /target directory, along with our compiled application.

In the next step, we want to add a Java file, which we can place in the Maven src/ directory. Create a /src/main/java/com/javaworld/Hello.java file, 

 Hello.java


com.javaworld

public class Hello {
  public static void main(String[] args){
    System.out.println("Hello, JavaWorld");
  }
}


Starting a new project: Archetypes in Maven and Spring

Maven archetype is a prototype focused on 
a number of predefined settings to start new projects. Archetype offers pre-packaged dependencies, such as a web application project for Java EE or Java.
The Spring system, which fits well with Maven, provides 
additional, advanced capabilities to
 get new ideas off the ground. 
Spring Initializr, for example, is tool that lets you 
identify the 
elements you want in new app really quickly. 
Initializr is not an archetype of Maven, but it does serve the purpose to generate a project layout based on upfront specifications. 

Adding dependencies

You'll need to add new functionality to an existing project 
from time to time while you create or manage apps. 
Maven makes it very easy to incorporate new 
features. 
Let's assume we are working on a project, and realize that we need some Apache Commons Language Library utilities, After JUnit, we can easily insert dependency into the original POM register.
 





Comments