Translate

Wednesday, July 30, 2014

MAVEN..

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies. Contrary to preceding tools like Apache Ant it uses conventions for the build procedure, and only exceptions need to be written down. An XML file describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. It 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.This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated.
Maven can be used to build and manage projects written in C#RubyScala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.
Maven is built using a plugin-based architecture that allows it to make use of any application controllable through standard input. Theoretically, this would allow anyone to write plugins to interface with build tools (compilers, unit test tools, etc.) for any other language. In reality, support and use for languages other than Java has been minimal. Currently a plugin for the .NET framework exists and is maintained, and a C/C++ native plugin is maintained for Maven 2.

Maven History

Maven, created by Sonatype's Jason van Zyl, began as a subproject of Apache Turbine in 2002. In 2003, it was voted on and accepted as a top level Apache Software Foundation project. In July 2004, Maven's release was the critical first milestone, v1.0. Maven 2 was declared v2.0 in October 2005 after about six months in beta cycles. Maven 3.0 was released in October 2010 being mostly backwards compatible with Maven 2.



Project Object Model

A Project Object Model (POM) provides all the configuration for a single project. General configuration covers the project's name, its owner and its dependencies on other projects. One can also configure individual phases of the build process, which are implemented as plugins. For example, one can configure the compiler-plugin to use Java version 1.5 for compilation, or specify packaging the project even if some unit tests fail.
Larger projects should be divided into several modules, or sub-projects, each with its own POM. One can then write a root POM through which one can compile all the modules with a single command. POMs can also inherit configuration from other POMs. All POMs inherit from the Super POM[6] by default. The Super POM provides default configuration, such as default source directories, default plugins, and so on.

Plugins

Most of Maven's functionality is in plugins. A plugin provides a set of goals that can be executed using the following syntax:
 mvn [plugin-name]:[goal-name]
For example, a Java project can be compiled with the compiler-plugin's compile-goal[7] by running mvn compiler:compile.
There are Maven plugins for building, testing, source control management, running a web server, generating Eclipse project files, and much more.[8] Plugins are introduced and configured in a <plugins>-section of a pom.xml file. Some basic plugins are included in every project by default, and they have sensible default settings.
However, it would be cumbersome if the archetypical build sequence of building, testing and packaging a software project required running each respective goal manually:
 mvn compiler:compile
 mvn surefire:test
 mvn jar:jar
Maven's lifecycle-concept handles this issue.
Plug-ins are the primary way to extend Maven. Developing a Maven plug-in can be done by extending the org.apache.maven.plugin.AbstractMojo class. Example code and explanation for a Maven plug-in to create a cloud-based virtual machine running an application server is given in the article Automate development and management of cloud virtual machines.


Dependencies

A central feature in Maven is dependency management. Maven's dependency-handling mechanism is organized around a coordinate system identifying individual artifacts such as software libraries or modules. The POM example above references the JUnit coordinates as a direct dependency of the project. A project that needs, say, the Hibernate-library simply has to declare Hibernate's project coordinates in its POM. Maven will automatically download the dependency and the dependencies that Hibernate itself needs (called transitive dependencies) and store them in the user's local repository. Maven 2 Central Repository is used by default to search for libraries, but one can configure the repositories to be used (e.g., company-private repositories) within the POM.
There are search engines such as The Central Repository Search Engine[11] which can be used to find out coordinates for different open-source libraries and frameworks.
Projects developed on a single machine can depend on each other through the local repository. The local repository is a simple folder structure which acts both as a cache for downloaded dependencies and as a centralized storage place for locally built artifacts. The Maven command mvn install builds a project and places its binaries in the local repository. Then other projects can utilize this project by specifying its coordinates in their POMs.

  

EXAMPLE OF pom.xml file:

Maven projects are configured using a Project Object Model, which is stored in a pom.xml-file. Here's an example:
<project>
  <!-- model version is always 4.0.0 for Maven 2.x POMs -->
  <modelVersion>4.0.0</modelVersion>

  <!-- project coordinates, i.e. a group of values which
       uniquely identify this project -->

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0</version>

  <!-- library dependencies -->

  <dependencies>
    <dependency>

      <!-- coordinates of the required library -->

      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>

      <!-- this dependency is only used for running and compiling tests -->

      <scope>test</scope>

    </dependency>
  </dependencies>
</project>


This POM only defines a unique identifier for the project (coordinates) and its dependency on the JUnit framework. However, that is already enough for building the project and running theunit tests associated with the project. Maven accomplishes this by embracing the idea of Convention over Configuration, that is, Maven provides default values for the project's configuration. The directory structure of a normal idiomatic Maven project has the following directory entries:



The Maven software tool auto-generated this directory structure for a Java project.




Directory name
Purpose
project home
Contains the pom.xml and all subdirectories.
src/main/java
Contains the deliverable Java sourcecode for the project.
src/main/resources
Contains the deliverable resources for the project, such as property files.
src/test/java
Contains the testing Java sourcecode (JUnit or TestNG test cases, for example) for the project.
src/test/resources
Contains resources necessary for testing.



Installing & Setting class path in environment variables


1.    Download maven from http://maven.apache.org/download.cgi
2.    Unzip the distribution archive, i.e. apache-maven-3.2.2-bin.zip to the directory you wish to install Maven 3.2.2. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.2.2 will be created from the archive.
3.    Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.2.2. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven 2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
4.    In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
5.    Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
6.    In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
7.    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, e.g. C:\Program Files\Java\jdk1.7.0_51 and that %JAVA_HOME%\bin is in your Path environment variable.
8.    Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
       
 mvn --version
It should print out your installed version of Maven, for example:
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: D:\apache-maven-3.0.5\bin\..
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_25\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"






Create maven project using command prompt

Ø  Once maven is installed and set up, open a command prompt and create a directory or folder using the following syntax:
mkdir [project name ]
e.g. mkdir mavenProject




Ø  Now go to the existing project i.e. cd mavenProject
Ø  Create a maven project using the following command in case you don’t know the exact command:
mvn archetype:generate





And then select the number for the one you one to create the project as:




In my case I have to create a webapp project so I selected number as 19

19: internal -> org.apache.maven.archetypes:maven-archetype-webapp (A simple Java web application)

Ø  Now it will prompt for the group id, Please mention the group id
Group Id : is the unique name of the project, here I have provided com.test.project
  

                 
Ø  After entering group id it will ask for artifact id
artifactid: unique name of your project, here I have provided the name as appProject




Ø  It will ask the version, leave it default and press enter
version  1.0-snapshot






Ø  And lastly it will ask for the package, leave it default and press enter





Ø  Press Y and the project gets created.

  


   

Ø  Now go to the project i.e. cd appProject (appProject is defined using groupId) and check the directory or folder.





Ø  check the project, src folder and pom.xml is created

OR



You can also create the project using a single line command if you are aware which type of the project you want to create.

Let’s say you want to create a simple project then you can create using the following command:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

In case you want to create a web application then please mention the below command:
mvn archetype:create 
 -DarchetypeGroupId=org.apache.maven.archetypes 
 -DarchetypeArtifactId=maven-archetype-webapp 
 -DarchetypeVersion=1.0 
 -DgroupId=com.maventest 
 -DartifactId=mywebtest 
 -Dversion=1.0-SNAPSHOT

Executing Commands


mvn compile : compile the project and create a target folder classes folder and the compile classes

   
mvn test: will excute the test file in maven project  and creates test-classes folder in target folder and the test class. When the preceding command is executed, Maven runs all goals associated with each of the phases up to and including the test-phase. In such a case, Maven runs the "resources:resources"-goal associated with the process-resources-phase, then "compiler:compile", and so on until it finally runs the "surefire:test"-goal.


mvn install: publish the jars that you created in the local repoitory


mvn –U clean install: will call mvn clean first and then mvn install.


mvn package: will compile all the Java files, run any tests, creates jar or war and package the deliverable code and resources into target/my-app-1.0.jar (assuming the artifactId is my-app and the version is 1.0.)

Using Maven, the user provides only configuration for the project, while the configurable plug-ins do the actual work of compiling the project, cleaning target directories, running unit tests, generating API documentation and so on. In general, users should not have to write plugins themselves. Contrast this with Ant and make, in which one writes imperative procedures for doing the aforementioned tasks.


Build lifecycles

Build lifecycle is a list of named phases that can be used to give order to goal execution. One of Maven's standard lifecycles is the default lifecycle, which includes the following phases, in this order:
1.     validate
2.     generate-sources
3.     process-sources
4.     generate-resources
5.     process-resources
6.     compile
7.     process-test-sources
8.     process-test-resources
9.     test-compile
10.  test
11.  package
12.  install
13.  deploy



Goals provided by plugins can be associated with different phases of the lifecycle. For example, by default, the goal "compiler:compile" is associated with the compile-phase, while the goal "surefire:test" is associated with the test-phase.
Example:
 mvn compiler:compile

  

Importing Project in Eclipse:

Ø  Before importing in eclipse execute the following command. Below command will help in converting a maven based Java project to support IDE i.e. eclipse

mvn eclipse:eclipse
For web application, you need extra parameter to make it support Eclipse’s wtp (web tool platform), instead, you should use this command :

mvn eclipse:eclipse -Dwtpversion=2.0





  
Ø   After executing the command start import your Maven based web application into Eclipse IDE.
Steps:
In Eclipse IDE, menu bar , File -> Import… -> Maven -> select Existing Maven Projects -> Next -> select root directory (select your project folder) ->Next -> Next -> Finish.



No comments:

Post a Comment