Saturday, January 21, 2012

Top 10 Maven Commands tutorial




Maven is a build automation management tool in java projects. Most of the people working on java related technologies know about the maven usage. I am listing down the list of Maven commands with examples reference which is very helpful for developer. This list can be used by all the Java developers for their daily usage in projects.

Please have a look of my previous article maven installation. For the maven installation, the jdk is required.

Listing down all the maven command tutorial at a high level.

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 the 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 the command prompt and creates a project 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 creates a project. This project contains the src/main/java and src/test/java folders which contain the HelloWorld java program in the main folder, and test class for hell0world program in the 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 project in eclipse. With this command also, we can create web projects like spring, struts which contain, WEB-INF, lib, classes folders and web.xml. This command runs at the command prompt and creates 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 creates 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 the jar, war files when you run the project with maven install.
Clean is maven predefined goal, this command deletes all the contents in the target folder.

4. compile maven project

mvn  compile
compile is maven predefined goal, this command compile all your java classes which include src files as well as text 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.
The package is a 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 these files to target folder and create a jar, war file. The final output for this command is jar/war of your project located in the target folder.

6. Deploy/install 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 the 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 the project

maven javadoc:javadoc
This will generate java documentation for your project. And the generated java doc report can be found in the 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  
The 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.


EmoticonEmoticon

Note: Only a member of this blog may post a comment.