Maven Tutorial

Apache Maven, is an innovative software project management tool, provides new concept of a project object model (POM) file to manage project’s build, dependency and documentation. The most powerful feature is able to download the project dependency libraries automatically.
In this tutorials, it provides many step by step examples and explanations on using Apache Maven 2.x.
Happy learning Maven. :)
Maven Installation & Configuration
Installation guide on Windows and Ubuntu.
- Install Maven on Windows
Article about how to install Maven on Windows. - Install Maven on Ubuntu
Article about how to install on Ubuntu. - Enable proxy access in Maven
To use proxy server to connect internet, you have to configure the proxy setting in Maven.
Maven Repository
Maven local, central and remote repository configuration and explanation, some terms you may need to understand before use Maven.
- What is Maven local repository
Maven local repository is used to store the project dependency libraries, and the default folder is “.m2″, you may need to change it to another folder. - What is Maven central repository
Maven central repository is the default location for Maven to download all the project dependency libraries. - What is Maven remote repository, and how to configure it
Not all libraries are store in Maven central repository, often times, you need to add some remote repositories to download the libraries from another location instead of the default central repository. - Maven library dependency explanation
Article about the different of dependency library in tradition way and Maven way, and explains where Maven will search those library. - Install custom library into Maven local repository
Many libraries are still not support Maven pom.xml concept, here’s a guide to show how to include the “non-maven-support” library into Maven local repository.
Maven based project and Eclipse IDE
Examples to create Java project and web application with Maven, and how to import it into Eclipse IDE.
- Create a Java project with Maven
Uses Maven to create a Java project. - Convert Maven based Java project to support Eclipse IDE
Guide to convert Maven based Java project to support Eclipse IDE. - Create a Web Application Project with Maven
Uses Maven to create a Web Application Project. - Convert Maven based web application to support Eclipse IDE
Guide to convert Maven based web application to support Eclipse IDE. - Create a project with Maven template
Alternatively, you can create a standard project from Maven templete. - Convert existing Java web project to Maven based project
Guide to convert existing Java web project to Maven based web application project, and support Eclipse IDE.
Maven Basic Operations
Some basic operations, compile, build, unit test, install, generate site and deploy your Maven based project.
- How to build project with Maven
“mvn package” to build project. - How to clean project with Maven
“mvn clean” to clean project. - How to run unit test with Maven
“mvn test” to run unit test. - How to install your project into Maven local repository
“mvn install” to package and deploy project to local repository. - How to generate a documentation site for your Maven based project
“mvn site” to generate documentation site for your project information. - How to deploy site with “mvn site-deploy” – WebDAV example
“mvn site-deploy” to deploy generated documentation site to server automatically, via WebDAV. - How to deploy Maven based war file to Tomcat
“mvn tomcat:deploy” to deploy WAR file to Tomcat.
Maven FAQ
Some common Maven FAQs.
- Add M2_REPO classpath variable to Eclipse IDE
To make your Maven project support Eclipse IDE, you need to add this M2_REPO classpath variable. - How to display Maven plugin goals and parameters
Use Maven to diplsay all available goals and parametera of a plugin. - Integrate Maven with Eclipse via External Tool Configuration
How to integrate Maven with Eclipse IDE. - Skip unit test during Maven build
Tip to skip running your unit test in Maven. - Change Maven resources folder location
A guide to show how to add another resource folder to your Maven’s project. - Search Maven coordinates (dependency) with Google
How do you know the library Maven coordinate? Search it folder by folder on Maven repository? Here’s a tip to use Google site search to get the Maven coordinate from Maven repository. - Create Ant build file from Maven pom.xml
A plugin to generate Ant build file (build.xml) from Maven’s pom.xml file. - Generate source code jar for Maven based project
Uses “maven-source-plugin” to pack your source code and deploy along with your project. - Generate javadoc jar for Maven based project
Uses “maven-javadoc-plugin” to generate javadoc jar and deploy along with your project. - Download J2EE API (javaee.jar) via Maven
- Download JavaMail API via Maven
- Download standard.jar (taglib) & jstl.jar via Maven
Maven Errors
Some common error messages in Maven.
- Maven dependency libraries not deploy in Eclipse IDE
- Unsupported WTP version: 1.5
- Annotations are not supported in -source 1.3
- Tomcat deploy Maven project web.xml to a wrong folder in Eclipse
- Maven + WebDAV – Embedded error: Failed to transfer file: … Return code is: 405
Maven References
- Apache Maven official tutorials
- Apache Maven (Wiki)
- http://maven.apache.org/guides/mini/guide-ide-eclipse.html
- http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html
- http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Hey,mkyong.May I ask you a question?
I created a Maven Project using maven-archetype-quikstart plugin in Eclipse 4.2.2. When I right click the pom.xml and run mvn test command,what I saw in console is the following:
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ————————————————————————
—————————————————
constituent[0]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/aether-api-1.13.1.jar
constituent[1]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/aether-connector-wagon-1.13.1.jar
constituent[2]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/aether-impl-1.13.1.jar
constituent[3]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/aether-spi-1.13.1.jar
constituent[4]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/aether-util-1.13.1.jar
constituent[5]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/commons-cli-1.2.jar
constituent[6]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-aether-provider-3.0.5.jar
constituent[7]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-artifact-3.0.5.jar
constituent[8]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-compat-3.0.5.jar
constituent[9]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-core-3.0.5.jar
constituent[10]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-embedder-3.0.5.jar
constituent[11]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-model-3.0.5.jar
constituent[12]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-model-builder-3.0.5.jar
constituent[13]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-plugin-api-3.0.5.jar
constituent[14]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-repository-metadata-3.0.5.jar
constituent[15]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-settings-3.0.5.jar
constituent[16]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/maven-settings-builder-3.0.5.jar
constituent[17]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/plexus-cipher-1.7.jar
constituent[18]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/plexus-component-annotations-1.5.5.jar
constituent[19]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/plexus-interpolation-1.14.jar
constituent[20]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/plexus-sec-dispatcher-1.3.jar
constituent[21]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/plexus-utils-2.0.6.jar
constituent[22]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/sisu-guava-0.9.9.jar
constituent[23]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/sisu-guice-3.1.0-no_aop.jar
constituent[24]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/sisu-inject-bean-2.3.0.jar
constituent[25]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/sisu-inject-plexus-2.3.0.jar
constituent[26]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/wagon-file-2.4.jar
constituent[27]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/wagon-http-2.4-shaded.jar
constituent[28]: file:/E:/ASF/apache-maven-3.0.5-bin/apache-maven-3.0.5/lib/wagon-provider-api-2.4.jar
—————————————————
Exception in thread “main” java.lang.UnsatisfiedLinkError: sun.nio.ch.IOUtil.iovMax()I
at sun.nio.ch.IOUtil.iovMax(Native Method)
at sun.nio.ch.IOUtil.(Unknown Source)
at sun.nio.ch.Util.(Unknown Source)
at sun.nio.ch.FileChannelImpl.(Unknown Source)
at java.io.FileInputStream.getChannel(Unknown Source)
at org.sonatype.aether.impl.internal.TrackingFileManager.read(TrackingFileManager.java:58)
at org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager.readRepos(EnhancedLocalRepositoryManager.java:144)
at org.sonatype.aether.impl.internal.EnhancedLocalRepositoryManager.find(EnhancedLocalRepositoryManager.java:69)
at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:307)
at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:216)
at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:193)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:281)
at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:186)
at org.sonatype.aether.impl.internal.DefaultRepositorySystem.readArtifactDescriptor(DefaultRepositorySystem.java:279)
at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:115)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getPluginDescriptor(DefaultMavenPluginManager.java:142)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getMojoDescriptor(DefaultMavenPluginManager.java:261)
at org.apache.maven.plugin.DefaultBuildPluginManager.getMojoDescriptor(DefaultBuildPluginManager.java:185)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.setupMojoExecution(DefaultLifecycleExecutionPlanCalculator.java:152)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.setupMojoExecutions(DefaultLifecycleExecutionPlanCalculator.java:139)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.java:116)
at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.java:129)
at org.apache.maven.lifecycle.internal.BuilderCommon.resolveBuildPlan(BuilderCommon.java:92)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
BTW,it works when I run it in prompt.
Any helps would be appreciated!
THX…
Hai mkyong, this satish developing web application using spring3 mvc and Hibernate with maven and jboss,I’m configuring all the dependecies in pom.xml but my problem is how to configure jboss4.2.3 in pom.xml using maven 2.x to deploy the web application war file in jboss server deploy folder automatically.? Can you guide me Regarding this post????????
whenver I try to use any MVN command i get the following error.
C:\>mvn eclipse:eclipse -Dwtpversion=1.5
[INFO] Scanning for projects…
[INFO] Searching repository for plugin with prefix: ‘eclipse’.
[INFO] org.apache.maven.plugins: checking for updates from central
[WARNING] repository metadata for: ‘org.apache.maven.plugins’ could not be retri
eved from repository: central due to an error: Error transferring file: Connecti
on refused: connect
[INFO] Repository ‘central’ will be blacklisted
[INFO] ————————————————————————
[ERROR] BUILD ERROR
[INFO] ————————————————————————
[INFO] The plugin ‘org.apache.maven.plugins:maven-eclipse-plugin’ does not exist
or no valid version could be found
[INFO] ————————————————————————
[INFO] For more information, run Maven with the -e switch
[INFO] ————————————————————————
[INFO] Total time: 1 second
[INFO] Finished at: Thu May 02 11:47:01 IST 2013
[INFO] Final Memory: 1M/4M
[INFO] ————————————————————————
How to resolve this error?
Can you message me with some hints on how you made this web site look this cool, I??d be thankful.
Please can i find here maven+ hibernate/facetsJPA/spring3+jsf
I actually Feel posting, _Maven Tutorial_ was good!
I actuallycan’t see eye to eye together with u more! Finally looks like Istumbled upon a blog truly worth browsing. I appreciate it, Cynthia
I want to create a table as below:
| ID | BlockerID | BlockedID |
| 1 | 5 | 6 |
| 2 | 5 | 7 |
| 3 | 5 | 8 |
| 4 | 5 | 10 |
| 5 | 9 | 5 |
So 5 has blocked 6, 7, 8, 10 and 9 has blocked 5.
Is possible to do thins like this:
@Entity
@Table(name=”BLOCKEE”)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Blockee
{
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name=”BLOCKEE_ID”)
@XmlElement
private int blockeeid;
@OneToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name=”BLOCKEE”, joinColumns= {@JoinColumn(name=”BLOCKER_ID”)},
inverseJoinColumns=@JoinColumn(table=”USER”,name=”USER_ID”))
private User userid;
@OneToMany(cascade= {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name=”BLOCKEE”, joinColumns={@JoinColumn(name=”BLOCKED_ID”)},
inverseJoinColumns=@JoinColumn(table=”USER”,name=”USER_ID”))
private Collection userlistId;
//getters and setters
}
Here is my User class:
Entity
@Table(name = “USER”)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User
{
@Id
@Column(name = “USER_ID”)
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlElement
private int userid;
@Column(name = “FIRSTNAME”, nullable = true, unique = false)
@XmlElement
private String firstname;
@Column(name = “LASTAME”, nullable = true, unique = false)
@XmlElement
private String lastname;
//getters and setters
}
Or who can I do it with JPA?
Thanks
mvn site:stage related configuration and examples, as I’m running automation test cases and the cases are executing multiple time(i.e two times).
good work it is really helpfull us………..
Can I put maven pom jars into a folder in Eclipse? My pom jars are in the root of the project, it’s not good for the layout, especially when I have many jars.
Thanks.
thanks.learn a lot of things
Thanks a lot! Your tutorials helped me today!
This Side Save my Job Some times because it really helps in understanding the concept and then the person in good touch to implement the concepts.Thanks
Its really helpful for beginers
Hi mate,
Just want to know that how can I deploy artefacts using -DaltResposiory=releaseRespotirt::::http://abc.com, the problem is that I do not know what to provide for ‘layout’ value? I have around 16 projects and I ant to deploy all one by one and can not use as I want to control my deployment via settings.xml. I have defined two repository ‘releaseRepository’ and ‘snapshotRepository’ and want to switch at build time via parameters passed. Your comments will be appreciated.
Thanks,
–
Sheeraz
This is the correction for above comment;
I can not use
Thanks
The http link for the article “Generate javadoc jar for Maven based project” is wrong,you may be change it to http://www.mkyong.com/maven/generate-javadoc-jar-for-maven-based-project/
god of tutorials .. Detailed and simple steps explanation is awesome.. Keep it Up
Very nice piece of information at one location…thnx
You are the God of tutorials.. Helped me a lot in understanding maven..Thanks!!!
Thanks for your guidance. really Helpfull.
Hi mkyong,
I did setting for Maven following your previous tutorials,but when i run this goal “mvn archetype:generate” in command window, build failed showing
[ERROR] No plugin found for prefix ‘archetype’ in the current project and in the
plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the….
Have checked proxy settings in “Internet Options” but there was nothing,have stucked here .
Please tell me some solution so that I can go forward.
Thanks.
Hi Mkyong,
Superb work !! These are masterpieces !! Thanks for writing all these articles and these will certainly help the development community and we have very less time digging the specifications published by these Opensource development teams !! Keep doing the work! I appreciate if you can publish your ideas on designing the webservices concepts (i mean the desing issues like performance, scalability, availability and standard approaches for services design etc)…
And finally, coming to build, I liked these articles but wanted to ask you one questions…. Suppose If I have some JSPs, Java files, XLS files etc in my web application, and I want to rebuild the specific areas of changes rather than…. all the content.. i.e, If I change the JSPs then build that part and deploy and If I change XSL file build those files got modified and deploy rather than build everything .. How can i do that?
Thanks
Ram
If it is a java example I am looking for…then i look for mkyong.com.
Valuable information. Lucky me I discovered your site by accident, and I’m shocked why this twist of fate did not happened in advance! I bookmarked it.
Thanks a lot! Your tutorials are great.
Hi,
I want to read the names of services for which i have given build?Please help me
Regards
Kiran
Thanks nice tutorial..
Thank you for your effort to put this together in a easy to read style.
Great style, right to the point. Will save it as a good reference for Maven. Thank you heaps for your efforts.
Thank you for these tutorials .
Maven is a powerful project management tool.
Hello Mr Young,
I am doing as instructed.On the command line i see maven downloading dependencies.But in the end i am not able to find any jar files in local repository.Please suggest a resolution for this.
Many Thanks
When you use Maven to build your project, it will download the dependencies that declared in your project
pom.xmlFor example, if you are using Eclipse, uses “
mvn eclipse:eclipse“@salman
thanks mkyong for helping us out with these tutorial.
i shall answer to this question with your permission ,actually all the dependencies are combined together and stored in common jar format.
when you install the dependencies by mvn install it looks in some of the maven repositories and installs the respective dependencies.
now if your requirement is to see all the dependencies in common folder use this command
“mvn dependency:copy-dependencies”
this will store all the dependencies in your target/dependency folder.
I think the main thing to use dependency management of maven is like that,
1, install maven plugin
2, set local repository in .m2/setting.xml and activate it.
3, click ‘mvn test’ to download dependency library automatically.
thanks a lot for your effort .
i have a semi problem , my archetype local catalog is empty , so when i try
mvn archetype:generateit shows only the remote repository templates
when i force using local repository
no thing shows up , there isn’t any template there
so what could i do ??
thanx in advance
Hi,
I am new to Maven. I am using Maven3.0.
Your tutorials are good.
Could you please suggest what is the option number I need to choose to generate folder structure for java project in maven 3.0?
Thanks And Regards
Dhaya
mkyong hattsoff to you man, i appreciate your effort to opensource your thoughts and tutorials, immense thanks to you :)
take care
Hey,
Just a question about maven which has cropped up in the mkyong hibernate tutorial:
I know tutorials require effort to keep up-to-date so this is not a critical comment.
When I get a message such as:
Unable to find resource 'hibernate:hibernate3:pom:3.2.4.ga' in repository central (http://repo1.maven.org/maven2)
What am I supposed to do about it? It doesn’t say whether or not I’m supposed to do anyhting to fix it or that it is okay to proceed as is.
Mind to share me your Maven’s hibernate coordinate in pom.xml?