0% found this document useful (0 votes)
226 views

Maven Tutorial 01

Maven is a build tool and project management framework that defines standard conventions and practices for building, reporting and managing Java projects. It provides a project model called the Project Object Model (POM) that defines dependencies, plugins and other configuration. Maven aims to standardize build processes across projects through its conventions over configuration approach and reusable plugins. It handles dependency resolution by searching local and remote repositories for dependencies based on their coordinates.

Uploaded by

Hidayat Shaik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
226 views

Maven Tutorial 01

Maven is a build tool and project management framework that defines standard conventions and practices for building, reporting and managing Java projects. It provides a project model called the Project Object Model (POM) that defines dependencies, plugins and other configuration. Maven aims to standardize build processes across projects through its conventions over configuration approach and reusable plugins. It handles dependency resolution by searching local and remote repositories for dependencies based on their coordinates.

Uploaded by

Hidayat Shaik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Javaero.

org Maven

Intro to Maven
2.0
Javaero.org Maven

What is MAVEN?

-Is a project management framework.

-Is a build tool and scripting framework.

-It is a set of standards, a repository format, and a piece of software


used to manage and describe projects.

-It defines a standard life cycle for building, testing and deploying
project artifacts.
Javaero.org Maven

“Maven is a declarative project management tool that decreases your overall


time to market by effectively leveraging your synergies. It simultaneously
reduces your headcount and leads to remarkable operational efficiencies”
Javaero.org Maven

Maven’s Origins
- Maven was borne of the practical desire to make several projects at
the Apache Software Foundation(ASF) work in the same, predictable
way.
- Before, every project at the ASF had a different approach to
compilation, distribution, and web site generation. Ex. Build process
for Tomcat was different from build process used by Struts. Turbine
developers used a different approach to site generation than the
developers of Jakarta Commons. Etc.
- This lack of common approach to building software meant that every
new project tended to copy and paste another project’s build system.
- The barrier to entry for a project with a difficult build system was
extremely high. Difficult to attract developer because it could take an
hour to configure everything in just the right way.
Javaero.org Maven

What does Maven provide?


-Comprehensive Software project Model
- Tools that interact with this declarative model.
-Common project language
- Project Object Model (POM)
- Allows developers to declare goals and dependencies and rely
on default structure and plugin capabilities
Javaero.org Maven
Benefits:
1. Coherence- it allows organization to standardize on a set of
best practices.
2. Reusability- reusing the best practices.
3. Agility- lowers the barrier to reuse not only of build logic but
of components. It is easier to create a component and
integrate it to a multi-project build. Easier for developers to
jump between projects without the a steep learning curve.
4. Maintainability- Can now stop building the build. Focus on the
application.
Javaero.org Maven
Principles:
1.Convention over configuration
3 conventions:
• standard directory layout. Ex. Project sources, resources, config
files, generated output, documentation.
• single Maven project producing single output(SoC principle).
• standard Naming conventions.
2. Reuse of build logic- use of plugins (key building blocks)
Javaero.org Maven
Principles cont..
3. Declarative execution- POM model.
pom.xml
modelVersion-
project- <project> Version of the object
Top level element <modelVersion>4.0.0</modelVersion> Model this POM is using groupId-
Of pom.xml Unique identifier of the
<groupId>com.mycompany.app</groupId>
<artifactId>javaero</artifactId> Organization that created
The project.
version- <packaging>jar</packaging>
artifactId-
Version of the generated <version>1.0-SNAPSHOT</version> Unique base name of the
Artifact.
<name>Javaero</name> Primary artifact being
SNAPSHOT- project is
<url>http://maven.apache.org</url> Generated by the object. packaging-
In a state of development
<description></description> Ex. Javaero-1.0.jar Type of packaging to be
Used by this artifact.
<dependencies> Ex. JAR, WAR, or EAR
name-
<dependency>
Display name used for the url-
Project. Often used in generated <groupId>junit</groupId>
This element indicates
Documentation, during build process <artifactId>junit</artifactId> Where the project’s site
Of the project, or projects that use it <version>3.8.1</version> Can be found.
As a dependency <scope>test</scope> description-
</dependency> Basic description of the
</dependencies> Project.
</project>

*This pom will allow you to compile, test and generate basic documentation.
Javaero.org Maven

Maven’s build lifecycle:


-consists of a series of phases where each phase can perform one or
more actions, or goals, related to that phase.Ex. Compile phase
invokes a certain set of goals to compile set of classes.
- If you tell Maven to compile, the validate, initialize, generate-
sources, process-sources, generate-resources, and compile phases
will execute.
- Standard build life cycle consists of many phases and these can
be thought of as extension points. When you need to add some
functionality to the build lifecycle you do so with the plugin.
- Maven Plugins provide reusable build logic that can be slotted into
the standard build life cycle. Any time you need to to customize
the way your projects builds you either employ the use of an
existing plugin or create custom plugin for the task at hand.
Javaero.org Maven

Principles cont..
4. Coherent organization of dependencies.
<project>
:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
:
</project>

- Where does dependency came from?


- Where is the JAR?
Javaero.org Maven

Dependencies… artifacts…repositories..

Dependency- is a reference to a specific artifact that resides in a


repository. In order for Maven to attempt to satisfy a dependency,
Maven needs to know what the repository to look in as well as the
dependency’s coordinates. A dependency is uniquely identified by the
following identifiers: groupId, artifactId and version.
• Dependencies are declarative
• In the POM you are not telling Maven where the dependencies
physically, you are simply telling Maven what a specific project expects.
• When a dependency is declared, Maven tries to satisfy that
dependency by looking in all of the remote repositories that are
available, within the context of your project, for artifacts that match
the dependency request. If matching artifact is located, it transports it
from remote repository to your local for general use.
Javaero.org Maven
2 types of repositories
1. Local - ~/.m2/repository
- you must have a local
repository in order for
Maven to work.
2. Remote-
Javaero.org Maven

general pattern for the repository Layout

If the groupId is a fully qualified domain name such as z.y.x then you would end up with a
directory structure like the following.
Javaero.org Maven

Locating Dependency Artifacts


Steps:
1. Maven will attempt to find the artifact with groupId, artifactId
and version in local repository -> ~/.m2/repository/…
2. If this file is not present, it will be fetched from remote
repository.
3. By default, Maven will fetch an artifact from the central
Maven repository at http://www.ibiblio.org/maven2.
4. If your project’s POM contains more than one remote
repository, Maven will attempt to download an artifact from
each repository in the order defined in your POM.
5. Once dependency is satisfied, the artifact is downloaded and
installed in your local repository.
Javaero.org Maven

Getting Started with Maven


Javaero.org Maven
Preparing to Use Maven:
1. Download Maven from http://maven.apache.org and unzip it
to your desired installation directory. Ex. C:\maven in
Windows, or /usr/local/maven in Linux.
2. Add System variable M2_HOME as well as $M2_HOME/bin
directory to your system path.
3. Type the following in a terminal or command prompt:
mvn --version
4. If you are behind a firewall, create a <your-home-
directory>/.m2/setting.xml with the following content:
Javaero.org Maven

5. If there is an active Internal Maven proxy running. create a


<your-home-directory>/.m2/setting.xml with the
following content:
Javaero.org Maven

Creating your First Maven Project


1. To create the Quick Start Maven project, execute the following:
c:\> mvn archetype:create –DgroupId=com.mycompany.app –DartifactId=my-app

2. New directory javaero has been created for the new project, and this
directory contains your pom.xml which looks like the ff:
Javaero.org Maven

Directory structure of your project after archetype


generation
Javaero.org Maven
3. Compile your project. Type the ff command:
c:\my-app> mvn compile
*The output were placed in target/classes

• Compile Test Sources and Run unit Tests.


c:\my-app> mvn test
* if you want to compile your test sources(but not execute the tests),
execute the ff command:
c:\my-app> mvn test-compile

5. Make a JAR file or the package.


c:\my-app> mvn package
*take a look at the target directory and you will see the generated JAR file.

6. Install the artifact(JAR file) you’ve generated into your local repository so
that it can be used by other projects. <your-home-
directory>/.m2/repository is the default location. Execute the ff:
c:\my-app> mvn install
Javaero.org Maven

Surefire plugin (which executes the test) looks for tests contained in files
with a particular naming convention. By default the ff tests are
included:
• **/*Test.java
• **/Test*.java
• **/*TestCase.java

Conversely, the ff tests are excluded:


• **/Abstract*Test.java
• **/Abstract*TestCase.java

7. Create a basic website for your project. Execute the ff command:


c:\my-app> mvn site
8. To clean and remove target directory. Execute the ff command:
c:\my-app> mvn clean
Javaero.org Maven

9. To create descriptor for the project. Execute the ff command:


for IntelliJ IDEA:
c:\my-app> mvn idea:idea

for eclipse:
c:\my-app> mvn eclipse:eclipse

8. To clean and remove target directory. Execute the ff command:


c:\my-app> mvn clean
Javaero.org Maven

Handling Classpath Resources


- add the directory src/main/resources. This is where you place any resources
you wish to package in the JAR. The rule is that all directories or files
placed within this directory are packaged in your JAR with the same exact
structure, starting at the base of the JAR.
Javaero.org Maven

Handling Test Classpath Resources


- add the directory src/test/resources.
Javaero.org Maven
Filtering Classpath Resources:
Filter your resource files dynamically by putting reference to the
property that will contain the value into your resource file using the syntax $
{property name}.
Get property values from:
1.pom.xml
2. settings.xml
3. External Propety file
4. System property.

To have Maven filter resources from copying, simply set filtering to “true” for
the resource directory in your pom.xml:
Ex: <project>
:

</project>
Javaero.org Maven
Example:
1. Create src/main/resources/application.properties.
# application.properties
application.name= ${project.name}
application.version=${project.version}
2. Execute command: mvn process-resources
3. application.properties under target/classes directory, which
will go into a JAR looks like this:
# application.properties
application.name=Maven Quick Start Archetype
application.version= 1.0-SNAPSHOT
Javaero.org Maven
To reference a property file defined in external file:
Ex:
1. Create external property file src/main/filter/filter.properties
# filter.properties
my.filter.value=hello!
2. Add reference to this new file in pom.xml

3. Add reference to application.properties


# application.properties
message= ${my.filter.value}
4. Execute command : mvn process-resources
Javaero.org Maven
Another Approach instead of getting from external file:
<project>
:
<properties>
<my.filter.value>hello!</my.filter.value>
</properties>
:
</project>
Javaero.org Maven
Preventing filtering of Binary Resources
Javaero.org Maven
Using Maven Plugins
- To customize the build of a Maven project, include additional plugin
or configure parameters for the plugins included in the build.
Javaero.org Maven
Javaero.org Maven

Summary:
- Maven is a set of standards, a repository, a framework and is a
software.
- Maven is more than just downloading another JAR and a set of
scripts, it is the adoption of processes that allow you to take
your software to the next level.
Javaero.org Maven

References:

-http://maven.apache.org

You might also like