What is maven?
Maven is a powerful build automation tool used to work with java projects. It provides comprehensive set of feature which one can use to managing project dependencies, building, testing, and packaging java application
How to build an application
Compiling the code i.e converting lets say file.java into file.class
Running tests
Packaging jar/war/ear files
deploying into any server we will be using tomcat server for this
Type of repositories
Local repository:- This repositories present in our local system in
~/.m2/repository
it contains all kind of plugins which has been pulled till now and more its just like your database where all the thing you need is presentRemote repository:- it is the main repository of the association which is hosted on the remote servers from where it pulls the plugins and libraries it needs for the building process . Maven central repository
Enterprise repositories:- An enterprise repository, also known as an internal repository or private repository, is a central storage system used within an organization to manage and distribute software artifacts. It is a dedicated repository that holds all the dependencies, libraries, frameworks, and other artifacts required by the organization's projects.
Creating maven file with eclippse
After you have installed eclippse follow these steps
new->maven project
create mvn directory first and then use this woking director you can use any directory according to your needs although i am using mvn
Click next. After that choose the last archetype which has group.id org.apache.maven.archetypes
then click next
choose the group-id and artcifact-Id and press finish
After the installation you can see
This kind of outlay to the left of the screen under
package explorer
. Heresrc/main/java
contains the source code andsrc/test/java
contains the test cases for that source code
pom.xml
It is a xml files which contains all the dependencies, configurations and plugins details. Maven uses this file to automate the task and manage the project lifecycle
<groupId>:- It defines a identifier which is unique to the group and organization
who is responsible for the application<artifactid>:- It defines a unique identifier for the specific module or artifact which is bieng developed within the project, it usually contains name of the project
<groupId>com.firstProgram</groupId> <artifactId>firstProgram</artifactId> <version>0.0.1-SNAPSHOT</version>
version:- It is the version of the application we are releasing, Here 0.0.1 shows the version and snapshots shows that the it's a unstable or devlopment version
<url>:- it contains the url of the website, organization is developing, by using this link user can get more information about the project, in our case its empty
<url>http://www.example.com</url>
<dependencies>:- in this tags our dependencies comes which we need to run or compile, test,run the code sucessfully, here we are using junit dependency for testing.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
- We have taken here one segment of plugin, We are using maven-clean-plugin which we will be using for the
mvn clean
command,Which is often used to remove the artifacts or the files from the previous build, Before creating new build
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
- Likewise there will be lot of plugins which we will be using for for testing and compiling purposes
Build lifecycle
- Maven is based around the central concept of life cycle, It means that the process of building and distributing a particular artifact(object) is defined
Lifecycle is consist of different list of build phases, it represents the stages/goals in the lifecycle which gets executed in a sequential manner
These stages are
validate
- validate the project is correct and all necessary information is availablecompile
- compile the source code of the projecttest
- test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployedpackage
- take the compiled code and package it in its distributable format, such as a JAR.verify
- run any checks on results of integration tests to ensure quality criteria are metinstall
- install the package into the local repository, for use as a dependency in other projects locallydeploy
- done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
Building code on ecclipse
Go to run and choose run as
After you click maven build a new screen will appear
Goal
:- These are the specific action or task which can be executed during build phase. All the stages which i have defined beforevalidate
,clean
,install
are termed as goal which comes under build phaseAs i had said before all the goals gets executed in sequential manner, It mean i want to, lets say validate and install the code, In that case i dont have to write validate and install in goals instead i just have to write install in the goals it will complete all the goals which is coming before install including validate
So Lets say you wrote the code and you want to deploy it before deploying you first need a jar file which will be obtained if we use install as our goal
after writing install, click on apply and run the code
In maven the completion of goals in build phase is determined by maven core engine itself. The core engine coordinates the execution of plugins and their associated goals according to the defined lifecycle and the configuration present in the pom.xml
When you execute a Maven command, such as
mvn clean install
, Maven parses the pom.xml file and determines the build lifecycle phases and goals to execute. It then traverses through the defined phases and invokes the corresponding plugins and their goals.If those plugins are not in the local repositories maven engine will pull it from central/remote repositories as you can see in the above image
https://repo.maven.....
is the remote repoTry to run the code with same goals this time it will not pull the plugins where these are already present into the local repo
Maven on linux
Creating Maven webapp
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4
Maven will pull a web-app from a repository having Group-id
of org.apache.maven.archetypes
and Artifact-id
of maven-archetype-webapp
and version 1.4
go to the directory where web app is installed and run mvn clean install
After executing the command maven will create a war file and store it in you local repository `~/.m2/repository/webapp
You can use the war file and deploy it in tomcat server
Tomcat server
Setting up configuration files for tomcat
Install tomcat7 or 8 i have installed tomcat7 after installing tomcat7 place it under /opt/
directory and go to tomcat directory and then go to its binary startup.sh
file should be there, run that file using ./startup.sh
and goto http://localhost:8080
The tomcat server shud be up and running there
Now open tomcat-user.xml and add the code snippet given below and save the file and try to open the site http://localhost:8080/manager
it will tell you to login, enter user and password as admin. We added the credentials of the user in setting.xml and using it here
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
<user username="deployer" password="deployer" roles="manager-script"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Now we have to update our pom.xml
so that maven runtime automatically upload the artifacts which be will obtain from the code to our given url
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/helloworld-webapp</path>
</configuration>
</plugin>
</plugins>
In the given configuration, <artifactId>tomcat7-maven-plugin</artifactId>
refers to the Maven plugin specifically designed for Apache Tomcat 7. The <version>2.2</version>
specifies the version of the plugin that will be used.
In url you have to add the link where your tomcat is being hosted, If you are hosting at port 8080 then use this link if 8081 change the port and write rest of the url as it is
Path will indicate at what path the application will be deployed if you choose webapp
it will deploy at http://localhost:8080/webapp
in my case its http://localhost:8080/helloworld-webapp/
Now under /.m2 /directory
create settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<servers>
<server>
<id>TomcatServer</id>
<username>admin</username>
<password>password</password>
</server>
</servers>
</settings>
You are good to go now try to deploy the code using
maven tomcat7:deploy
Integerating jenkins with maven
Jenkins is a Continuous integeration tool which we will be using for the automating our task of compiling testing and building
Start jenkins using service start jenkins and setup the jenkins by adding admin password and install intial suggested plugins after that log into the system
go to Dashboard -> Manage jenkins ->Plugins and then click on available pluginsand install github and maven plugin which we will be needed in future to pull repo from the github
After installing go to Dashboard -> manage jenkins -> Nodes -> new Node and configure the server
number of executors
:- It indicated number of job which it can run simulatneouslyremote root directory
:- It is the remote directory where the user jenkins is present
We can create that using linux command
useradd jenkins
passwd jenkins
Add jenkins user into sudo files
visudo
And add jenkins user with root priviliges
jenkins
: The user account to which the rule applies.ALL
: The hosts or machines on which the rule is valid. In this case, it specifies that the rule is applicable to all hosts.(ALL:ALL)
: The user and group specification, allowing the user to run commands as any user and any group.ALL
: The commands or programs that the user is allowed to execute. Again,ALL
specifies that the user can run any command.
Creating jenkins server
Launch method in jenkins
: it defines how slaves will communicate with the jenkins server. By choosing launch agent via ssh i am allwing slaves to communicate using ssh protocol
We have to configure our ssh file so slaves can configure with ssh server using password which will be present here vi /etc/ssh/sshd_config
go to this directory and search for PasswordAuthentication and enable it, if it is commented out remove the comment or if its written no there write yes in front of it
Host
:- It refers to the hostname or ip addresses where maven artifacts or repository is hosted in my case it is localhost
credentials
: it is the login details using which you have logged into the system, which will used by the system to communicate with the maven server
Save and apply the configuration setting of the node
Set Global configuration
it basically contains path and version of the tools you system is using which is used by the slaves which will run on the server
We have to add the path where Git,Maven and java is installed beacuse we are using maven to automate java project's lifecycle
Creating jobs
Under dashboard go to new item and create a new maven project
we have to configure
Restrict where this project can be run
,source code management
andgoals
Restrict where this project can be run
:- It controls on which server the this job can be runSource code management
:- It refers to the managing of the source code,Jenkins is repsonsible for pulling the software from there repositories and build those source code into final product could be jar file could be war depends on the configurationcredentials
:- asking for credentials again so it can communicate with external system and repositories in our case its github
Branch on which the source code is i.e master
Root POM
:- It is main POM file which will be responsible for holding plugins and configuration of the plugins the system need in order to build a project in our case its POM.xml which will hold all the details
Goal
:- These are the specific action or task which can be executed during build phase.
Save and Apply the changes and then click on build now
After finishing the build the output should be like this if it is finished successfully, if you encounters any error you can troubleshoot it using chatgpt :)