What is Maven.
Maven is a popular open-source build tool developed by the Apache Group to build, publish, and deploy several projects at once for better project management.Maven comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging. Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache. With Maven, all dependencies of your project are maintained in a single pom.xml file. Maven takes care of downloading these dependencies into the local repositories and makes them available for the project.
Need for Maven
It is a build tool.A tool to setup a java application.When we develop any large application or API than we have to make sure that my made app is compatible in every other machine.Hence,Maven helps us in provisioning all sets of libraries and dependencies that may be require for our API or application to run in every development machine.Maven or gradle build tool have a specialised place where all libraries and dependencies can be specified which is pom.xml (incase of Maven).If we delete this pom.xml file from our project than our project will not compile.Maven Vs Gradle
Gradle - It's a build automation tool that is open-source and builds on concepts of Apache Maven and Apache Ant.It is capable of building any type of software.It provides support for building, testing and deploying softwares on different platform.It allows to write buildscripts with java.It is easy to migrate to gradle from maven or other build tools.Gradle provides integration with several development tools and servers including - eclipse,Intellij,jenkins and android studio.
Maven -Maven is older open-source project management tool primarily used for java projects.
Working difference -
- Gradle is used for large projects while maven is specifically designed for smaller projects.
- Gradle avoids the work by tracking input and output tasks ans only runs the task that have been changed.Hence gives a faster performance.Maven doesn't use build cache. thus, its' build time is slower than gradle.
Maven Lifecycle Methods
A Build Lifecycle is a well-defined sequence of phases, which define the order in which the goals are to be executed.When Maven starts building a project, it steps through a defined sequence of phases and executes goals.A goal represents a specific task which contributes to the building and managing of a project.
The Maven build lifecycle has 8-stages by default. These are in a particular Maven lifecycle order, as mentioned below.
- Validation
- Compilation
- Testing
- Packaging
- Integration testing
- Verification
- Installation
- Deployment.
mvn build
Maven Build -> It does the dependency management for Eclipse build path based on Maven's pom.xml. The result will probably be a .war file in the target subfolder of your project.( A goal in maven build is necessary to specify otherwise errors will come.This goal is 1 of the lifecycle phase.This goal launches the maven web application on tomcat on port 8080 ). Whenever a goal (a specific build task) is to be executed, Maven looks for the configuration details in POM.It resolves Maven dependencies from the Eclipse workspace without installing to local Maven repository (requires dependency project be in same workspace).mvn clean
Maven clean plugins -> The Maven Clean Plugin, as the name implies, attempts to clean the files and directories generated by Maven during its build. While there are plugins that generate additional files, the Clean Plugin assumes that these files are generated inside the target directory.Maven clean - The first command deletes target directory and then builds all you code and installs artifacts into local repository. It cleans the target folder,clean every file and previous results of the previous builds.
mvn install
Maven install - Maven install is Maven build with added additional step. That additional step is uploading the result from the Maven build script to your local Maven repository.Maven some build lifecycle phases - compile,test,package,install.
Maven install is one step among the maven lifecycle which copies the packaged binary in your local maven repository
When you do a Mvn install, it will roughly:
Generate whatever it needs,
Compile the sources,
Copy other resources,
Create the artifact for your project,
Run unit tests,
Copy the artifact to the local Mvn repository (this is usually $HOME/.m2/repository).
So a Mvn clean install will first clean the target and then run the steps above. mvn install is to do the whole build cycle again with validate, compile, test, package, verify and install the created artifacts (e.g. a JAR or WAR file … ) into the local repository (defaults to ${user.home}/.m2/repository)
mvn deploy
install phase installs the package in local/remote maven repository.Deploy phase Copies the final package to the remote repository.It will deploy our build , copy the final package to the remote repository or sharing the build package to other developers.mvn test
To run the unit test.This phase tests the compiled source code suitable for testing framework.mvn package
It will compile the code & packaged it in jar format.This phase creates the JAR/WAR package as mentioned in the packaging in POM.xml.mvn verify
It is for integration test. The main difference between mvn verify and mvn test is -when you run a Maven goal, it will run any previous goal. The order of basic phases is:
- Validate
- Compile
- Test
- Package
- Verify
- Install
- Deploy
-
If you run Test, Maven will execute validate, compile and test.
Based on this, the first point is that verify includes test.
TEST - test the compiled source code using a suitable unit testing
framework. These tests should not require the code be packaged or deployed
VERIFY - run any checks on results of integration tests to ensure quality
criteria are met.
Maven Commands
clean install -x -> check if there is any error in debug mode for successfully building the maven.
mvn clean -x -> get more information about why our build is failing on using mvn clean.
mvn compile:
This command is used to compile the project’s source code.
mvn clean:
Here, the project is cleaned to remove all previous-build files generated.
mvn test:
With this command, one can run project testing steps.
mvn test-compile:
This command is used to compile the code from the test source.
mvn install:
This command helps deploys the packaged WAR/JAR files storing them as classes in the local repository.
mvn package:
With this Maven lifecycle command, one packages or creates a project WAR or JAR file to be able to use a distributable format.
mvn deploy:
The deploy command occurs after compilation, running project tests, and project building. Here the packaged WAR/JAR files are copied to the remote repository for use by other developers.
Maven Commands
clean install -x -> check if there is any error in debug mode for successfully building the maven.
mvn clean -x -> get more information about why our build is failing on using mvn clean.
mvn compile:
This command is used to compile the project’s source code.
mvn clean:
Here, the project is cleaned to remove all previous-build files generated.
mvn test:
With this command, one can run project testing steps.
mvn test-compile:
This command is used to compile the code from the test source.
mvn install:
This command helps deploys the packaged WAR/JAR files storing them as classes in the local repository.
mvn package:
With this Maven lifecycle command, one packages or creates a project WAR or JAR file to be able to use a distributable format.
mvn deploy:
The deploy command occurs after compilation, running project tests, and project building. Here the packaged WAR/JAR files are copied to the remote repository for use by other developers.
Maven Commands
clean install -x -> check if there is any error in debug mode for successfully building the maven.
mvn clean -x -> get more information about why our build is failing on using mvn clean.
mvn compile:
This command is used to compile the project’s source code.
mvn clean:
Here, the project is cleaned to remove all previous-build files generated.
mvn test:
With this command, one can run project testing steps.
mvn test-compile:
This command is used to compile the code from the test source.
mvn install:
This command helps deploys the packaged WAR/JAR files storing them as classes in the local repository.
mvn package:
With this Maven lifecycle command, one packages or creates a project WAR or JAR file to be able to use a distributable format.
mvn deploy:
The deploy command occurs after compilation, running project tests, and project building. Here the packaged WAR/JAR files are copied to the remote repository for use by other developers.
Maven Build Profiles
Profile - A profile in Maven is an alternative set of configuration values which set or override default values. Using a profile, you can customize a build for different environments. Profiles are configured in the pom. xml and are given an identifier.
- Global Profile - Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml) in Modified settings.xml
- Local profile(per project) - Defined in the project POM file, pom.xml
- User defined profile(per user) - Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml)
Profile Activation
A Maven Build Profile can be activated in various ways.
Explicitly using command console input.
Through maven settings.
Based on environment variables (User/System variables).
OS Settings (for example, Windows family).
Present/missing files.
Profile Activation via Maven Settings
Open Maven settings.xml file available in %USER_HOME%/.m2 directory where %USER_HOME% represents the user home directory. If settings.xml file is not there, then create a new one.
Maven profile
Profiles are a core features of the framework that allows us to map our beans to different profiles.eg...Dev,Test,Prod,QA. A profile represents a unique cofiguration for the environment.
Suppose we have made a restful web app .So,this application will be accessed using a url. This url will vary when tested on different -2 environment.So,we require different configuration for this url to have compatibility with every other environment.Profile is the identity that uniquely identify different working environment.Profile is the bunch of configuration.We can have multiple profiles for different environment.
Activate profile through VM Arguments :
Instead of manually writting spring.profile.active=dev or test in application.properties to switch to different -2 environment .We can update the environment status under VM arguments in run.configuration as
-Dspring.profile.active=dev
or
-Dspring.profile.active=testing
This is a dynamic way to switch across environment.
Settings.xml
The Settings. xml file contains adjustable parameters about server administration within an Installation. Such parameters include the ability to adjust the size of the System Logs, Email Server settings, and settings specific to the database the platform is hosted on.
It allows us to specify which local and remote repository location it will use. It can also be used to store data which we don't want to show in our source code such as credentials.It allows values Such as local repository location or path, alternate remote repository server authenication information and other information.
Whaen we do maven configuration for the first time and run some maven goals than in the user home directory a .m2 folder gets created.Inside this .m2 folder, we can copy settings.xml file from the maven installtion directory.Copying the settings.xml file inside .m2 folder gives user specific configuaration and incase of eclipse IDE, we have to specify the location of this file for user specific configuration
By-default , the path for local repository is given inside .m2 folder but if we want to give any custom path for the local repository than we can give it under settings.xml in
Archetype
Archetype tells maven the type of project we r building.
That can be a java project archetype,spring applications archetype.,hibernate application archetype.
Different types of archetype r available inside maven repository.
If we want to build a java application we will include web archetype.
The combination of group Id ,artifactId ,version decides the type of artifact we r using.scope tag defines the scope for our artifact.
mvn archetype : generate -> it'll fetch all the archetype from central repository and will ask for the user input for the artifact Id,groupId version.
Ways to run a spring boot application
Ways to run a spring boot app
1.using maven artifact or maven descriptor which lets to execute maven command inorder to built a jar file.
Eg.. Running a jar file -> java -jar (jar file)
2.Plugins in pom.xml (running directly without creating any jar file) Eg.. ./mvnw spring-boot:run
(Maven install takes our spring project & generate the maven artifact . The generated maven artifact is gonna be a jar file.On running mvn install , the target folder is generated. )
(The jar file generated not only contains the compiled sources from our spring project it also contain things needed to run the application )