100% found this document useful (1 vote)
89 views

Maven

Uploaded by

niraj chavhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
89 views

Maven

Uploaded by

niraj chavhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 28

Maven : Java

Module : Maven

Copyright © 2009 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture.
Module Objective
• At the end of this module, participants will be able to
understand:
– What Maven is
– The Physical Overview of Maven2
– The Maven repositories
– Maven 2 life cycle phases
– How to set the environment for Maven 2
– The pom.xml file
– How to customize the pom.xml file
– How to configure Maven with eclipse
– How to create Eclipse related files using Maven
– How to run Maven from Eclipse

Copyright © 2009 Accenture All Rights Reserved. 2


What is Maven?
• Maven is a software project management and comprehension
tool.
• Maven can manage the build of project, documentation and
reporting from a central piece of information.
• Maven is similar in functionality to the Apache Ant tool.
• Maven is hosted by Apache Software Foundation.
• Maven uses POM (Project Object Model) in order to describe
the software project which is being built, it’s dependencies on
other external modules and build order.
• Maven comes with predefined targets for performing certain
tasks like compilation of code and it’s packaging.
• Maven is network-ready. The core engine can dynamically
download plug-ins from a repository.

Copyright © 2009 Accenture All Rights Reserved.


Physical Overview of Maven 2
pom.xml Local & Remote
Repositories

Dependency
Project Object
Management
Model
Model

Project Life cycle and phases

Plug-in Plug-in Plug-in

Source files Generated Resources Binaries Packaged Binaries


Files
Copyright © 2009 Accenture All Rights Reserved.
Maven Repositories
• Maven repositories store a set of artifacts which are used by
Maven during dependency resolution for a project.
• Local repositories can be accessed on the local hard disk.
• Remote repositories can be accessed through the network.
• An artifact is bundled as a JAR file which contains the binary
library or executable.
• An artifact can also be a war or an ear.

Copyright © 2009 Accenture All Rights Reserved.


Maven 2 Life cycle phases
• Plug-ins are software modules which are written to fit into the
plug-in framework of Maven.
• Each task within a plugin is called a mojo.
• A mojo is executed when the Maven engine executes the
corresponding phase on the build life cycle.
• The association between the phase of a life cycle and a mojo is
called as a binding.
• Few phases of the build life cycle are described below:
a. validate = Ensures that the current configuration and the content of
POM is valid.
b. compile = Compiles the source code and the classes are placed in
the target directory tree.
c. test-compile = Compiles the source code of the unit tests.
d. test = Runs the compiled unit tests and verifies the results
Copyright © 2009 Accenture All Rights Reserved.
Maven 2 Life cycle phases (Continued…)
e. package = Bundles the executable binaries into a distribution
archive such as Jar or war
f. install = Adds the archive to the local Maven directory. Thus
making it available for any other module dependent on it.
g. deploy = Adds the archive to the remote Maven directory. Thus
making the artifact available to a larger audience.

Copyright © 2009 Accenture All Rights Reserved.


Setting the environment for Maven 2
• Download Maven2 from http://maven.apache.org/download.html
• Unzip the installation archive to c:/
• Set the M2_HOME and Path environment variables in the
following way:
– M2_HOME=C:\apache-maven-2.2.1
– Path=%M2_HOME%\bin
• If installed correctly, you should be able to test it by opening a
command prompt and typing :

• The output should be like this:

Copyright © 2009 Accenture All Rights Reserved.


First Maven 2 project
• In order to create everything which is needed for a simple Java
project that can be built using Maven, you can use Archetype
plug-in.
• Archetype is a standard plug-in which comes with Maven 2.
• The Archetype plug-in runs outside of a Maven project build life
cycle and is used for creating Maven projects.
• Issue the following command from the directory that you want to
contain SampleProject:

• The above command provides the archetype plug-in with the


coordinates of your module.
• The command also creates a starter pom.xml file for the project
along with the conventional Maven 2 directory structure.
Copyright © 2009 Accenture All Rights Reserved.
First Maven 2 project (Continued…)
• The output should be similar to the following:

Copyright © 2009 Accenture All Rights Reserved.


First Maven 2 project (Continued…)
• The Archetype plug-in creates a directory tree, a pom.xml file
and a placeholder App.java application.
• It also creates a directory tree for unit test source code and a
placeholder AppTest.java unit test.
• The following figures shows the directory structure created:

Copyright © 2009 Accenture All Rights Reserved.


First Maven 2 project (Continued…)
• Remove App.java and place the following SimpleExample.java :

Copyright © 2009 Accenture All Rights Reserved.


Sample pom.xml file
• The file generated by the Archetype plug-in for SampleProject is shown below:

Copyright © 2009 Accenture All Rights Reserved.


Customizing the generated pom.xml file

Copyright © 2009 Accenture All Rights Reserved.


Compiling the customized Project
• You can now compile the SampleProject using mvn compile
command.

• You should see the report of a successful build, creating one


class file in the target tree.
• This can take a little while if it is the first time you run it because
some dependencies might need to be downloaded from the
central repository over the Internet.

Copyright © 2009 Accenture All Rights Reserved.


Directory structure after compilation
• After compilation, the .class files (SimpleExample.class) file is
created in the target folder shown below:

Copyright © 2009 Accenture All Rights Reserved.


Adding a Unit test
• It is a development best practice to provide unit tests for all code
modules.
• Maven 2 created a placeholder AppTest.java unit test for you.
Remove the file and place the following SimpleExampleTest.java :

Copyright © 2009 Accenture All Rights Reserved.


Running a unit test
• You can use mvn test command.

• Maven 2 compiles the source and the unit test. It then runs the
tests, reporting on the number of successes, failures, and errors,
as shown in the following figure:

Copyright © 2009 Accenture All Rights Reserved.


Configuring Maven with eclipse
• Eclipse needs to know the path to the local maven repository.
Therefore the classpath variable M2_REPO has to be set. Exit
out of eclipse and execute the following command in a
command prompt. The following command will be making use of
the maven-eclipse-plugin.

• The execution of the above command should result in the


following:

Copyright © 2009 Accenture All Rights Reserved.


Configuring Maven with eclipse (Continued..)
• Now open up your eclipse and check if this variable M2_REPO
has been added.

Copyright © 2009 Accenture All Rights Reserved.


Creating eclipse specific files
• To generate the eclipse metadata files from your pom.xml you
execute the following command:

• The above command generates eclipse related files like .project


and .classpath .

Copyright © 2009 Accenture All Rights Reserved.


Importing the project in Eclipse
• After generating the Eclipse Classpath and Project files for your
project, import the project into Eclipse with the following steps:

1. From the Eclipse workspace menu select File > Import

2. Expand General and select Existing Projects into Workspace

3. Mark Select root directory and hit Browse. Browse for the directory
in which your POM file is in. Press OK.

4. You should now see your project checked under the Projects: box.
Press Finish.

5. You should now see your project in Eclipse.


Copyright © 2009 Accenture All Rights Reserved.
Running Maven outside eclipse
• Delete all the files under C:\MavenExample-
1\SampleProject\target
• Let’s now use Maven to build it as a jar and try to run it outside
eclipse. Execute the following command from the command line:

• You can see SampleProject-1.0-SNAPSHOT.jar under


C:\MavenExample-1\SampleProject\target. Based on the
pom.xml file, the jar was built.
• You can run the Java application in the command line as
follows:

Copyright © 2009 Accenture All Rights Reserved.


Running Maven from Eclipse
• Eclipse provides a feature which allows you to run external
programs.
• This feature can be utilized to insert Maven commands to a list
of external tools which are available to your workspace.
• Go through the following steps for the setup:
1. From the Eclipse workspace menu select Run > External Tools >
External Tools Configurations...
2. Select Program from the left.
3. Right click on Program and select New.
4. Provide a name for this configuration. This name is used for
identifying your maven command in the list of external tools. E.g.
mvn install.
5. Press Variables… (on the right hand side).
6. Press Edit Variables…
Copyright © 2009 Accenture All Rights Reserved.
Running Maven from Eclipse (Continued)
7. Press New…

8. Enter the following:

• Variable = maven_exec
• Value = C:\apache-maven-2.2.1\bin\mvn.bat
• Description = Maven Executable

9. Press Ok

10. Select maven_exec from the Select Variable and press ok.

11. Press Browse Workspace... under Working Directory and


select the folder in which your project's POM file is located if you
want to launch a Maven goal within a single project
Copyright © 2009 Accenture All Rights Reserved.
Running Maven from Eclipse (Continued)

Copyright © 2009 Accenture All Rights Reserved.


Running Maven from Eclipse (Continued)
12. Press Apply.
13. Press the Common tab on the right and select the External
Tools check box in the Display in favorites menu.
14. Press Apply and Close
15. You can now run Maven goals from the External Tools Shortcut
(see below).

Copyright © 2009 Accenture All Rights Reserved.


Questions and Comments

Copyright © 2009 Accenture All Rights Reserved. 28

You might also like