Tutorial for Maven Beginners

Tutorial for Maven Beginners:

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.

Note : Download Maven  from this Maven Download

Maven's Objectives:

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


Proxy configuration in <Maven_Home>/conf/settings.xml:

    <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <username>ktpot</username>
      <password>*** </password>
      <host>10.60.54.40</host>     
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>


Sample POM.XML:

<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.ktpot.blog</groupId>
  <artifactId>ktpot</artifactId>
  <name>Knowledge Transfer Pot</name>
 <packaging>war</packaging>
 <version>6.0.0-Beta</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>

New dependency can be added into pom.xml.

Maven Project Structure:

Directory namePurpose
project homeContains the pom.xml and all sub-directories.
src/main/javaContains the deliverable Java sourcecode for the project.
src/main/resourcesContains the deliverable resources for the project, such as property files.
src/test/javaContains the testing Java sourcecode (JUnit or TestNG test cases, for example) for the project.
src/test/resourcesContains resources necessary for testing.

Useful Commands:


Check Version:
mvn --version

Create a New Project:
mvn archetype:generate -DgroupId=com.ktpot.blog -DartifactId=ktpot -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Clean a project:
mvn clean
Compile a project:
mvn compile
Run unit tests (it also compiles a project):
mvn test
Build a package (it also executes unit tests):
mvn package
Run integration test (it also builds a package):
mvn verify
Install a package into local repository (it also runs integration tests):
mvn install
Install an artifact into local repository (skip integration test execution):
mvn -DskipITs=true install
Install an artifact into local repository (skip unit and integration test execution):
mvn -DskipTests=true install

mvn eclipse:eclipse – Run this command where the pom.xml resides. It will create the .project so that you can easily import into eclipse
mvn eclipse:eclipse -Dwtpversion=2.0 - Run this command where the pom.xml resides. It will create dynamic web content project so that you can import the project directly into eclipse and run it in your development environment.

Complete Maven video tutorial available in the below link.(Along with Eclipse Configuration)
http://www.classiclearn.com/java/maven-tutorials-video_fa35be16f.html







0 comments: