How to install the maven ?
Maven is a build tool that is purely written in java language and requires java to be installed on the machine to use it.
The latest version of the maven: apache-maven-3.9.9 and the minimal jdk requires is: jdk 8+
1. Windows operating system:
In Windows maven is distributed as a binary distribution, so we can download the binary distribution and add to the SYSTEM PATH to use it directly.
1. download https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.zip
2. extract and place the extracted directory under c:\ drive (for eg.. when we extracted the above downloaded file, it will be extracted into apache-maven-3.9.5 directory)
3. goto windows environment variables and create #2 ENV variables
3.1 M2_HOME=c:\apache-maven-3.9.5 (optional)
3.2 MAVEN_HOME=apache-maven-3.9.5 (optional)
append the MAVEN to the SYSTEM PATH variable as PATH=%PATH%\%MAVEN_HOME%\bin
4. test the maven installation by running the below command from the command-prompt
mvn --version
2. Ubuntu or Mac operating system:
2.1 install the minimal jdk version required to run the maven using the below commands:
ubuntu: apt update -y
apt install -y openjdk-17-jdk
mac: homebrew update -y
homebrew install -y openjdk-17-jdk
incase of ubuntu or mac, the maven is an packaged software distribution that is distributed through platform repositories like ubuntu repo etc. So we dont need to download and extract to use, rather we can use the relevant package manager of the platform as below:
ubuntu: sudo apt install -y maven
mac: homebrew install -y maven
-----------------------------------------------------------------------------------------------------------------------------
How to build the java project using maven?
• From the previous discussions we had above, we can understand we don't have to write any build script file for building the maven applications all that we need to do is
1. create the project using Maven standardized directory structure
2. provide the information about the project like name, type etc to the maven, so that it can takecare of applying the standard build activities that should applied for the java project for building/packaging the application
1. create the java project with standardized directory structure
project (root directory)
|-src
|-main
|-java (sourcecode)
|-resources (non-java sourcecode)
|-test (unittest code)
|-java (java sourcecode)
|-resources (non-javasourcecode)
In the above project directory structure we see #2 source directories
1. main
2. test
• In the project we write sourcecode and unittest sourcecode also for testing the actual code of the project. when we are packaging and delivering the application to the customer, we want to exclude the unittest code aspart of the application package (jar/war/ear). Since the maven builds the project and packages it, to let the maven identify and include only the source code of the application we need to place the source and testcode into separate project directories as above.
Why there are #2 directories under the sourcecode java and resources?
• The java source code should be compiled and shipped to the customer, whereas in a project apart from java source code we have non-java source code also like properties, configurations etc unless these files are available/shipped to the customer the application will not work (like database configuration files etc),
For these non-java sourcecode maven dont have to compile, but should ship or package into final distribution
so to let the maven differentiate which is the javasourcecode and non-javasource code, it has provided #2 directories
2. provide the information about the project to the maven
upon creating the project with standardized directory structure, all that we need to do to build the project by using maven is provide the information about the project to the maven build system by writing pom.xml under the root directory of the project (we dont need to write build script file like build.xml)
pom = stands for project object model. object model refers to structured data, since this file contains project structured data, it is called pom file. here we need describe the information about the project only, we don't need to write at least one build instruction also to build the project.
The pom.xml is an XML file in which we just only need to write 4 elements to build an project mandatorily
1. modelVersion
2. groupId
3. artifactId
4. version
modelVersion = refers to the version of the pom file we are using in writing the pom.xml
groupId = organization name, for whom we are developing the project. It usually will be written as domain name of the organization since domains are unique
artifactId= projectName
version = version of the project we are developing
Comments
Post a Comment