Maven Advanced

A maven project is a special directory structure  which is accepted by all industry to develop web application and enterprise application.It is used to provide clean separation between all the classes,XML file and the jar file in a well formed manner.
A typical maven project  contains some standard directory structure for every java based web project and enterprise project. Its structure is like......

 Folder Directory Explanation.





List OF Maven Command

. Compile
  1. mvn compile 
    . Compile (offline)
    1. mvn -o compile  
    it will save you a lot of time because it will not download any thing from internet.

    3. Install the generated output to the respective repository
    1. mvn install  
    4. Clean Target Directory
    1. mvn clean  
    5. Package as jar or war
    1. mvn package  
    6. Generate Eclipse project descriptors
    1. mvn eclipse:eclipse  
    7. Run unit tests
    1. mvn test  
    8. Run unit tests and print the output to console rather than to file

    1. mvn -Dsurefire.useFile=false test  
    9. Run individual test
    1. mvn test –Dtest=org.shankh.mavenTest  
    10. Package without running tests
    1. mvn package -Dmaven.test.skip  
    11. Install without Running Tests
    1. mvn install -Dmaven.test.skip  
    12. To download source code into your maven repo:

    1. mvn eclipse:clean eclipse:eclipse –DdownloadSources=true  
    13. Create a new project

    1. mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app  
    Comment : Instead of ‘generate’ i wrote ‘create’ initially, but as simonz correctly pointed out, mvn archetype:create is now deprecated. so use mvn archetype:generate instead.
    14. Create a Web Application

    1. mvn archetype:generate  
    2.         -DarchetypeGroupId=org.apache.maven.archetypes  
    3.         -DarchetypeArtifactId=maven-archetype-webapp  
    4.         -DgroupId=com.mycompany  
    5.         -DartifactId=my-app  
    15. Start embedded Jetty
    1. mvn jetty:run  
    16. Deploys a WAR to embedded instance of Jetty and starts it
    1. mvn jetty:run-war  
    17. Specify Java Build version
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
     </plugins>
    </build>
    18. Generate site for a single project
    1. mvn site  
    19. Generate Javadoc
    1. mvn javadoc:javadoc  
    20. Set Maven_opts System environment variable (will save you from out of memory error)

    1. MAVEN_OPTS=”-Xmx512m -Xms256m -XX:MaxPermSize=128m”  
    21. Setting M2 variable in eclipse (informing of repository location
    Goto Window -> Preferences -> Java -> build path -> classpath variable -> New
    Name: M2_REPO
    Path:
    C:\Users\my_user_name\.m2\repository  (In vista)
    C:\Documents And Settings\my_user_name\.m2\repository  (In XP)
    ~/.m2/repository (in Linus/ unix flavours)

            Creating a maven project in eclipse

    we can create a new maven project by using eclipse or we can convert the eclipse project into maven project in simple manner.

    Creating a new scratch level maven  project 

    Screenshot is ......


    1.create a new dynamic project by runing commnad like  ..

       mvn archetype:generate    -DarchetypeGroupId=org.apache.maven.archetypes           -DarchetypeArtifactId=maven-archetype-webapp    -DgroupId=com.student -DartifactId=MyMavenApp
    Here we are mentioning the project name  "MyMavenApp" and package name  "com.student " as a artifactId and groupId.
    It will create a new maven template and with the project name and package name.

     2.create minimum some source folders for a the project under the project by right clicking on the project.
     MyMavenApp->right click->other->source folder.
      like...

    src/main/java                                                                   

    src/main/webapp

    src/main/webapp

    src/test/java

    src/test/resources

     

    3. Create some java package under the src/main/java folder to keep all the java file.

    4. write some XML configuration file under src/main/resources folder.

    5. create a WEB-INF folder under src/main/webapp folder and place the web.xml file under that folder.Place all the jsp under the src/main/webapp folder.

    6.If u have some unit testing java file place them under src/test/java folder else ignore it.

    7.If u have some unit testing related XML configuration file then place them under src/test/resources folder else ignore it.

    8.Create a pom.xml file and place all the jar file information as a maven dependency to it.

    9.Run the maven command to compile all the java classes and to place  the required jar file into the eclipse workspace.

    ex..mvn   -e compile

    10.Run maven command to execute all the java file and to create jar or war file based on our requirement...

    ex..mvn -e install

     11.If you want to clean all the file and create a new one then provide the clean command for flushing all the  maven created file .
    ex..mvn -e clean
    we can use this command for  togather with install or compile command

    ex.mvn -e clean install 

    or mvn -e clean compile.

    12.If we want to import a project which  we will be checking out from svn repository  the we have  to use eclipse command to create eclipse dot project and dot class-path file .  like.......

    mvn eclipse:eclipse

    Project Life cycle.

    Project life cycles are central to Maven 2. Most developers are familiar with the notion of build phases such as compile, test, and deploy. Ant has targets with names like those. In Maven 1, corresponding plug-ins are called directly. To compile Java source code, for instance, the java plug-in is used:
     $maven java:compile 
    


    In Maven 2, this notion is standardized into a set of well-known and well-defined lifecycle phases (see Figure 2). Instead of invoking plug-ins, the Maven 2 developer invokes a lifecycle phase: $mvn compile.

     

    A simple screenshot of maven project is 

     

     

    Some of the more useful Maven 2 life cycle phases are the following:
    • generate-sources: Generates any extra source code needed for the application, 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, WAR, 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, copies the final package to the remote repository for sharing with other developers and projects

     

    ....................Feel free to give a Feedback....................


  
     

4 comments:

  1. Thank u so much...It s very Helpful..

    ReplyDelete
  2. this explanation is giving clear cut idea about maven.this is very understandable and excellent explanation.thnks a lot..........
    one request plz give lk ths axplanation about log4j....

    ReplyDelete
  3. Good Work....Keep it up and Thanx alot....

    ReplyDelete