Maven is a popular open source build tool for enterprise Java projects, designed to take much of the hard

work out of the build process. Maven uses a declarative approach, where the project structure and contents are described, rather then the task-based approach used in Ant or in traditional make files, for example. This helps enforce company-wide development standards and reduces the time needed to write and maintain build scripts. Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information Maven's primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with: * Making the build process easy * Providing a uniform build system * Providing quality project information * Providing guidelines for best practices development * Allowing transparent migration to new features Installation Instructions Maven is a Java tool, so you must have Java installed in order to proceed. More precisely, you need a Java Development Kit (JDK), the Java Runtime Environment (JRE) is not sufficient. Additional optional installation steps are listed after the platform specific instructions. Windows 2000/XP 1. Unzip the distribution archive, i.e. to the directory you wish to install Maven 3.0. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apachemaven-3.0 will be created from the archive.

e. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause). Most developers are familiar with the notion of build phases such as compile. and deploy. you should find the build output and the final library or application that was being built.g.5. then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.9.0_02 and that %JAVA_HOME%\bin is in your Path environment variable. That's it! If you look in the target subdirectory. also be sure that the M2_HOME doesn't have a '\' as last character. Be sure to omit any quotation marks around the path even if it contains spaces. 3. In Maven 1. Open a new command prompt (Winkey + R then type cmd) and run mvn -version to verify that it is correctly installed. The local repository is created in your home directory (or alternative location that you created it). In the same dialog. make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK. 4. for instance. the java plug-in is used: $maven java:compile . Project lifecycles Project lifecycles are central to Maven 2. To compile Java source code. C:\Program Files\Java\jdk1. Ant has targets with names like those. Building a Project The vast majority of Maven-built projects can be built with the following command: mvn clean install This command tells Maven to build all the modules. and the "Environment Variables" button. test. selecting the "Advanced" tab. Note: For Maven < 2. and to install it in the local repository.2. and is the location that all downloaded binaries and the projects you built are stored.0.0. corresponding plug-ins are called directly.

which is generally accomplished using the appropriate plug-ins compile: Compiles the project source code test-compile: Compiles the project unit tests test: Runs the unit tests (typically using JUnit) in the src/test directory package: Packages the compiled code in its distributable format (JAR.In Maven 2. etc.) integration-test: Processes and deploys the package if necessary into an environment where integration tests can be run install: Installs the package into the local repository for use as a dependency in other projects on your local machine deploy: Done in an integration or release environment. Instead of invoking plug-ins. Figure 2. the Maven 2 developer invokes a lifecycle phase: $mvn compile. Maven 2 lifecycle phases Some of the more useful Maven 2 lifecycle phases are the following: y y y y y y y y generate-sources: Generates any extra source code needed for the application. WAR. this notion is standardized into a set of well-known and well-defined lifecycle phases (see Figure 2). copies the final package to the remote repository for sharing with other developers and projects .

with examples in several -DartifactId=my-app DarchetypeArtifactId= maven-archetype-quickstart -DinteractiveMode=false If you have just installed Maven. execute the following Maven goal: mvn archetype:generate -DgroupId=com. You may also need to execute the command a couple of times before it succeeds. You can run this archetype on top of an existing Maven 2 project to add integrated documentation. maven-archetype-mojo:Create your own Maven 2 plugins. Maven 2 default. there are ways to fix that.mycompany. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. Don't worry.Project Template (Archetype) Purpose maven-archetype-archetype:Create your own project template (archetype). suitable for JAR generation. Creating a Project On your command line. You will notice that the generate goal created a directory with the same name given as the artifactId. maven-archetype-webapp:Creates a web application project (WAR). etc. with a simple Hello World JSP. with directories and subprojects for the EJBs. servlets. Change into that directory. maven-archetype-j2ee-simple:Creates a J2EE project (EAR). cd my-app . maven-archetype-site:Documentation-only site. This is because the remote server may time out before your downloads are complete. maven-archetype-quickstart:Simple Java project. it may take a while on the first run.

apache. It is a single configuration file that contains the majority of information required to build a project in just the way you want.src |-.0. my-app | The src/main/java directory contains the project source code.xml `-.0. and the this directory you will notice the following standard project structure.xml is the project's Project Object Model.apache.test `-.0" xmlns:xsi="http://www. This project's POM is: <project xmlns=" `"> <modelVersion>4.0 http://maven.0</modelVersion> <groupId>com.mycompany ` | `-.main | `-.0. the src/test/java directory contains the test source.App.0.mycompany | ` `-. The POM The pom.xml file is the core of a project's configuration in Maven. or | ` | `</groupId> <artifactId>my-app</artifactId> <packaging>jar</packaging> <version>1. The POM is huge and can be daunting in its" xsi:schemaLocation="http://maven.0-SNAPSHOT</version> ` `-. but it is not necessary to understand all of the intricacies just yet to use it

The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.apache. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. the POM contains every important piece of information about your project and is essentially one-stop-shopping for finding anything related to your project. artifactId This element indicates the unique base name of the primary artifact being generated by this project. Understanding the POM is important and new users are encouraged to refer to the Introduction to the POM.<name>Maven Quick Start Archetype</name> <url>http://maven. modelVersion This element indicates what version of the object model this POM is using. The primary artifact for a project is typically a JAR file. The POM is the basic unit of work in Maven.plugins is the designated groupId for all Maven plug-ins.xml contains the Project Object Model (POM) for this project. A typical artifact produced by Maven .org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.xml files. so let's walk through each of them to familiarize you with the POM essentials: y y y y project This is the top-level element in all Maven pom. For example org.apache. This is a very simple POM but still displays the key elements every POM contains. Secondary artifacts like source bundles also use the artifactId as part of their final name. groupId This element indicates the unique identifier of the organization or group that created the project.1</version> <scope>test</scope> </dependency> </dependencies> </project> pom. In short. This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project.8.maven.

. JAR. packaging This element indicates the package type to be used by this artifact (e. version This element indicates the version of the artifact generated by the project.jar).. This not only means if the artifact produced is JAR. WAR. which indicates that a project is in a state of development. This is often used in Maven's generated documentation.). and end with the following: . reports The command line will print out various actions.y y y y y would have the form <artifactId>-<version>.0. This is often used in Maven's generated documentation. just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.It Also contains the important information like test case . (The lifecycle is a topic we will deal with further on in the guide. description This element provides a basic description of your project. name This element indicates the display name used for the project. For now. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version.g. etc. Build the Project mvn package This command will create a target directory inside the project (in this case myapp) This target folder contains the jar file for the project . myapp1. This is often used in Maven's generated documentation. or EAR but can also indicate a specific lifecycle to use as part of the build process.<extension> (for example. [INFO] [INFO] [INFO] [INFO] [INFO] [INFO] -----------------------------------------------------------------------BUILD SUCCESSFUL -----------------------------------------------------------------------Total time: 2 seconds Finished at: Thu Oct 05 21:16:04 CDT 2006 Final Memory: 3M/6M . url This element indicates where the project's site can be found.) The default value for the packaging element is JAR so you do not have to specify this for most projects. We will discuss the use of snapshots and how they work further on in this guide. WAR. EAR.

which is an ordered sequence of phases.3</version> <scope>compile</scope> </dependency> And that's it! You don't have to hunt around to know in which other JARs (and in which versions) you need to run Hibernate 3. 2.xml. When a phase is given. 3. Rather than a Maven will execute every phase in the sequence up to and including the one defined. You would simply add a new dependency to the dependencies section in pom.0-SNAPSHOT.0. this is a phase. 5.3.App Which will print the quintessential: Hello World! ==================================================================== Suppose you want to use Hibernate in your project.mycompany. the phases that actually get executed are: 1. A phase is a step in the build lifecycle. 6. if we execute the compile phase. Maven will do it for you! . as follows: <dependency> <groupId>hibernate</groupId> <artifactId>hibernate</artifactId> <version>3. 4.package.0. For example. validate generate-sources process-sources generate-resources process-resources compile You may test the newly compiled and packaged JAR with the following command: java -cp target/my-app-1.[INFO] -----------------------------------------------------------------------Unlike the first command executed (archetype:generate) you may notice the second is simply a single word .jar com.

Sign up to vote on this title
UsefulNot useful