How to build project with Maven

To build a Maven based project, open your console, change to your project folder where pom.xml file is placed, and issue this command :


mvn package

This will execute the Maven “package” phase.

Maven build lifecycle
Maven is run by phases, read this default Maven build lifecycle article for more detail. So, when the “package” phase is executed, all its above phases – “validate“, “compile” and “test“, including the current phase “package” will be executed orderly.

“mvn package” example

When you run “mvn package” command, it will compile source code, run unit test and pack it depends on your “packaging” tag in pom.xml file. For example,

1. If “packaging” = jar, it will package your project into a “jar” file and put it into your target folder

File : pom.xml


<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>Maven Example</artifactId>
	<packaging>jar</packaging>
	...

2. If “packaging” = war, it will package your project into a “war” file and put it into your target folder.

File : pom.xml


<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong</groupId>
	<artifactId>Maven Example</artifactId>
	<packaging>war</packaging>
	...

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Trupti
Guest
Trupti

I am getting this error-
Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1.1 from any of the configured repositories

Please help me resolve this issue.

Joseph
Guest
Joseph

Hi. My project team is using maven build. We have a package name as **/**/**/RCS under which all the classes are getting generated. But the jar does not contain these classes for some reason. Is there a way to include these classes into the RCS.jar?

There is no problem with other packages having a similar format.

Please suggest where the issue can be.

Thanks in advance!!!

http://wiredtreecouponcode2013.drupalgardens.com
Guest
http://wiredtreecouponcode2013.drupalgardens.com

Normally I do not learn article on blogs, however I wish to say that
this write-up very forced me to check out and do it! Your
writing taste has been amazed me. Thank you, quite nice post.

Akram
Guest
Akram

Hi mkyong, I have a question please, we have an ant build.xml, we want to migrate to maven, how to convert targets from build.xml to maven.
thanks, your help is appreciated.

rohit
Guest
rohit

how to genrate ear file from Maven?

Siva
Guest
Siva
when i run mvn package. i am getting the following error.. Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\namasivayams&gt;cd C:\test\deleteLater\testMVNProject\mkyongweb-core C:\test\deleteLater\testMVNProject\mkyongweb-core&gt;mvn package [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building mkyongweb-core Maven Webapp [INFO] task-segment: [package] [INFO] ------------------------------------------------------------------------ [WARNING] POM for 'org.apache.maven.wagon:wagon-file:pom:1.0-beta-2:runtime' is invalid. Its dependencies (if any) will NOT be available to the current build. [WARNING] POM for 'org.apache.maven.wagon:wagon-ssh-external:pom:1.0-beta-2:runt ime' is invalid. Its dependencies (if any) will NOT be available to the current build. [WARNING] POM for 'org.apache.maven.wagon:wagon-ssh:pom:1.0-beta-2:runtime' is i nvalid. Its dependencies (if any) will NOT be available to the current build. [WARNING]… Read more »
venkatesh
Guest
venkatesh

i am runing the server in debuging mode i am getting this error:Could not initialize the ServletContextListener