Maven Videos:
Sunil
IntelliqIT & Others
Maven Intro:
About Maven
- Maven is java based application build tool.
- Build tools package source code into Artifact.
- Artifact is the package/application used by enduser, it is in the form of (exe, msi (Microsoft Installer), war (Web Archive), jar (java archive), rar).
- Developers write source code then compile and then generate artifact. (Source code is like raw code which is compiled, compiling is converting into machine readable format) and converted into class file, these class files are used to generates artifact.
- Other Build tools are:
- Java based application: Maven (from Apache), Ant (from Apache), Gradle
- Microsoft .NET based application : MSBuild
- Python based application: PyBuilder
- POM.xml (Project Object Model), it is a configuration xml file.
- code
- code
Java Project Workflow
- Source Code
- Test Code
- Project Structure (directory strucutre)
- Dependencies/Library (Download from mvnrepository.com)
- Configuration
- Task: Build, Test, Run,
- Reporting
- code
- code
API (Application Programming Interface)
- It is a code provided by service providers to integrate your application to take their services.
- Example : Google Map, In contact us webpage a map is displays the address location, this map is taken from google map, google provde API of google map and it is used by developers and integrate in clients's application.
- Example: Paypal/other payment service, If you purchase an item on website, a pyament gateway is linked to process payment, ex. paypal, they provide API code and developers used the code and integrate in the appliation.
- API can be downloaded from Maven Global Server by visiting www.search.maven.org, search for required API version and copy the API tag which will be pasted in POM.xml file.
- Developers download the required APIs from Maven Global Server in their maven local repository.
- Run the git add. and git commit commands and commited in central repository. (two files will be committed, API downloaded and modified pom.xml)
- code
Sub Title
Vulnerability
- It is a thread, risk or harm to your application.
- API are third party code which developers used and integrate in the application, safety and security will be main concern as it may hamful to the customers or application. API is required in order to perform third party activities. Maven global server provides secure and safe API.
Maven Global Server
- It provides safe and secure API of known popular services, it is a search web page where you can search and download required API.
- www.search.maven.org
- Developers download required APIs in maven local repository, where a local repository is a folder in developers machine.
- Developers move source code along with APIs to central repository, for security they move read only APIs.
Sub Title
Maven Installation Linux:
Pre-requisite: JDK
- ubuntu 20.04:
- $sudo apt-get update
- $sudo apt-get install openjdk-17-jdk -y or #sudo apt install default-jdk (latest jdk version) or simply type java and system will inform you the commands to run to install.
- Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed in Java development
- #java --version
- Fedora, CentOS
- $sudo yum install update
- $sudo yum list | grep openjdk (list the jdk package)
- $sudo yum install java-1.8.0-openjdk or #sudo yum install java* -y
- $java -version
- code
Set Environment Variable
- Ubuntu 20.04:
- $ which java (it will display the path/location where java is installed, /usr/bin/java ).
- /usr/bin/java symbolic link to '/etc/alternatives/java'
- #file /etc/alternatives/java (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.amzn2.0.1.x86_64/jre/bin/java) copy until jre
- #cd ~ (home folder of user)
- #ls -a (.bash_profile or .bashrc)use either file.
- #sudo vi .bash_profile
- add line: JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.amzn2.0.1.x86_64/jre" and PATH=$JAVA_HOME/bin:$PATH (save & exit)
- #source .bsh_profile
- #echo $PATH
- #echo $JAVA_HOME
- code
- code
Install Ubuntu, Fedora, RHEL, Centos
Install Maven
- Maven Installation on ubuntu:
- Pre-requisite: JDK 17 (
- Install:#apt-get install maven -y
- Version: #mvn --version or #mvn -v
- Home directory: /usr/share/maven
- Install Git & Maven together: #sudo apt-get install -y git maven
- Maven Installation on Amazon Linux, RHEL, Fedora, CentOS
- Go to https://https://maven.apache.org/download.cgi -->Binary tar.gz archive and right click on link and copy address link.
- $sudo su
- #cd ~
- #cd /opt
- #wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
- #tar -xzvf apache-maven-3.9.6-bin.tar.gz (files will be extracted here, move to a maven folder for easy understanding)
- #mv apache-maven-3.9.6 maven
- #cd /maven/bin
- #./mvn -v (it will show maven version) to run mvn from anywhere, set the Environment Variable of maven.
- Set Environment Varibale Maven:
- Go to root home directory #cd ~
- # ll -a (list of files including hidden)
- #vi .bash_profile
- below fi enter the following:
- M2_HOME=/opt/maven
- M2=/opt/maven/bin
- JAVA_HOME=/usr/lib/jvm/java-17-amazon-corretto.x86_64
- PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2
- export PATH (save and exit)
- #echo $PATH (if it does not show path for maven and java)
- #source .bash_profile
- #echo $PATH (/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/lib/jvm/java-17-amazon-corretto.x86_64:/opt/maven:/opt/maven/bin)
- To get java path: 'find / -name java-17* (it will give the path of java installed: /usr/lib/jvm/java-17-amazon-corretto.x86_64), now mvn command can run from anywhere
- #sudo init 6 (restart the server so that path takes effect)
- Shutdown and Restart: After shutdown and restart the maven installed machine, go to folder where agent files are unzipped, #cd /myagent and run #./run.sh
- POM.xml : Project Object Model, it contains the following information
- project information:
- Dependencies:
- Plugins:
- Profiles: in which environment it uses, dev, test or debug etc..
- Goals:
- compile: it will compile the source code
- Test: compile the code and test the code. (if you define command mvn test then first it will compile and then run test).
- Package: test the code and create package.
- Install:
- Deploy:
- clean: the extra folder which was created (target) will be removed.
- Execution goals:
- single: single goal execution, mvn compile or mvn test etc..
- multi goal: dual execution, mvn clean package (it will remove target folder and create package)
- Maven Errors:
- 1) Error injecting: org.apache.maven.plugin.war.WarMojo
Set Environment Varibale of Maven
- $cd ~
- $vi .bash_profile or vi .bashrc
- code
- code
Maven Installation Windows:
Pre-requisite
- Install jdk 1.8 or higher, Download :
- Set Environment variable: Control Panel> system and security> system>advanced system settings> Environmental Variable. (user environment variables is set for each user and system environment variables set for everyone).
- Step 1) user variable select new, name = JAVA_HOME, variable value=C:\Program Files\Java\jdk-21
- Step 2) Click on path and new and paste C:\Program Files\Java\jdk-21\bin
- To confirm the path of JDK Installation: open cmd and run echo %JAVA_HOME%
- at cmd run where java it will show java.exe path
- at cmd run java -version
- code
Install Apache-Maven
- Download Binary zip ardhive file, create a folder (Maven) and extract it here. (C:\Maven\apache-maven-3.9.6)
- Set Environmental Variable for Maven in Windows: control panel> system and security> system>advanced system settings> Environmental Variable. (user environment variables is set for each user and system environment variables set for everyone).
- Step 1) user variable click new variable and enter variable name =MAVEN_HOME, variable value=C:\Maven\apache-maven-3.9.6 (for version 1 use name M2_HOME and Verson 2 use MAVEN_HOME)
- Step 2) path: edit path> new and paste C:\Maven\apache-maven-3.9.6\bin
- Check maven is installed: open cmd c:\maven -version
- code
code
code
code
Maven Architecuture:
POM.xml file
- POM.xml : Project Object Model, it contains the following information
- project information:
- Dependencies:
- Plugins:
- Profiles: in which environment it uses, dev, test or debug etc..
- Goals:
- compile: it will compile the source code
- Test: compile the code and test the code. (if you define command mvn test then first it will compile and then run test).
- Package: test the code and create package.
- Install:
- Deploy:
- clean: the extra folder which was created (target) will be removed.
- Execution goals:
- single: single goal execution, mvn compile or mvn test etc..
- multi goal: dual execution, mvn clean package (it will remove target folder and create package)
- Maven Errors:
- 1) Error injecting: org.apache.maven.plugin.war.WarMojo
Artifact storage:
code
code
code
Maven Repository:
Local
- When you intall maven on devlopers/clients machine, a local repository created .M2
Remote
- It referes to companies server which is used when maven needs to download dependency, it first downloaded to local repository then used.
Central
code
code
Create Artifact:
Create Artifact in Windows Machine: open cmd
- C:/user/username>mvn --version
- C:/user/username>mvn archetype:generate (it will create directory structure, Developers will create directory structure for java project or use existing template. Required internet as it download some plugins and is called maven arche type plugins. it will download for first time. it generates lot of maven numbers, developers will provide number related to their application).
- Choose a number or apply filter: 1797 for default and continue
- 1740: For Sprint Project
- 1765: For Sample Maven Project
- 1244: MVC 4 Web Application
- 2630: Project for multiple platforms like Web, Android, ios.
- 1797/2099: default number which it points by default: The number displays is default number.
- Choose a number for org.apache.maven.archetypes:maven-archetype-quickstart versoin: enter 8 and continue
- groupID: is used to follow project structure: enter com.stardistributors
- For commercial application -- com.<project_name> (Ex: com.stardistributors)
- For government application --Ex: gov.<project_name>
- For organisation application --Ex: org.<project_name>
- ArtifactID: Artifact file name: enter webappstar and continue
- Define value for property SNAPSHOT Version: enter to take default
- Define value for property Package: enter to take default
- enter Y to continue creating project.
- Project is created, check the path (c:\users\username\webappstar)
- It src folder and POM.xml file
- src folder: it has two folders
- main folder: code created by developers will be here
- test folder: code created by developers for unit testing will be here.
- POM.xml: Project Object Model: It store API information.
- convert the folder where project is created (webappstar) into working directory (.git) so that code can be pushed to remote respisitory to build artifact.
- git init
- git add .
- git commit -m "first commit" now code will be pushed to local repository.
- Devlopers search API in Maven Global Server using search.maven.org and to download they copy dependency tag and paste it in POM.xml file. POM.xml file contain the information of API which need to download.
- To download API, go to structure folder c:\users\user\webappstar and run c:\users\user\webappstar\mvn compile
- c:\mvn compile (API will be downloaded from maven global server to maven local repository). Target folder is created
- Maven local repository is created with the name .m2(hidden folder)
- git status (two untracked files, target folder is created and POM.xml file is modified(
- git add .
- git commit -m "second commit"
- If the developers found that they downloaded wrong API, they can roll back to previous or "first commit".
- To check the java code go to C:\Users\administrator\starwebapp\src\main\java\com\stardistributors\app.java (Developers will keep all java code here). As we are using default "hello world" code will be here. There will be a number of java files in real paroject, just copy app.java and make another file and make sure its class name should be similar to file name (star.java and inside
- Compile: Before generating artifact java code files need to compile, it will create java.class files.
- mvn compile (it will compile all java files and create java.class files)
- all .class files are stored in Target folder\.....
- Check test file and it has only one App.java file, earlier we have created one more java file Star.java, just create it here. SampleTest.java and change the class name inside as well. complile test file.
- mvn test (This will compile java file in test folder and create class file for test files)
- Generate Artifiact of all class files. run mvn package
- mvn package: it create jar file and will be available in the target folder.
- starwebapp-1.0-SNAPSHOT.jar generated.
code
code
code
code
Maven Project 1:
Maven Project 2:
Maven Dependency:
Transitive Dependency
- check transitive dependency in Eclipse dependency hierarchy.
- code
- code
Exclude Dependency
Scope Dependency
code
- scope
- runtime
- provided
- code
- code
code
Maven Eclipse:
Install Eclipe IDE on Windows:
- pre-requisite : JDK should be installed. jdk 10
- search in google "eclipse" and Download, run as administrator
- code
- code
code
code
code
code
Maven Build Lifecycle:
Lifecycle: 1 to 6 steps are default and in sequence
- Generate Resources (Dependecies)
- Code compilation: mvn compile, scan the whole source code and check any syntax based errors. (convert source code to machine readable 0,1)
- Unit Test: mvn test : all unit test cases will execute.
- Package Build(jar, war): mvn package: build the application and generate artifact. put it in the target folder.
- Install (local repo or artifact):mvn install: previous stages will also run compile + test + package (local maven repository will be created .M2)
- Deploy (clients servers deployment): mvn deploy : previous stages will run compile + test +package +install (push the artifact to third party software like nexus)
- clean (delete all run time files):
- mvn clean test:
- mvn clean package: it will remove target folder.
code
code
code
code
Maven Title:
Maven Title:
Maven Title: