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
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 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
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
-
Go to Environment Variables from search bar and click on 'Environvent Variables' option:
-
Now its time to set the path of bin directory. Select path and click edit:
-
Open cmd, check the java version to confirm
java -version
-
Download maven from official source, extract and paste in the C drive
-
Set environment variables and path for maven like we did to java
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:
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)
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
Maven will download dependencies using a repository
-There are 3 types of repositories
-
Central Repository
-
Remote Repository
-
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