guycalledavinash / maven

Let's talk about Maven

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preface

It is not a secret that java files have a .java extension

A project has many .java files which can't be executed directly as computers only understand binary language

So, the java compiler (javac) compiles source code into bytecode creating .class files, which are organised into packages

process

To deploy java project, we should package all .class files (bytecode) to JAR file or WAR file

		JAR : Java Archive
		WAR : Web Archive

The standalone java projects are packaged as a JAR file (a stadalone application is where the project runs only on one computer)

The web applications are packaged as a WAR file where multiple users can access a web applications at a time.

Maven

Maven is an open source build automation software developed by Apache

It is limited to java projects

Maven can create initial project folder structure

Typically a java project is not built using java alone, to make things easy, there are other frameworks and libraries involved like springboot, hibernate, kafka, redis, etc

They are called project dependencies which are to be downloaded but thanks to Maven, it does it for us.

Required dependencies are stored in "pom.xml" file where maven downloads them (pom: project object model)

The pom.xml file is created automatically, acts as an input file when we create a maven project

Maven compiles and packages the project code

maven

Pre-requisite

Since maven is java based, jre & jdk should be installed locally, along with maven of course

Once the installation is done, it is mandatory to set the environment variables for jdk and maven

  1. Go to Environment Variables from search bar and click on 'Environvent Variables' option: 1

  2. Select JAVA_HOME from the system variables, edit that: 2

  3. Change variable name and value 3

  4. Now its time to set the path of bin directory. Select path and click edit: 4

  5. Click new, add the path of bin directory of jdk 5

  6. Open cmd, check the java version to confirm java -version

  7. Download maven from official source, extract and paste in the C drive

  8. Set environment variables and path for maven like we did to java

Maven project:

To create a standalone appliaction:

mvn archetype:generate -DgroupId=in.JohnySins -DartifactId=01-Maven-App -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

To create a web application:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=in.Donaldtrump -DartifactId=01-maven-web-app -DinteractiveMode=false

This creates a project folder structure which includes src folder and pom.xml file like this:

str

Now, to execute commands, make sure to do it where there's pom.xml file, so change pwd

To compile the project, do this from project folder:

mvn compile

This creates a target folder where multiple .class files are created.

In production environment there're many .class files, which are to be packaged

mvn package

All the bytecode is packaged to jar or war files (jar in this case) jar

This is the outcome: jaar

Maven goals

clean: deletes target folder

compile: compiles source code

test: executes Junit tests (unit test code)

package: packages bitecode to jar or war

install: installs project in repo

deploy: deploys the

package = compile + test + package

install = compile + test + package + install

deploy = compile + test + package + install + deploy

Dependencies

Maven will download dependencies using a repository

-There are 3 types of repositories

  1. Central Repository

  2. Remote Repository

  3. Local Repository

Central repository is maintaining by apache organization

Every company will maintain their own remote repository

Local repository is created in the system (Location : C://users//.m2)

When the maven goals are executed, the system will search for dependencies, for which it will search in local repo first, if not central.

In production, remote repositories are used to store artifacts.

To know more about them, checkout Nexus Repository

About

Let's talk about Maven