Artikel Terbaru

Saturday, 21 January 2012

Top 10 Maven Commands tutorial




Maven is build automation management  tool in java projects. Most of the people working on java related technologies know about the maven usage.

For developer purpose, listing down the list of maven commands with examples for reference
This list can be used by all the java developers for their daily usage in projects.
I have already blogged about maven installation <a href="http://www.cloudhadoop.com/2010/12/installing-maven.html">Installation of Maven</a>, if you are not installed maven. For maven installation,jdk1.6 environment is required.

Iam Listing down all the maven command tutorial at highlevel.

1.       Creating a sample maven standalone project:-
2.       Creating a sample web standalone project:-
3.       Clean project
4.       Compile project
5.       Building web apps:-
6.       Deploy project:-
7.       Run unit and integration tests  
8.       Ignore test execution
9.       Generate  java documentation for project
10.   Maven debug
11.   Generate site


1.Creating a maven standalone project:-

mvn archetype:generate -DgroupId=org.cloudhadoop -DartifactId=myproject

This is the starting step for any java project to create. This command runs at command prompt and  create  myproject with project name i.e. artifact id.  This project is located in group “org.cloudhadoop” using groupid,.
After running this command for the first time, this tries to download all the required artifacts downloaded from remote repositories ( from maven .. etc) and copy to your local repository and after that it create a project. This project contains the src/main/java and src/test/java folders which contain the hellowold java program in main folder, and test class for hell0world program in test folder.
2.Creating a web standalone project:-
mvn archetype:generate   -DarchetypeGroupId=org.apache.maven.archetypes  
-DarchetypeArtifactId=maven-archetype-webapp   -DgroupId=com.cloudhadoop  
-DartifactId=mywebapp
Most of the times, we usually create a web based projects in eclipse. With this command also, we can create web project like spring, struts which contains, WEB-INF, lib, classes folders and web.xml. This command runs at command prompt and create mywebapp with project name i.e. artifact id.  This project is located in group “org.cloudhadoop” using groupid,.
After running this command for the first time, this also  tries to download all the required artifacts downloaded from remote repositories ( from maven .. etc) and copy to your local repository and after that it create a project.
The directory structure is different from the #1 command and creates as per maven directory structure for web applications.
3. Cleaning project
mvn  clean
As you know target folder contains all your compiled classes, as well as jar,war files when you run project with maven install.
Clean is maven predefined goal, this command  delete all the contents in the target folder.

4. compile maven project
mvn  compile
compile is maven predefined goal ,this command  complie all your java classes which includes src files as well as test files in your project.

5.Build web apps:-
mvn package
maven  package goal is used to build  maven applications.
As you know any java project can be packed as jar or war.
Package is maven predefined goal. By giving this command, first compile all the java files(using compile goal)  and run all your test classes and copy all this files to target folder and create a jar, war file. The final out put for this command is jar/war of your project located in target folder.

6.Deploy project:-
mvn install or 
mvn deploy
When we run this command under your project,  it will do all the tasks  in “mvn package” and create the required jar/war file in target folder.
Maven install goal is used to deploy the project(jar/war) to the local repository. and local repository location is /.m2/repositories/groupid/.
Maven deploy goal is used to deploy the project to the remote repository like nexus. so that other developers can use this artifact in their module. remote repository location is specified in pom.xml.

7. Run unit and integration tests  :-

mvn test
Test goal is used to run only the test classes in your project. The test files are located under src/test/java and copy the result to your target/test-classes, target/reports if any.
8. Ignore test execution
mvn package –DskipTests or
mvn package -Dmaven.test.skip
In 4#, with maven package command run, tests are also executed. If you want to skip the unit tests, The we can use this command. You can use this option with install goal also.
9.Generate  java documentation for project

maven javadoc:javadoc
This will generate java documentation for your project. And the generated java doc report can be found in target folder
This will includes API documentation for your java classes in your project.
12.   Debug maven
mvn -X  
This command is used to start the maven goals in debug mode and gives logging information. This command gives more information like what artifact is failing for what reason .
This command can be used when you are not getting any clue for your maven project execution failure.

11.Generate site
mvn site  
Predefined goal in maven is used to generate site documentation in formatted style.

Please let me know if you like my post  and share your comments.