JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven










    
Maven Tutorial

  • Is a project management framework.
  • It is the product of apache group.
  • 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.
  • Projects created by maven tool can be easily managed by the other developer.
Repositories

  • This is the place where all jars required by the maven tool are stored.
  • It is of three types
    • Local (jar filea are downloaded from the remote)
    • Network / proxy
    • Central(remote repository)


POM(Porject Object Model)


    • This is declarative xml file created by the maven tool.
    • It is having project architecture.
    • It contains project configuration information.
    • It is used by the maven tool to download jar from the repository.
    • By architecture configuration values maven tool creates project structure.
    • This is present as pom.xml.
    • This can be inherited between parent and child project.


Maven Installation


  • It is easy to install.
  • It is present as .zip or .gz (for linux)
  • Download it from http://maven.apache.org/download.html
  • Extract it in c:\ or any where.
  • set maven_home and path as shown below.

     
C:\>set MAVEN_HOME=C:\apache-maven-3.0.4

C:\>set PATH=%PATH%;c:\apache-maven-3.0.4\bin

C:\>mvn  -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 14:14:56+0530)
Maven home: c:\apache-maven-3.0.4\bin\..
Java version: 1.6.0_11, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_11\jre




Creating HelloWorld Application in Maven

Maven Project is created in two ways:-
  • Without argument in mvn archetype:generate
  • With argument in mvn archetype:generate
Without argument in mvn archetype:generate
 
  • We will create a maven project.
  • This will be done by the mvn  file.
  • We need to provide differrent arguments with mvn, one is archetype:generate.
  • mvn archetype:generate is used to generate a maven project.
  • This command will ask you to provide following values
    • Archetype (this is project type by default it is no 16  i.e quickstart)
    • Group Id (package name)
    • Artifact Id (application name)
    • Version  and
    • package

  • First create one directory  mavenTest.
  • Change the directory to it.
  • Now put the commnand mvn archetype:generate
  • This will contact with remote repository.
  • It will crerate one folder  .m2 in the cureent User.
  • This will be like c:\Users\Shyam\.m2.
  • All required jar will be downloaded here.

  • Now the above command will give differrent  archetype.
  • By default it is quickstart (corejava applucation).

C:\mavenTest>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>
>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.
archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.appfuse.archetypes:appfuse-basic-jsf (AppFuse archetype for c
reating a web application with Hibernate, Spring and JSF)
2: internal -> org.appfuse.archetypes:appfuse-basic-spring (AppFuse archetype fo
r creating a web application with Hibernate, Spring and Spring MVC)
3: internal -> org.appfuse.archetypes:appfuse-basic-struts (AppFuse archetype fo
r creating a web application with Hibernate, Spring and Struts 2)
4: internal -> org.appfuse.archetypes:appfuse-basic-tapestry (AppFuse archetype
for creating a web application with Hibernate, Spring and Tapestry 4)
5: internal -> org.appfuse.archetypes:appfuse-core (AppFuse archetype for creati
ng a jar application with Hibernate and Spring and XFire)
6: internal -> org.appfuse.archetypes:appfuse-modular-jsf (AppFuse archetype for
 creating a modular application with Hibernate, Spring and JSF)
7: internal -> org.appfuse.archetypes:appfuse-modular-spring (AppFuse archetype
for creating a modular application with Hibernate, Spring and Spring MVC)
8: internal -> org.appfuse.archetypes:appfuse-modular-struts (AppFuse archetype
for creating a modular application with Hibernate, Spring and Struts 2)
9: internal -> org.appfuse.archetypes:appfuse-modular-tapestry (AppFuse archetyp
e for creating a modular application with Hibernate, Spring and Tapestry 4)
10: internal -> org.makumba:makumba-archetype (Archetype for a simple Makumba ap
plication)
11: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (A simpl
e J2EE Java application)
12: internal -> org.apache.maven.archetypes:maven-archetype-marmalade-mojo (A Ma
ven plugin development project using marmalade)
13: internal -> org.apache.maven.archetypes:maven-archetype-mojo (A Maven Java p
lugin development project)
14: internal -> org.apache.maven.archetypes:maven-archetype-portlet (A simple po
rtlet application)
15: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
16: internal -> org.apache.maven.archetypes:maven-archetype-quickstart ()

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 16:
Define value for property 'groupId': : pkg
Define value for property 'artifactId': : myapp
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  pkg: :
Confirm properties configuration:
groupId: pkg
artifactId: myapp
version: 1.0-SNAPSHOT
package: pkg
 Y: :
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-quickstart:RELEASE
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: pkg
[INFO] Parameter: packageName, Value: pkg
[INFO] Parameter: package, Value: pkg
[INFO] Parameter: artifactId, Value: myapp
[INFO] Parameter: basedir, Value: C:\mavenTest
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\mavenTest\myapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.511s
[INFO] Finished at: Mon Jun 10 09:47:58 IST 2013
[INFO] Final Memory: 7M/13M
[INFO] ------------------------------------------------------------------------



  • Here we will select number 16 for quickstart  corejava project.
  • Add the group id (package name ) pkg.
  • In artifactId add the application name i.e. myapp

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 16:
Define value for property 'groupId': : pkg
Define value for property 'artifactId': : myapp
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  pkg: :
Confirm properties configuration:
groupId: pkg
artifactId: myapp
version: 1.0-SNAPSHOT
package: pkg



  • This way it will generate myapp.
  • This myapp is having the following structure.


  • myapp is having pom.xml.
  • src\main\java\pkg\ has  App.java.
  • this App.java contains java code.

  • src\test\java\pkg\ has  AppTest.java.
  • AppTest.java contains testing junit   code.

App.java has following code


package pkg;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void mainString[] args )
    {
        System.out.println"Hello World!" );
    }
}

AppTest.java has following code

package pkg;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     @param testName name of the test case
     */
    public AppTestString testName )
    {
        supertestName );
    }

    /**
     @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuiteAppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTruetrue );
    }
}

Compilation

  • Now we will compile all the files by using mvn compile.
  • This will compile all the files.
  • It will put all the class files in the target directory.

C:\mavenTest>cd myapp

C:\mavenTest\myapp>mvn compile
[INFO] Scanning for projects...
[INFO] Building myapp 1.0-SNAPSHOT
[INFO] Compiling 1 source file to C:\mavenTest\myapp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS


  • Now App.class will be kept  in target\classes\pkg\App.class
  • AppTest.class will be kept in target\test-classes\pkg\AppTest.class

Running Program

  • We have to run App program as shown below.

C:\mavenTest\myapp>java -cp  target\classes pkg.App
Hello World!


Testing the Application
  • We can test our application for junit framework.
  • This is done by mvn test.
  • It will call target\test-classes\pkg\AppTest.class.
  • This is given as shown below.

C:\mavenTest\myapp>mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Compiling 1 source file to C:\mavenTest\myapp\target\test-classes
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ myapp ---
[INFO] Surefire report directory: C:\mavenTest\myapp\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running pkg.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------





pom.xml

  • The pom.xml is having the following structure

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>pkg</groupId>
  <artifactId>myapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>myapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

First part
  • First part has project structure as shown below.
  • It has groupId for package name
  • It has artifactId for application name
  <groupId>pkg</groupId>
  <artifactId>myapp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>


Second part
  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  • This part contains dependencies tag.
  • This contain differrent dependency required.
  • In the <dependency>  there is  gropuId,  artifactId and version.
  • In this case it has been provided for junit.
  • So junit 3.8.1 will be downloaded from repository.
  • There can be the dependency for servlet, ejb, jsf, struts or any other.
  • It is generally added by the maven.
  • Sometimes user has to add.

pom.xml in more descriptive way.

  • The following picture shows sample pom.xml





With argument in mvn archetype:generate
  • We will create a maven project.
  • This will be done by the mvn  file.
  • We need to provide differrent arguments with mvn, one is archetype:generate.
  • mvn archetype:generate is used to generate a maven project.
  • Now we will pass  following arguments in mvn archetype:generate
    • Archetype (this is project type by default it is no 16  i.e quickstart)
    • Group Id (package name)
    • Artifact Id (application name)
    • Version  and
    • package
  • Add the following code
C:\mavenTest>mvn archetype:generate -DgroupId=com.jaiswaltraining -DartifactId=testApp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.jaiswaltraining
[INFO] Parameter: packageName, Value: com.jaiswaltraining
[INFO] Parameter: package, Value: com.jaiswaltraining
[INFO] Parameter: artifactId, Value: testApp
[INFO] Parameter: basedir, Value: C:\mavenTest
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\mavenTest\testApp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS


Compilation

  • Change the directory to testApp.
  • Now we will compile all the files by using mvn compile.
  • This will compile all the files.
  • It will put all the class files in the target directory.
C:\mavenTest>cd testApp

C:\mavenTest\testApp>mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Compiling 1 source file to C:\mavenTest\testApp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS



Running Program

We have to run App program as shown below.

C:\mavenTest\testApp>java -cp  target\classes com.jaiswaltraining.App
Hello World!

Testing the Application
  • We can test our application for junit framework.
  • This is done by mvn test.
  • It will call target\test-classes\pkg\AppTest.class.
  • This is given as shown below.

C:\mavenTest\testApp>mvn test
[INFO] Scanning for projects...
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ testApp ---
[INFO] Surefire report directory: C:\mavenTest\testApp\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.jaiswaltraining.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.026 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS


Download above application.

testApp.zip

myapp.zip