Maven

MAVEN:

Maven is a Build Tool (Creates project structure and runs commands) and a Project Management Tool (It gives reports on existing code)

Maven repository contains projects and jars in a centralized location. When you create a project using maven and if it requires external jars,
maven would download it from external repository. Ofcourse it would search the local repository first. You define the jars required by your project
in the dependency tag of pom.xml. Each project has a pom.xml.

External repository: exists on the internet
Internal repository: exists on your system at ~/.m2/repository

Some jars may not be in the default maven repository. For such jars, the owner of those jars provide with a repository location that you need to specify
in the pom.xml of your project under the repository tag.

mvn archetype:generate — shows you a list of available archetypes with their groupid and artifactid
OR
if you know your desired group id and artifact id, then you can run the below command.
mvn archetype:create -DgroupId=<your group> -DartifactId=<your artifact> -DarchetypeArtifactId=<desired artifact> -DarchetypeGroupId=<desired artifact group>

Each project has a groupid, artifactid, version, packaging. Group id, artifact id and Version together are called maven coordinates.

 

To build/execute projects using maven, the command you must use is: mvn [phase name]

Some important Maven Phases:

1. Validate — validates the pom and directory structure
2. Compile — compiles to .class files
3. Test — Runs junit tests
4. Package — Packages the application to jar or war depending on what you specified in the pom.xml
5. Install — publishes the jar file into local maven repository
6. Deploy — does not deploy to your app server. It deploys to the remote repository so that other users can use.

Each phase will also run previous phases.

7. clean — Removes the target folder, classes and jars from your project structure

 
Maven first finds the dependencies in the local repository first and if not found goes to the remote repository.
Scopes:
test — runs during test phase
compile — runs during compile. This is the default scope and the compile scope is available in all classpaths and dependencies are packaged.
provided — only available during the compilation and test classpath and dependencies are not packaged.

Plugins: Modular architecture. All the core components act as plugins
Example: Compiler Plugin, Jetty Plugin, Eclipse Plugin
For example, while using jetty plugin, execute with:
mvn jetty:run
jetty is the plugin name.
run is the command.

<configuration inside plugin is used to configure the plugin.
Example check for updated class files using scaninterval

All plugins don’t need to be mentioned in the pom.xml, it will get it from the remote repository.

mvn eclipse:eclipse –> generates the eclipse project

Download the maven-eclipse plugin, m2e and run maven commands from eclipse.

Leave a comment